Skip to content

Commit

Permalink
refactor: import keyword definiions (rather than require)
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Sep 16, 2020
1 parent c5c1b84 commit 61e7520
Show file tree
Hide file tree
Showing 32 changed files with 226 additions and 167 deletions.
1 change: 1 addition & 0 deletions lib/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
FormatDefinition,
AsyncFormatDefinition,
KeywordDefinition,
KeywordErrorDefinition,
CodeKeywordDefinition,
MacroKeywordDefinition,
FuncKeywordDefinition,
Expand Down
8 changes: 4 additions & 4 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ export interface AsyncValidateFunction<T = any> extends ValidateFunction<T> {

export type AnyValidateFunction<T = any> = ValidateFunction<T> | AsyncValidateFunction<T>

export interface ErrorObject {
keyword: string
export interface ErrorObject<T = string> {
keyword: T
dataPath: string
schemaPath: string
params: Record<string, unknown> // TODO add interface
// Added to validation errors of propertyNames keyword schema
// Added to validation errors of "propertyNames" keyword schema
propertyName?: string
// Excluded if messages set to false.
// Excluded if option `messages` set to false.
message?: string
// These are added with the `verbose` option.
schema?: unknown
Expand Down
14 changes: 8 additions & 6 deletions lib/vocabularies/applicator/additionalItems.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import type {CodeKeywordDefinition} from "../../types"
import type {CodeKeywordDefinition, KeywordErrorDefinition} from "../../types"
import type KeywordCxt from "../../compile/context"
import {alwaysValidSchema, checkStrictMode} from "../util"
import {applySubschema, Type} from "../../compile/subschema"
import {_, Name, str} from "../../compile/codegen"

const error: KeywordErrorDefinition = {
message: ({params: {len}}) => str`should NOT have more than ${len} items`,
params: ({params: {len}}) => _`{limit: ${len}}`,
}

const def: CodeKeywordDefinition = {
keyword: "additionalItems",
type: "array",
schemaType: ["boolean", "object"],
before: "uniqueItems",
error,
code(cxt: KeywordCxt) {
const {gen, schema, parentSchema, data, it} = cxt
const len = gen.const("len", _`${data}.length`)
Expand All @@ -33,10 +39,6 @@ const def: CodeKeywordDefinition = {
})
}
},
error: {
message: ({params: {len}}) => str`should NOT have more than ${len} items`,
params: ({params: {len}}) => _`{limit: ${len}}`,
},
}

module.exports = def
export default def
15 changes: 7 additions & 8 deletions lib/vocabularies/applicator/additionalProperties.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import type {CodeKeywordDefinition, KeywordErrorCxt} from "../../types"
import type {CodeKeywordDefinition, KeywordErrorDefinition} from "../../types"
import {allSchemaProperties, schemaRefOrVal, alwaysValidSchema, usePattern} from "../util"
import {applySubschema, SubschemaApplication, Type} from "../../compile/subschema"
import {_, nil, or, Code, Name} from "../../compile/codegen"
import N from "../../compile/names"

const error: KeywordErrorDefinition = {
message: "should NOT have additional properties",
params: ({params}) => _`{additionalProperty: ${params.additionalProperty}}`,
}

const def: CodeKeywordDefinition = {
keyword: "additionalProperties",
type: "object",
schemaType: ["boolean", "object", "undefined"], // "undefined" is needed to support option removeAdditional: "all"
trackErrors: true,
error,
code(cxt) {
const {gen, schema, parentSchema, data, errsCount, it} = cxt
if (!errsCount) throw new Error("ajv implementation error")
Expand Down Expand Up @@ -91,13 +97,6 @@ const def: CodeKeywordDefinition = {
applySubschema(it, subschema, valid)
}
},
error: {
message: "should NOT have additional properties",
params: ({params}: KeywordErrorCxt): Code =>
_`{additionalProperty: ${params.additionalProperty}}`,
},
}

module.exports = def

export default def
2 changes: 1 addition & 1 deletion lib/vocabularies/applicator/allOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ const def: CodeKeywordDefinition = {
},
}

module.exports = def
export default def
2 changes: 1 addition & 1 deletion lib/vocabularies/applicator/anyOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ const def: CodeKeywordDefinition = {
},
}

module.exports = def
export default def
2 changes: 1 addition & 1 deletion lib/vocabularies/applicator/contains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ const def: CodeKeywordDefinition = {
},
}

