Skip to content

Commit

Permalink
Merge pull request #1564 from erikbrinkman/jsonschematype-improvements
Browse files Browse the repository at this point in the history
Some improvements to JSONSchemaType for records
  • Loading branch information
epoberezkin authored Apr 24, 2021
2 parents f45b963 + d88ba06 commit 2edc54c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
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)[]>}
: [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

0 comments on commit 2edc54c

Please sign in to comment.