Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvements to JSONSchemaType for records #1564

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/types/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,24 @@ export type JSONSchemaType<T, _partial extends boolean = false> = (
// "properties" are optional for more concise dictionary schemas
// "patternProperties" and can be only used with interfaces that have string index
type: JSONType<"object", _partial>
// "required" type does not guarantee that all required properties are listed
// it only asserts that optional cannot be listed
required: _partial extends true ? Readonly<(keyof T)[]> : Readonly<RequiredMembers<T>[]>
additionalProperties?: boolean | JSONSchemaType<T[string]>
unevaluatedProperties?: boolean | JSONSchemaType<T[string]>
properties?: _partial extends true ? Partial<PropertiesSchema<T>> : PropertiesSchema<T>
patternProperties?: {[Pattern in string]?: JSONSchemaType<T[string]>}
propertyNames?: JSONSchemaType<string>
propertyNames?: Omit<JSONSchemaType<string>, "type"> & {type?: "string"}
dependencies?: {[K in keyof T]?: Readonly<(keyof T)[]> | PartialSchema<T>}
dependentRequired?: {[K in keyof T]?: Readonly<(keyof T)[]>}
dependentSchemas?: {[K in keyof T]?: PartialSchema<T>}
minProperties?: number
maxProperties?: number
}
} & (// "required" type does not guarantee that all required properties
// are listed it only asserts that optional cannot be listed.
// "required" is not necessary if it's a non-partial type with no required keys
_partial extends true
? {required: Readonly<(keyof T)[]>}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering - why "required" is required in partial schemas? I am probably missing something, as I write the example, it does not seem required (even when it has to be). This example type checks:

type MyUnion = {tag: "a"; a: number} | {tag: "b"; b: number}

const myUnionSchema: JSONSchemaType<MyUnion> = {
  type: "object",
  required: ["tag"],
  oneOf: [
    {
      properties: {
        tag: {const: "a"},
        a: {type: "number"},
      },
    },
    {
      properties: {
        tag: {const: "b"},
        b: {type: "number"},
      },
    },
  ],
};

but it probably it should have required: ["a"] (or "b") in subschemas...

Either way, it's unrelated issue - I am going to merge it as is - thank you very much!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it doesn't matter because because partial schemas are just that, partial:

export type PartialSchema<T> = Partial<JSONSchemaType<T, true>>

: [RequiredMembers<T>] extends [never]
? {required?: Readonly<RequiredMembers<T>[]>}
: {required: Readonly<RequiredMembers<T>[]>})
: T extends null
? {
nullable: true
Expand Down
31 changes: 31 additions & 0 deletions spec/types/json-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ const invalidSchema: JSONSchemaType<MyData> = {
type: [],
}

type MyEnumRecord = Record<"a" | "b" | "c" | "d", number | undefined>

describe("JSONSchemaType type and validation as a type guard", () => {
const ajv = new _Ajv({allowUnionTypes: true})

Expand Down Expand Up @@ -259,6 +261,35 @@ describe("JSONSchemaType type and validation as a type guard", () => {
should.not.exist(ajv.errors)
})
})

describe("schema should be simple for record types", () => {
it("typechecks a valid type without required", () => {
const myEnumRecordSchema: JSONSchemaType<MyEnumRecord> = {
type: "object",
propertyNames: {enum: ["a", "b", "c", "d"]},
additionalProperty: {type: "number"},
}
// eslint-disable-next-line no-void
void myEnumRecordSchema
})

it("requires required for non-optional types", () => {
// @ts-expect-error missing required
const requiredSchema: JSONSchemaType<{a: number}> = {
type: "object",
}
// eslint-disable-next-line no-void
void requiredSchema
})

it("doesn't require required for optional types", () => {
const optionalSchema: JSONSchemaType<{a?: number}> = {
type: "object",
}
// eslint-disable-next-line no-void
void optionalSchema
})
})
})

// eslint-disable-next-line no-void
Expand Down