Skip to content

Commit 29168a4

Browse files
committed
add refs varaible for jtd validation
There seems to be some weird behavior with typescript when refs was left to default. By specifying it manually the behavior was fixed. fixes #2167
1 parent 490eb8c commit 29168a4

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

lib/core.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ export default class Ajv {
340340
validate<T>(schema: Schema | JSONSchemaType<T> | string, data: unknown): data is T
341341
// Separated for type inference to work
342342
// eslint-disable-next-line @typescript-eslint/unified-signatures
343-
validate<T>(schema: JTDSchemaType<T>, data: unknown): data is T
343+
validate<T, R extends Record<string, unknown>>(
344+
schema: JTDSchemaType<T, R>,
345+
data: unknown
346+
): data is T
344347
// This overload is only intended for typescript inference, the first
345348
// argument prevents manual type annotation from matching this overload
346349
validate<N extends never, T extends SomeJTDSchemaType>(
@@ -371,7 +374,10 @@ export default class Ajv {
371374
compile<T = unknown>(schema: Schema | JSONSchemaType<T>, _meta?: boolean): ValidateFunction<T>
372375
// Separated for type inference to work
373376
// eslint-disable-next-line @typescript-eslint/unified-signatures
374-
compile<T = unknown>(schema: JTDSchemaType<T>, _meta?: boolean): ValidateFunction<T>
377+
compile<T = unknown, R extends Record<string, unknown> = Record<string, unknown>>(
378+
schema: JTDSchemaType<T, R>,
379+
_meta?: boolean
380+
): ValidateFunction<T>
375381
// This overload is only intended for typescript inference, the first
376382
// argument prevents manual type annotation from matching this overload
377383
compile<N extends never, T extends SomeJTDSchemaType>(

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"scripts": {
1313
"eslint": "eslint \"lib/**/*.ts\" \"spec/**/*.*s\" --ignore-pattern spec/JSON-Schema-Test-Suite",
14-
"prettier:write": "prettier --write \"./**/*.{json,yaml,js,ts}\"",
14+
"prettier:write": "prettier --write --cache \"./**/*.{json,yaml,js,ts}\"",
1515
"prettier:check": "prettier --list-different \"./**/*.{json,yaml,js,ts}\"",
1616
"test-spec": "cross-env TS_NODE_PROJECT=spec/tsconfig.json mocha -r ts-node/register \"spec/**/*.spec.{ts,js}\" -R dot",
1717
"test-codegen": "nyc cross-env TS_NODE_PROJECT=spec/tsconfig.json mocha -r ts-node/register 'spec/codegen.spec.ts' -R spec",

spec/types/jtd-schema.spec.ts

+47
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,30 @@ describe("JTDSchemaType", () => {
353353
})
354354

355355
it("should typecheck ref schemas", () => {
356+
const ajv = new _Ajv()
356357
const refs: JTDSchemaType<number[], {num: number}> = {
357358
definitions: {
358359
num: {type: "float64"},
359360
},
360361
elements: {ref: "num"},
361362
}
363+
364+
// test that ajv.validate captures ref data type
365+
const validData: unknown = [0]
366+
if (ajv.validate(refs, validData)) {
367+
const postValidation: number[] = validData
368+
postValidation[0].should.equal(0)
369+
}
370+
should.not.exist(ajv.errors)
371+
372+
// test that ajv.compile captures ref data type
373+
const validate = ajv.compile(refs)
374+
if (validate(validData)) {
375+
const postValidation: number[] = validData
376+
postValidation[0].should.equal(0)
377+
}
378+
should.not.exist(validate.errors)
379+
362380
const missingDef: JTDSchemaType<number[], {num: number}> = {
363381
// @ts-expect-error
364382
definitions: {},
@@ -393,6 +411,35 @@ describe("JTDSchemaType", () => {
393411
void [refs, missingDef, missingType, nullRefs, refsNullOne, refsNullTwo]
394412
})
395413

414+
it("should typecheck nested ref schemas", () => {
415+
const ajv = new _Ajv()
416+
const schema: JTDSchemaType<{str: string; ref: number}, {num: number}> = {
417+
definitions: {
418+
num: {type: "int32"},
419+
},
420+
properties: {
421+
ref: {ref: "num"},
422+
str: {type: "string"},
423+
},
424+
}
425+
426+
// test that ajv.validate captures ref data type
427+
const validData: unknown = {str: "", ref: 0}
428+
if (ajv.validate(schema, validData)) {
429+
const validated: string = validData.str
430+
validated.should.equal("")
431+
}
432+
should.not.exist(ajv.errors)
433+
434+
// test that ajv.compile captures ref data type
435+
const validate = ajv.compile(schema)
436+
if (validate(validData)) {
437+
const validated: string = validData.str
438+
validated.should.equal("")
439+
}
440+
should.not.exist(validate.errors)
441+
})
442+
396443
it("should typecheck metadata schemas", () => {
397444
const meta: JTDSchemaType<number> = {type: "float32", metadata: {key: "val"}}
398445
const emptyMeta: JTDSchemaType<unknown> = {metadata: {key: "val"}}

0 commit comments

Comments
 (0)