module.exports = def
export default def
28 changes: 15 additions & 13 deletions lib/vocabularies/applicator/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {CodeKeywordDefinition, SchemaMap, AnySchema} from "../../types"
import type {CodeKeywordDefinition, KeywordErrorDefinition, SchemaMap, AnySchema} from "../../types"
import type KeywordCxt from "../../compile/context"
import {alwaysValidSchema, propertyInData} from "../util"
import {applySubschema} from "../../compile/subschema"
Expand All @@ -11,10 +11,23 @@ interface PropertyDependencies {

type SchemaDependencies = SchemaMap

const error: KeywordErrorDefinition = {
message: ({params: {property, depsCount, deps}}) => {
const property_ies = depsCount === 1 ? "property" : "properties"
return str`should have ${property_ies} ${deps} when property ${property} is present`
},
params: ({params: {property, depsCount, deps, missingProperty}}) =>
_`{property: ${property},
missingProperty: ${missingProperty},
depsCount: ${depsCount},
deps: ${deps}}`, // TODO change to reference?
}

const def: CodeKeywordDefinition = {
keyword: "dependencies",
type: "object",
schemaType: "object",
error,
code(cxt: KeywordCxt) {
const {gen, schema, data, it} = cxt
const [propDeps, schDeps] = splitDependencies()
Expand Down Expand Up @@ -71,17 +84,6 @@ const def: CodeKeywordDefinition = {
}
}
},
error: {
message: ({params: {property, depsCount, deps}}) => {
const property_ies = depsCount === 1 ? "property" : "properties"
return str`should have ${property_ies} ${deps} when property ${property} is present`
},
params: ({params: {property, depsCount, deps, missingProperty}}) =>
_`{property: ${property},
missingProperty: ${missingProperty},
depsCount: ${depsCount},
deps: ${deps}}`, // TODO change to reference?
},
}

module.exports = def
export default def
16 changes: 9 additions & 7 deletions lib/vocabularies/applicator/if.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import type {CodeKeywordDefinition, SchemaObjCxt} from "../../types"
import type {CodeKeywordDefinition, KeywordErrorDefinition, SchemaObjCxt} from "../../types"
import type KeywordCxt from "../../compile/context"
import {alwaysValidSchema, checkStrictMode} from "../util"
import {applySubschema} from "../../compile/subschema"
import {_, str, Name} from "../../compile/codegen"

const error: KeywordErrorDefinition = {
message: ({params}) => str`should match "${params.ifClause}" schema`,
params: ({params}) => _`{failingKeyword: ${params.ifClause}}`,
}

const def: CodeKeywordDefinition = {
keyword: "if",
schemaType: ["object", "boolean"],
trackErrors: true,
error,
code(cxt: KeywordCxt) {
const {gen, parentSchema, it} = cxt
if (parentSchema.then === undefined && parentSchema.else === undefined) {
Expand Down Expand Up @@ -56,15 +62,11 @@ const def: CodeKeywordDefinition = {
}
}
},
error: {
message: ({params}) => str`should match "${params.ifClause}" schema`,
params: ({params}) => _`{failingKeyword: ${params.ifClause}}`,
},
}

module.exports = def

function hasSchema(it: SchemaObjCxt, keyword: string): boolean {
const schema = it.schema[keyword]
return schema !== undefined && !alwaysValidSchema(it, schema)
}

export default def
42 changes: 28 additions & 14 deletions lib/vocabularies/applicator/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import type {Vocabulary} from "../../types"
import additionalItems from "./additionalItems"
import items from "./items"
import contains from "./contains"
import dependencies from "./dependencies"
import propertyNames from "./propertyNames"
import additionalProperties from "./additionalProperties"
import properties from "./properties"
import patternProperties from "./patternProperties"
import notKeyword from "./not"
import anyOf from "./anyOf"
import oneOf from "./oneOf"
import allOf from "./allOf"
import ifKeyword from "./if"
import thenElse from "./thenElse"

const applicator: Vocabulary = [
// array
require("./additionalItems"),
require("./items"),
require("./contains"),
additionalItems,
items,
contains,
// object
require("./dependencies"),
require("./propertyNames"),
require("./additionalProperties"),
require("./properties"),
require("./patternProperties"),
dependencies,
propertyNames,
additionalProperties,
properties,
patternProperties,
// any
require("./not"),
require("./anyOf"),
require("./oneOf"),
require("./allOf"),
require("./if"),
require("./thenElse"),
notKeyword,
anyOf,
oneOf,
allOf,
ifKeyword,
thenElse,
]

export default applicator
2 changes: 1 addition & 1 deletion lib/vocabularies/applicator/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ const def: CodeKeywordDefinition = {
},
}

module.exports = def
export default def
2 changes: 1 addition & 1 deletion lib/vocabularies/applicator/not.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ const def: CodeKeywordDefinition = {
},
}

