Skip to content

Commit

Permalink
use new addKeywords method api and keywords option in test
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Sep 1, 2020
1 parent 20ca3ff commit e675f8a
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 150 deletions.
16 changes: 11 additions & 5 deletions lib/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Vocabulary, KeywordDefinition, Options} from "./types"
import SchemaObject from "./compile/schema_obj"
import Cache from "./cache"
import {ValidationError, MissingRefError} from "./compile/error_classes"
import rules from "./compile/rules"
import $dataMetaSchema from "./data"
import {Options} from "./types"

var compileSchema = require("./compile"),
resolve = require("./compile/resolve"),
Expand Down Expand Up @@ -445,10 +445,16 @@ function addInitialFormats(self) {
}
}

function addInitialKeywords(self, keywords, skipValidation?: boolean) {
for (var name in keywords) {
var keyword = keywords[name]
self.addKeyword(name, keyword, skipValidation)
function addInitialKeywords(self, defs: Vocabulary | {[x: string]: KeywordDefinition}) {
if (Array.isArray(defs)) {
self.addVocabulary(defs)
return
}
self.logger.warn("keywords option as map is deprecated, pass array")
for (var keyword in defs) {
var def = defs[name]
if (!def.keyword) def.keyword = keyword
self.addKeyword(def)
}
}

Expand Down
6 changes: 2 additions & 4 deletions lib/keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ export function addKeyword(
if (typeof kwdOrDef == "string") {
keyword = kwdOrDef
if (typeof def == "object") {
// TODO enable once tests are updated
// this.logger.warn("these parameters are deprecated, see docs for addKeyword")
if (def.keyword === undefined) def.keyword = keyword
else if (def.keyword !== keyword) throw new Error("invalid addKeyword parameters")
this.logger.warn("these parameters are deprecated, see docs for addKeyword")
def.keyword = keyword
}
} else if (typeof kwdOrDef == "object" && def === undefined) {
def = kwdOrDef
Expand Down
4 changes: 2 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Options {
unicode?: boolean
format?: false | string
formats?: object
keywords?: object
keywords?: Vocabulary | {[x: string]: KeywordDefinition} // map is deprecated
unknownFormats?: true | string[] | "ignore"
schemas?: object[] | object
missingRefs?: true | "ignore" | "fail"
Expand Down Expand Up @@ -196,7 +196,7 @@ export interface KeywordErrorDefinition {
params?: (cxt: KeywordErrorContext) => Code
}

export type Vocabulary = KeywordDefinition[]
export type Vocabulary = (KeywordDefinition | string)[]

export interface KeywordErrorContext {
gen: CodeGen
Expand Down
7 changes: 4 additions & 3 deletions spec/async.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ describe("compileAsync method", () => {
})

function test(schema, expectedLoadCallCount) {
ajv.addKeyword("myFooBar", {
ajv.addKeyword({
keyword: "myFooBar",
type: "string",
validate: function (sch, data) {
return sch === data
Expand Down Expand Up @@ -326,7 +327,7 @@ describe("compileAsync method", () => {
})

it("if schema compilation throws some other exception", (done) => {
ajv.addKeyword("badkeyword", {compile: badCompile})
ajv.addKeyword({keyword: "badkeyword", compile: badCompile})
var schema = {badkeyword: true}
ajv.compileAsync(schema, shouldFail(done))

Expand Down Expand Up @@ -390,7 +391,7 @@ describe("compileAsync method", () => {
})

it("if schema compilation throws some other exception", () => {
ajv.addKeyword("badkeyword", {compile: badCompile})
ajv.addKeyword({keyword: "badkeyword", compile: badCompile})
var schema = {badkeyword: true}
return shouldReject(ajv.compileAsync(schema))

Expand Down
2 changes: 1 addition & 1 deletion spec/async/keyword.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"description": "async custom keywords (validated)",
"description": "async keywords (validated)",
"schema": {
"$async": true,
"properties": {
Expand Down
13 changes: 7 additions & 6 deletions spec/async_schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ instances.forEach(addAsyncFormatsAndKeywords)

jsonSchemaTest(instances, {
description:
"asynchronous schemas tests of " +
instances.length +
" ajv instances with different options",
"asynchronous schemas tests of " + instances.length + " ajv instances with different options",
suites: {
"async schemas":
typeof window == "object"
Expand All @@ -41,21 +39,24 @@ function addAsyncFormatsAndKeywords(ajv) {
validate: checkWordOnServer,
})

ajv.addKeyword("idExists", {
ajv.addKeyword({
keyword: "idExists",
async: true,
type: "number",
validate: checkIdExists,
errors: false,
})

ajv.addKeyword("idExistsWithError", {
ajv.addKeyword({
keyword: "idExistsWithError",
async: true,
type: "number",
validate: checkIdExistsWithError,
errors: true,
})

ajv.addKeyword("idExistsCompiled", {
ajv.addKeyword({
keyword: "idExistsCompiled",
async: true,
type: "number",
compile: compileCheckIdExists,
Expand Down
24 changes: 8 additions & 16 deletions spec/async_validate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ describe("async schemas, formats and keywords", function () {
describe("async custom keywords", () => {
beforeEach(() => {
instances.forEach((_ajv) => {
_ajv.addKeyword("idExists", {
_ajv.addKeyword({
keyword: "idExists",
async: true,
type: "number",
validate: checkIdExists,
errors: false,
})

_ajv.addKeyword("idExistsWithError", {
_ajv.addKeyword({
keyword: "idExistsWithError",
async: true,
type: "number",
validate: checkIdExistsWithError,
Expand Down Expand Up @@ -138,12 +140,8 @@ describe("async schemas, formats and keywords", function () {
var validate = _ajv.compile(schema)

return Promise.all([
shouldBeInvalid(validate({userId: 5, postId: 10}), [
"id not found in table posts",
]),
shouldBeInvalid(validate({userId: 9, postId: 25}), [
"id not found in table users",
]),
shouldBeInvalid(validate({userId: 5, postId: 10}), ["id not found in table posts"]),
shouldBeInvalid(validate({userId: 9, postId: 25}), ["id not found in table users"]),
])
})
)
Expand Down Expand Up @@ -378,16 +376,10 @@ describe("async schemas, formats and keywords", function () {
shouldBeInvalid(validate({foo: {foo: "manana"}})),
shouldBeInvalid(validate({foo: {foo: 1}})),
shouldThrow(validate({foo: {foo: "today"}}), "unknown word"),
shouldBeValid(
validate((data = {foo: {foo: {foo: "tomorrow"}}})),
data
),
shouldBeValid(validate((data = {foo: {foo: {foo: "tomorrow"}}})), data),
shouldBeInvalid(validate({foo: {foo: {foo: "manana"}}})),
shouldBeInvalid(validate({foo: {foo: {foo: 1}}})),
shouldThrow(
validate({foo: {foo: {foo: "today"}}}),
"unknown word"
),
shouldThrow(validate({foo: {foo: {foo: "today"}}}), "unknown word"),
])
})
)
Expand Down
Loading

0 comments on commit e675f8a

Please sign in to comment.