Skip to content

Commit

Permalink
feat: remove deprecated option useDefaults: shared
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 29, 2020
1 parent cd2aeb7 commit f918e51
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 54 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,6 @@ Defaults:
- `false` (default) - do not use defaults
- `true` - insert defaults by value (object literal is used).
- `"empty"` - in addition to missing or undefined, use defaults for properties and items that are equal to `null` or `""` (an empty string).
- `"shared"` (deprecated) - insert defaults by reference. If the default is an object, it will be shared by all instances of validated data. If you modify the inserted default in the validated data, it will be modified in the schema as well.
- _coerceTypes_: change data type of data to match `type` keyword. See the example in [Coercing data types](#coercing-data-types) and [coercion rules](https://github.com/ajv-validator/ajv/blob/master/COERCION.md). Option values:
- `false` (default) - no type coercion.
- `true` - coerce scalar data types.
Expand Down
2 changes: 1 addition & 1 deletion lib/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default function Ajv(opts: Options): void {
this._compilations = []
this.RULES = rules()
if (opts.schemaId !== undefined && opts.schemaId !== "$id") {
throw new Error("option schemaId is not supported from v7")
throw new Error("option schemaId is not supported in v7")
}

opts.loopRequired = opts.loopRequired || Infinity
Expand Down
27 changes: 2 additions & 25 deletions lib/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import N from "./names"
const equal = require("fast-deep-equal")
const ucs2length = require("./ucs2length")

var resolve = require("./resolve"),
stableStringify = require("fast-json-stable-stringify")
const resolve = require("./resolve")

/**
* Functions below are used inside compiled validations function
Expand Down Expand Up @@ -51,7 +50,6 @@ function compile(schema, root, localRefs, baseId) {
patterns: string[] = [],
patternsHash = {},
defaults: any[] = [],
defaultsHash = {},
customRules: KeywordCompilationResult[] = []

root = root || {schema: schema, refVal: refVal, refs: refs}
Expand Down Expand Up @@ -129,8 +127,7 @@ function compile(schema, root, localRefs, baseId) {
RULES, // TODO refactor - it is available on the instance
resolveRef, // TODO remove to imports
usePattern, // TODO remove to imports
useDefault, // TODO remove to imports
customRules, // TODO add to types
customRules,
opts,
formats,
logger: self.logger,
Expand Down Expand Up @@ -264,26 +261,6 @@ function compile(schema, root, localRefs, baseId) {
}
return "pattern" + index
}

function useDefault(value: any): Expression {
switch (typeof value) {
case "boolean":
case "number":
case "string":
return _`${value}`
case "object":
if (value === null) return "null"
var valueStr = stableStringify(value)
var index = defaultsHash[valueStr]
if (index === undefined) {
index = defaultsHash[valueStr] = defaults.length
defaults[index] = value
}
return "default" + index
default:
throw new Error(`unsupported default type "${typeof value}"`)
}
}
}

/**
Expand Down
6 changes: 2 additions & 4 deletions lib/compile/validate/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function assignDefaults(it: CompilationContext, ty?: string): void {
}

function assignDefault(
{gen, compositeRule, data, useDefault, opts, logger}: CompilationContext,
{gen, compositeRule, data, opts, logger}: CompilationContext,
prop: string | number,
defaultValue: any
): void {
Expand All @@ -31,7 +31,5 @@ function assignDefault(
const condition =
`${childData} === undefined` +
(opts.useDefaults === "empty" ? ` || ${childData} === null || ${childData} === ""` : "")
// TODO remove option `useDefaults === "shared"`
const defaultExpr = opts.useDefaults === "shared" ? useDefault : JSON.stringify
gen.if(condition, `${childData} = ${defaultExpr(defaultValue)}`)
gen.if(condition, `${childData} = ${JSON.stringify(defaultValue)}`)
}
3 changes: 1 addition & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface Options {
cb?: (err: Error, schema: object) => void
) => PromiseLike<object | boolean>
removeAdditional?: boolean | "all" | "failing"
useDefaults?: boolean | "empty" | "shared"
useDefaults?: boolean | "empty"
coerceTypes?: boolean | "array"
strictDefaults?: boolean | "log"
strictKeywords?: boolean | "log"
Expand Down Expand Up @@ -130,7 +130,6 @@ export interface CompilationContext {
// }
compositeRule?: boolean
usePattern: (str: string) => string
useDefault: (value: any) => Expression
customRules: KeywordCompilationResult[]
self: any // TODO
RULES: ValidationRules
Expand Down
29 changes: 8 additions & 21 deletions spec/options/useDefaults.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,15 @@ describe("useDefaults option", () => {
}
})

describe("useDefaults: by value / by reference", () => {
describe("using by value", () => {
it("should NOT modify underlying defaults when modifying validated data", () => {
test("value", new Ajv({useDefaults: true}))
test("value", new Ajv({useDefaults: true, allErrors: true}))
})
})

describe("using by reference", () => {
it("should modify underlying defaults when modifying validated data", () => {
test("reference", new Ajv({useDefaults: "shared"}))
test("reference", new Ajv({useDefaults: "shared", allErrors: true}))
})
describe("useDefaults: defaults are always passed by value", () => {
it("should NOT modify underlying defaults when modifying validated data", () => {
test(new Ajv({useDefaults: true}))
test(new Ajv({useDefaults: true, allErrors: true}))
test(new Ajv({useDefaults: "shared"}))
test(new Ajv({useDefaults: "shared", allErrors: true}))
})

function test(useDefaultsMode, ajv) {
function test(ajv) {
var schema = {
properties: {
items: {
Expand All @@ -155,13 +148,7 @@ describe("useDefaults option", () => {
var data2 = {}
validate(data2).should.equal(true)

if (useDefaultsMode === "reference") {
data2.items.should.eql(["a-default", "another-value"])
} else if (useDefaultsMode === "value") {
data2.items.should.eql(["a-default"])
} else {
throw new Error("unknown useDefaults mode")
}
data2.items.should.eql(["a-default"])
}
})

Expand Down

0 comments on commit f918e51

Please sign in to comment.