module.exports = def
export default def
14 changes: 8 additions & 6 deletions lib/vocabularies/applicator/oneOf.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import type {CodeKeywordDefinition, AnySchema} from "../../types"
import type {CodeKeywordDefinition, KeywordErrorDefinition, AnySchema} from "../../types"
import type KeywordCxt from "../../compile/context"
import {alwaysValidSchema} from "../util"
import {applySubschema} from "../../compile/subschema"
import {_} from "../../compile/codegen"

const error: KeywordErrorDefinition = {
message: "should match exactly one schema in oneOf",
params: ({params}) => _`{passingSchemas: ${params.passing}}`,
}

const def: CodeKeywordDefinition = {
keyword: "oneOf",
schemaType: "array",
trackErrors: true,
error,
code(cxt: KeywordCxt) {
const {gen, schema, it} = cxt
if (!Array.isArray(schema)) throw new Error("ajv implementation error")
Expand Down Expand Up @@ -54,10 +60,6 @@ const def: CodeKeywordDefinition = {
})
}
},
error: {
message: "should match exactly one schema in oneOf",
params: ({params}) => _`{passingSchemas: ${params.passing}}`,
},
}

module.exports = def
export default def
2 changes: 1 addition & 1 deletion lib/vocabularies/applicator/patternProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ const def: CodeKeywordDefinition = {
},
}

module.exports = def
export default def
2 changes: 1 addition & 1 deletion lib/vocabularies/applicator/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ const def: CodeKeywordDefinition = {
},
}

module.exports = def
export default def
14 changes: 8 additions & 6 deletions lib/vocabularies/applicator/propertyNames.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import type {CodeKeywordDefinition} from "../../types"
import type {CodeKeywordDefinition, KeywordErrorDefinition} from "../../types"
import type KeywordCxt from "../../compile/context"
import {alwaysValidSchema} from "../util"
import {applySubschema} from "../../compile/subschema"
import {_, str} from "../../compile/codegen"

const error: KeywordErrorDefinition = {
message: ({params}) => str`property name '${params.propertyName}' is invalid`, // TODO double quotes?
params: ({params}) => _`{propertyName: ${params.propertyName}}`,
}

const def: CodeKeywordDefinition = {
keyword: "propertyNames",
type: "object",
schemaType: ["object", "boolean"],
error,
code(cxt: KeywordCxt) {
const {gen, schema, data, it} = cxt
if (alwaysValidSchema(it, schema)) return
Expand All @@ -28,10 +34,6 @@ const def: CodeKeywordDefinition = {

cxt.ok(valid)
},
error: {
message: ({params}) => str`property name '${params.propertyName}' is invalid`, // TODO double quotes?
params: ({params}) => _`{propertyName: ${params.propertyName}}`,
},
}

module.exports = def
export default def
2 changes: 1 addition & 1 deletion lib/vocabularies/applicator/thenElse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ const def: CodeKeywordDefinition = {
},
}

module.exports = def
export default def
3 changes: 2 additions & 1 deletion lib/vocabularies/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {Vocabulary} from "../../types"
import refKeyword from "./ref"

const core: Vocabulary = ["$schema", "$id", "$defs", "definitions", require("./ref")]
const core: Vocabulary = ["$schema", "$id", "$defs", "definitions", refKeyword]

export default core
15 changes: 8 additions & 7 deletions lib/vocabularies/core/ref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {CodeKeywordDefinition, AnySchema} from "../../types"
import type {CodeKeywordDefinition, KeywordErrorDefinition, AnySchema} from "../../types"
import type KeywordCxt from "../../compile/context"
import {MissingRefError} from "../../compile/error_classes"
import {applySubschema} from "../../compile/subschema"
Expand All @@ -7,9 +7,15 @@ import {_, str, nil, Code, Name} from "../../compile/codegen"
import N from "../../compile/names"
import {SchemaEnv, resolveRef} from "../../compile"

const error: KeywordErrorDefinition = {
message: ({schema}) => str`can't resolve reference ${schema}`,
params: ({schema}) => _`{ref: ${schema}}`,
}

const def: CodeKeywordDefinition = {
keyword: "$ref",
schemaType: "string",
error,
code(cxt: KeywordCxt) {
const {gen, schema, it} = cxt
const {allErrors, baseId, schemaEnv: env, opts, validateName, self} = it
Expand Down Expand Up @@ -101,11 +107,6 @@ const def: CodeKeywordDefinition = {
gen.assign(N.errors, _`${N.vErrors}.length`)
}
},
// TODO incorrect error message
error: {
message: ({schema}) => str`can't resolve reference ${schema}`,
params: ({schema}) => _`{ref: ${schema}}`,
},
}

module.exports = def
export default def
Loading

0 comments on commit 61e7520

Please sign in to comment.