Skip to content

Fix AJV warning, "$ref: keywords ignored in schema" #392

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

Merged
merged 2 commits into from
Dec 15, 2020
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
1 change: 0 additions & 1 deletion test/programs/comments-override/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
},
"sub2": {
"$ref": "#/definitions/MySubObject",
"additionalProperties": false,
"description": "Property-level description"
}
},
Expand Down
3 changes: 1 addition & 2 deletions test/programs/namespace-deep-1/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
],
"type": "object"
}
},
"type": "object"
}
}

3 changes: 1 addition & 2 deletions test/programs/namespace-deep-2/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
],
"type": "object"
}
},
"type": "object"
}
}

42 changes: 38 additions & 4 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,33 @@ import { resolve } from "path";
import { versionMajorMinor as typescriptVersionMajorMinor } from "typescript";
import * as TJS from "../typescript-json-schema";

const ajv = new Ajv();
let ajvWarnings: string[] = [];
const ajv = new Ajv({
logger: {
log: console.log,
warn: (message) => {
ajvWarnings.push(message);
},
error: (message) => {
throw new Error("AJV error: " + message);
},
},
});

const BASE = "test/programs/";

interface AjvTestOptions {
skipCompile: boolean;
expectedWarnings: string[];
}

export function assertSchema(
group: string,
type: string,
settings: TJS.PartialArgs = {},
compilerOptions?: TJS.CompilerOptions,
only?: boolean
only?: boolean,
ajvOptions: Partial<AjvTestOptions> = {}
) {
const run = only ? it.only : it;

Expand All @@ -37,7 +54,15 @@ export function assertSchema(
// test against the meta schema
if (actual !== null) {
ajv.validateSchema(actual);

assert.equal(ajv.errors, null, "The schema is not valid");

// Compiling the schema can reveal warnings that validateSchema doesn't.
if (!ajvOptions.skipCompile) {
ajvWarnings = [];
ajv.compile(actual);
assert.deepEqual(ajvWarnings, ajvOptions.expectedWarnings || [], "Got unexpected AJV warnings");
}
}
});
}
Expand Down Expand Up @@ -233,11 +258,20 @@ describe("schema", () => {

describe("annotations", () => {
assertSchema("annotation-default", "MyObject");
assertSchema("annotation-ref", "MyObject");
assertSchema("annotation-ref", "MyObject", {}, undefined, undefined, {
skipCompile: true
});
assertSchema("annotation-tjs", "MyObject", {
validationKeywords: ["hide"],
});
assertSchema("annotation-id", "MyObject");
assertSchema("annotation-id", "MyObject", {}, undefined, undefined, {
expectedWarnings: [
"schema id ignored",
"schema id ignored",
"schema id ignored",
"schema id ignored"
]
});
assertSchema("annotation-items", "MyObject");

assertSchema("typeof-keyword", "MyObject", { typeOfKeyword: true });
Expand Down
18 changes: 15 additions & 3 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,20 @@ const validationKeywords = {
id: true
};

/**
* Subset of descriptive, non-type keywords that are permitted alongside a $ref.
* Prior to JSON Schema draft 2019-09, $ref is a special keyword that doesn't
* permit keywords alongside it, and so AJV may raise warnings if it encounters
* any type-related keywords; see https://github.com/ajv-validator/ajv/issues/1121
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! This is what I should have done when fixing the other issue but it was too much back then. 👍

*/
const annotationKeywords: { [k in keyof typeof validationKeywords]?: true } = {
description: true,
default: true,
examples: true,
// A JSDoc $ref annotation can appear as a $ref.
$ref: true
};

const subDefinitions = {
items: true,
additionalProperties: true,
Expand Down Expand Up @@ -1262,10 +1276,8 @@ export class JsonSchemaGenerator {
this.recursiveTypeRef.delete(fullTypeName);
// If the type was recursive (there is reffedDefinitions) - lets replace it to reference
if (this.reffedDefinitions[fullTypeName]) {
// Here we may want to filter out all type specific fields
// and include fields like description etc
const annotations = Object.entries(returnedDefinition).reduce((acc, [key, value]) => {
if (validationKeywords[key] && typeof value !== undefined) {
if (annotationKeywords[key] && typeof value !== undefined) {
acc[key] = value;
}
return acc;
Expand Down