Skip to content

Commit

Permalink
refactor(schema): Improve error message when noCallback is specified`
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed May 29, 2019
1 parent 791c010 commit 540f451
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
40 changes: 13 additions & 27 deletions api-metadata.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"definitions": {

"function_metadata": {
"type": "object",
"properties": {
Expand All @@ -16,44 +15,31 @@
"description": "If the function doesn't take a callback parameter in some browsers and throws an error when a callback is passed.",
"type": "boolean"
},
"noCallback": {
"description": "Set when `fallbackToNoCallback` is set and this browser doesn't support callbacks for this function.\nMUST NOT BE SET IN api-metadata.json.",
"type": "boolean"
},
"singleCallbackArg": {
"description": "If the function callback takes only one parameter.\nSet to `false` to enforce that the function's varargs are never unwrapped.",
"type": "boolean"
}
},
"required": [
"minArgs",
"maxArgs"
],
"not": {
"anyOf": [
{"required": ["noCallback"]}
]
},
"required": ["minArgs", "maxArgs"],
"additionalProperties": false
},

"namespace_metadata": {
"type": "object",
"not": {
"anyOf": [
{"required": ["minArgs"]},
{"required": ["maxArgs"]}
]
},
"additionalProperties": {
"anyOf": [
{ "$ref": "#/definitions/function_metadata" },
{ "$ref": "#/definitions/namespace_metadata" }
]
"additionalProperties": false,
"patternProperties": {
"^(?!(maxArgs|minArgs|noCallback)$).*": {
"anyOf": [
{ "$ref": "#/definitions/function_metadata" },
{ "$ref": "#/definitions/namespace_metadata" }
]
}
}
}

},

"$ref": "#/definitions/namespace_metadata"
"additionalProperties": {
"description": "Top-level namespace",
"$ref": "#/definitions/namespace_metadata"
}
}
22 changes: 20 additions & 2 deletions test/test-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@

const {assert} = require("chai");
const Ajv = require("ajv");
const betterAjvErrors = require("better-ajv-errors");
const betterAjvErrors = (() => {
// Wrapper to work around https://github.com/atlassian/better-ajv-errors/pull/21
const _betterAjvErrors = require("better-ajv-errors");
function betterAjvErrors(schema, data, errors, options) {
return errors
.map(e => _betterAjvErrors(schema, data, [e], options))
.join("\n\n");
}

Object.setPrototypeOf(betterAjvErrors, _betterAjvErrors);

return /** @type {typeof _betterAjvErrors} */ (betterAjvErrors);
})();

const ajv = new Ajv({jsonPointers: true, allErrors: true});

Expand All @@ -13,7 +25,13 @@ describe("api-metadata.json", () => {
it("api-metadata.json matches schema", () => {
const valid = ajv.validate(schema, data);
if (!valid) {
assert.fail(undefined, undefined, betterAjvErrors(schema, data, ajv.errors, {indent: 2}));
assert.fail(
undefined,
undefined,
`API Metadata doesn't match schema:
${betterAjvErrors(schema, data, ajv.errors, {indent: 2})}`,
);
}
});
});

0 comments on commit 540f451

Please sign in to comment.