Skip to content

Commit

Permalink
fix inference for JTDSchemaType
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbrinkman committed Mar 5, 2021
1 parent 3a8c461 commit f3424d5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ export default class Ajv {
// AnySchema will be compiled and cached using schema itself as a key for Map
validate(schema: Schema | string, data: unknown): boolean
validate(schemaKeyRef: AnySchema | string, data: unknown): boolean | Promise<unknown>
validate<T>(
schema: Schema | JTDSchemaType<T> | JSONSchemaType<T> | string,
data: unknown
): data is T
validate<T>(schema: Schema | JSONSchemaType<T> | string, data: unknown): data is T
// This is separated to help typescript with inference
// eslint-disable-next-line @typescript-eslint/unified-signatures
validate<T>(schema: JTDSchemaType<T>, data: unknown): data is T
validate<T>(schema: AsyncSchema, data: unknown | T): Promise<T>
validate<T>(schemaKeyRef: AnySchema | string, data: unknown): data is T | Promise<T>
validate<T>(
Expand All @@ -323,10 +323,10 @@ export default class Ajv {

// Create validation function for passed schema
// _meta: true if schema is a meta-schema. Used internally to compile meta schemas of user-defined keywords.
compile<T = unknown>(
schema: Schema | JTDSchemaType<T> | JSONSchemaType<T>,
_meta?: boolean
): ValidateFunction<T>
compile<T = unknown>(schema: Schema | JSONSchemaType<T>, _meta?: boolean): ValidateFunction<T>
// This is separated to help typescript with inference
// eslint-disable-next-line @typescript-eslint/unified-signatures
compile<T = unknown>(schema: JTDSchemaType<T>, _meta?: boolean): ValidateFunction<T>
compile<T = unknown>(schema: AsyncSchema, _meta?: boolean): AsyncValidateFunction<T>
compile<T = unknown>(schema: AnySchema, _meta?: boolean): AnyValidateFunction<T>
compile<T = unknown>(schema: AnySchema, _meta?: boolean): AnyValidateFunction<T> {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"terser": "^5.2.1",
"ts-node": "^9.0.0",
"tsify": "^5.0.2",
"typescript": "^4.0.0"
"typescript": "^4.2.0"
},
"collective": {
"type": "opencollective",
Expand Down
4 changes: 2 additions & 2 deletions spec/types/jtd-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ const mySchema: JTDSchemaType<MyData> = {
describe("JTDSchemaType", () => {
it("validation should prove the data type", () => {
const ajv = new _Ajv()
const validate = ajv.compile<MyData>(mySchema)
const validate = ajv.compile(mySchema)
const validData: unknown = {type: "a", a: 1}
if (validate(validData) && validData.type === "a") {
validData.a.should.equal(1)
}
should.not.exist(validate.errors)

if (ajv.validate<MyData>(mySchema, validData) && validData.type === "a") {
if (ajv.validate(mySchema, validData) && validData.type === "a") {
validData.a.should.equal(1)
}
should.not.exist(ajv.errors)
Expand Down

0 comments on commit f3424d5

Please sign in to comment.