Skip to content

Commit

Permalink
fix: undefined issue
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Dec 18, 2024
1 parent 4be8f35 commit e31973a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion nodepkg/typedef/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@innoai-tech/typedef",
"version": "0.3.8",
"version": "0.3.9",
"monobundle": {
"exports": {
".": "./src/index.ts"
Expand Down
47 changes: 24 additions & 23 deletions nodepkg/typedef/src/encoding/JSONSchemaDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const refName = (ref: string) => {
export class JSONSchemaDecoder {
static decode(
type: JSONSchema | false,
resolveRef: (ref: string) => [JSONSchema, string],
resolveRef: (ref: string) => [JSONSchema, string]
): Type {
if (type === false) {
return t.never() as any;
Expand All @@ -19,7 +19,8 @@ export class JSONSchemaDecoder {

def = new Map<string, Type>();

constructor(private resolveRef: (ref: string) => [JSONSchema, string]) {}
constructor(private resolveRef: (ref: string) => [JSONSchema, string]) {
}

decode(jsonSchemaObject: JSONSchema): Type {
const jsonSchema = structuredClone(jsonSchemaObject);
Expand All @@ -37,17 +38,17 @@ export class JSONSchemaDecoder {
if (hasTitle) {
tt = tt.use(
t.annotate({
description: jsonSchema?.["description"],
}),
description: jsonSchema?.["description"]
})
);
} else {
const [title, ...others] = jsonSchema?.["description"].split(/[.\n]/);

tt = tt.use(
t.annotate({
title: title,
description: others ? others.join("\n").trim() : undefined,
}),
description: others ? others.join("\n").trim() : undefined
})
);
}
}
Expand Down Expand Up @@ -95,8 +96,8 @@ export class JSONSchemaDecoder {
if (schema["x-enum-labels"]) {
return e.use(
t.annotate({
enumLabels: schema["x-enum-labels"],
}),
enumLabels: schema["x-enum-labels"]
})
);
}

Expand All @@ -106,15 +107,15 @@ export class JSONSchemaDecoder {
if (schema["discriminator"]) {
const discriminatorPropertyName = schema["discriminator"][
"propertyName"
] as string;
] as string;

if (discriminatorPropertyName) {
const mapping: Record<string, any> = {};

if (schema["discriminator"]["mapping"]) {
const discriminatorMapping = schema["discriminator"][
"mapping"
] as Record<string, any>;
] as Record<string, any>;

if (discriminatorMapping) {
for (const [k, sub] of Object.entries(discriminatorMapping)) {
Expand All @@ -129,13 +130,13 @@ export class JSONSchemaDecoder {

const discriminatorPropertyType = Schema.schemaProp(
sub,
"properties",
)[discriminatorPropertyName];
"properties"
)?.[discriminatorPropertyName];

if (discriminatorPropertyType) {
const discriminatorPropertyValue = Schema.schemaProp(
discriminatorPropertyType,
"enum",
"enum"
)?.[0];

if (!isUndefined(discriminatorPropertyValue)) {
Expand Down Expand Up @@ -191,9 +192,9 @@ export class JSONSchemaDecoder {
if (additionalProperties) {
return t.record(
this.decode(
schema["propertyNames"] ?? { type: "string" },
schema["propertyNames"] ?? { type: "string" }
) as Type<string>,
this.decode(additionalProperties),
this.decode(additionalProperties)
);
}

Expand All @@ -203,7 +204,7 @@ export class JSONSchemaDecoder {
if (isArrayType(schema)) {
if (isArray(schema["items"])) {
return t.tuple(
(schema["items"] as JSONSchema[]).map((s) => this.decode(s)) as any,
(schema["items"] as JSONSchema[]).map((s) => this.decode(s)) as any
);
}

Expand Down Expand Up @@ -254,7 +255,7 @@ const typeRelationKeywords: { [k: string]: string[] } = {
"dependentSchemas",

"maxProperties",
"minProperties",
"minProperties"

// "required",
// "dependentRequired",
Expand All @@ -269,23 +270,23 @@ const typeRelationKeywords: { [k: string]: string[] } = {
"minItems",
"uniqueItems",
"maxContains",
"minContains",
"minContains"
],
string: [
"pattern",
"contentMediaType",
"contentEncoding",
"contentSchema",
"maxLength",
"minLength",
"minLength"
],
number: [
"maximum",
"minimum",
"multipleOf",
"exclusiveMaximum",
"exclusiveMinimum",
],
"exclusiveMinimum"
]
};

export const validationRules = [
Expand All @@ -304,12 +305,12 @@ export const validationRules = [
"minimum",
"multipleOf",
"exclusiveMaximum",
"exclusiveMinimum",
"exclusiveMinimum"
];

const hasProps = <T extends object>(
schema: T,
props: Array<keyof T>,
props: Array<keyof T>
): boolean => {
return props.some((prop) => Object.hasOwn(schema, prop));
};
Expand Down

0 comments on commit e31973a

Please sign in to comment.