Skip to content

Commit

Permalink
loopEnum: test, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 11, 2020
1 parent 36f6177 commit d36783c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ Defaults:
inlineRefs: true,
passContext: false,
loopRequired: Infinity,
loopEnum: Infinity,
ownProperties: false,
multipleOfPrecision: false,
errorDataPath: 'object', // deprecated
Expand Down Expand Up @@ -1237,6 +1238,7 @@ Defaults:
- integer number - to limit the maximum number of keywords of the schema that will be inlined.
- _passContext_: pass validation context to custom keyword functions. If this option is `true` and you pass some context to the compiled validation function with `validate.call(context, data)`, the `context` will be available as `this` in your custom keywords. By default `this` is Ajv instance.
- _loopRequired_: by default `required` keyword is compiled into a single expression (or a sequence of statements in `allErrors` mode). In case of a very large number of properties in this keyword it may result in a very big validation function. Pass integer to set the number of properties above which `required` keyword will be validated in a loop - smaller validation function size but also worse performance.
- _loopEnum_: by default `enum` keyword is compiled into a single expression. In case of a very large number of allowed values it may result in a large validation function. Pass integer to set the number of values above which `enum` keyword will be validated in a loop.
- _ownProperties_: by default Ajv iterates over all enumerable object properties; when this option is `true` only own enumerable object properties (i.e. found directly on the object rather than on its prototype) are iterated. Contributed by @mbroadst.
- _multipleOfPrecision_: by default `multipleOf` keyword is validated by comparing the result of division with parseInt() of that result. It works for dividers that are bigger than 1. For small dividers such as 0.01 the result of the division is usually not integer (even when it should be integer, see issue [#84](https://github.com/ajv-validator/ajv/issues/84)). If you need to use fractional dividers set this option to some positive integer N to have `multipleOf` validated using this formula: `Math.abs(Math.round(division) - division) < 1e-N` (it is slower but allows for float arithmetics deviations).
- _errorDataPath_ (deprecated): set `dataPath` to point to 'object' (default) or to 'property' when validating keywords `required`, `additionalProperties` and `dependencies`.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"bundle": "del-cli dist && node ./scripts/bundle.js . Ajv pure_getters",
"bundle-beautify": "node ./scripts/bundle.js js-beautify",
"dot": "del-cli lib/dotjs/*.js \"!lib/dotjs/index.js\" && node scripts/compile-dots.js",
"tsc": "tsc || true && cp -r lib/refs dist/refs",
"tsc": "tsc && cp -r lib/refs dist/refs",
"build": "npm run dot && npm run tsc",
"test-karma": "karma start",
"test-browser": "del-cli .browser && npm run bundle && scripts/prepare-tests && npm run test-karma",
Expand Down
17 changes: 17 additions & 0 deletions spec/options/options_code.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,21 @@ describe("code generation options", function () {
return storeContext
}
})

describe("loopEnum option", () => {
it("should use loop if more values than specified", () => {
const ajv1 = new Ajv()
const ajv2 = new Ajv({loopEnum: 2})
test(ajv1, {enum: ["foo", "bar"]})
test(ajv2, {enum: ["foo", "bar"]})
test(ajv1, {enum: ["foo", "bar", "baz"]})
test(ajv2, {enum: ["foo", "bar", "baz"]})

function test(ajv, schema) {
ajv.validate(schema, "foo").should.equal(true)
ajv.validate(schema, "boo").should.equal(false)
ajv.validate(schema, 1).should.equal(false)
}
})
})
})

0 comments on commit d36783c

Please sign in to comment.