Skip to content

Commit

Permalink
Merge pull request #1475 from erikbrinkman/infer
Browse files Browse the repository at this point in the history
fix inference for JTDSchemaType
  • Loading branch information
epoberezkin committed Mar 6, 2021
2 parents 80d09b3 + bbf7a51 commit 937643e
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 @@ -99,7 +99,7 @@
"terser": "^5.2.1",
"ts-node": "^9.0.0",
"tsify": "^5.0.2",
"typescript": "^4.0.0",
"typescript": "^4.2.0",
"vuepress": "^1.8.2"
},
"collective": {
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 937643e

Please sign in to comment.