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

fix inference for JTDSchemaType #1475

Merged
merged 2 commits into from
Mar 6, 2021
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
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