Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ajv-validator/ajv
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v8.0.1
Choose a base ref
...
head repository: ajv-validator/ajv
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v8.0.2
Choose a head ref
  • 13 commits
  • 11 files changed
  • 4 contributors

Commits on Mar 27, 2021

  1. Copy the full SHA
    2a562e0 View commit details

Commits on Mar 28, 2021

  1. Unverified

    The email in this signature doesn’t match the committer email.
    Copy the full SHA
    f316bd8 View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    38c5b93 View commit details
  3. readme: changes history

    epoberezkin committed Mar 28, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    991124a View commit details
  4. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    ea08b32 View commit details
  5. Copy the full SHA
    60ebfb2 View commit details
  6. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f3d29b4 View commit details

Commits on Mar 29, 2021

  1. Copy the full SHA
    cbd2718 View commit details
  2. Merge pull request #1519 from asaid-0/fix-1509

    Add error path to strict mode tuple warning
    epoberezkin authored Mar 29, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    cf8d31f View commit details

Commits on Mar 30, 2021

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    676dbb1 View commit details
  2. Merge pull request #1524 from papb/patch-1

    docs(json-type-definition): fix missing bullet
    epoberezkin authored Mar 30, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    e62d034 View commit details

Commits on Mar 31, 2021

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    ec1818b View commit details
  2. 8.0.2

    epoberezkin committed Mar 31, 2021
    Copy the full SHA
    24dba5c View commit details
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ Performance of different validators by [json-schema-benchmark](https://github.co

## Install

To install version 7:
To install version 8:

```
npm install ajv
@@ -154,7 +154,9 @@ Learn how to use Ajv and see more examples in the [Guide: getting started](https

See [https://github.com/ajv-validator/ajv/releases](https://github.com/ajv-validator/ajv/releases)

**Please note**: [Changes in version 7.0.0](https://github.com/ajv-validator/ajv/releases/tag/v7.0.0)
**Please note**: [Changes in version 8.0.0](https://github.com/ajv-validator/ajv/releases/tag/v8.0.0)

[Version 7.0.0](https://github.com/ajv-validator/ajv/releases/tag/v7.0.0)

[Version 6.0.0](https://github.com/ajv-validator/ajv/releases/tag/v6.0.0).

7 changes: 5 additions & 2 deletions docs/.vuepress/components/HomeSection.vue
Original file line number Diff line number Diff line change
@@ -31,8 +31,11 @@ export default {
@media only screen and (max-width: $MQMobileNarrow)
padding 0 30px
& >>> .theme-code-block
margin-top 12px
& >>> .theme-code-group
button
position relative
outline none
z-index 1
&.testimonials
background linear-gradient(295.26deg, rgba(232, 232, 232, 0.8) 26.65%, rgba(255, 255, 255, 0.04) 83.48%)
9 changes: 8 additions & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
@@ -86,6 +86,7 @@ module.exports = {
items: [
{link: "/news/", text: "News"},
{link: "/security", text: "Security"},
{link: "/v6-to-v8-migration", text: "Migrate from v6"},
{link: "/faq", text: "FAQ"},
{link: "/license", text: "License"},
],
@@ -145,7 +146,13 @@ module.exports = {
},
{
title: "Information",
children: ["/faq", "/security", ["/license", "License"]],
children: [
"/news/",
"/faq",
"/security",
["/v6-to-v8-migration", "Migrate from v6 to v8"],
["/license", "License"],
],
},
],
repo: "ajv-validator/ajv",
2 changes: 1 addition & 1 deletion docs/json-type-definition.md
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ It has a required member `type` and an optional members `nullable` and `metadata
- `"uint16"` - unsigned word value (0 .. 65535)
- `"int32"` - signed 32-bit integer value
- `"uint32"` - unsigned 32-bit integer value
`type` values that define floating point numbers:
- `type` values that define floating point numbers:
- `"float32"` - 32-bit real number
- `"float64"` - 64-bit real number

4 changes: 4 additions & 0 deletions lib/compile/codegen/code.ts
Original file line number Diff line number Diff line change
@@ -154,3 +154,7 @@ export function safeStringify(x: unknown): string {
export function getProperty(key: Code | string | number): Code {
return typeof key == "string" && IDENTIFIER.test(key) ? new _Code(`.${key}`) : _`[${key}]`
}

export function regexpCode(rx: RegExp): Code {
return new _Code(rx.toString())
}
2 changes: 1 addition & 1 deletion lib/compile/codegen/index.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import type {ScopeValueSets, NameValue, ValueScope, ValueScopeName} from "./scop
import {_, nil, _Code, Code, Name, UsedNames, CodeItem, addCodeArg, _CodeOrName} from "./code"
import {Scope, varKinds} from "./scope"

export {_, str, strConcat, nil, getProperty, stringify, Name, Code} from "./code"
export {_, str, strConcat, nil, getProperty, stringify, regexpCode, Name, Code} from "./code"
export {Scope, ScopeStore, ValueScope, ValueScopeName, ScopeValueSets, varKinds} from "./scope"

// type for expressions that can be safely inserted in code without quotes
7 changes: 4 additions & 3 deletions lib/vocabularies/applicator/items.ts
Original file line number Diff line number Diff line change
@@ -46,11 +46,12 @@ export function validateTuple(
})

function checkStrictTuple(sch: AnySchemaObject): void {
const {opts, errSchemaPath} = it
const l = schArr.length
const fullTuple = l === sch.minItems && (l === sch.maxItems || sch[extraItems] === false)
if (it.opts.strictTuples && !fullTuple) {
const msg = `"${keyword}" is ${l}-tuple, but minItems or maxItems/${extraItems} are not specified or different`
checkStrictMode(it, msg, it.opts.strictTuples)
if (opts.strictTuples && !fullTuple) {
const msg = `"${keyword}" is ${l}-tuple, but minItems or maxItems/${extraItems} are not specified or different at path "${errSchemaPath}"`
checkStrictMode(it, msg, opts.strictTuples)
}
}
}
14 changes: 8 additions & 6 deletions lib/vocabularies/format/format.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import type {
ErrorObject,
} from "../../types"
import type {KeywordCxt} from "../../compile/validate"
import {_, str, nil, or, Code, getProperty} from "../../compile/codegen"
import {_, str, nil, or, Code, getProperty, regexpCode} from "../../compile/codegen"

type FormatValidate =
| FormatValidator<string>
@@ -92,11 +92,13 @@ const def: CodeKeywordDefinition = {
}

function getFormat(fmtDef: AddedFormat): [string, FormatValidate, Code] {
const fmt = gen.scopeValue("formats", {
key: schema,
ref: fmtDef,
code: opts.code.formats ? _`${opts.code.formats}${getProperty(schema)}` : undefined,
})
const code =
fmtDef instanceof RegExp
? regexpCode(fmtDef)
: opts.code.formats
? _`${opts.code.formats}${getProperty(schema)}`
: undefined
const fmt = gen.scopeValue("formats", {key: schema, ref: fmtDef, code})
if (typeof fmtDef == "object" && !(fmtDef instanceof RegExp)) {
return [fmtDef.type || "string", fmtDef.validate, _`${fmt}.validate`]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "8.0.1",
"version": "8.0.2",
"description": "Another JSON Schema Validator",
"main": "dist/ajv.js",
"types": "dist/ajv.d.ts",
41 changes: 28 additions & 13 deletions spec/options/strict.spec.ts
Original file line number Diff line number Diff line change
@@ -208,34 +208,49 @@ describe("strict mode", () => {

//@ts-expect-error
const badSchema1: JSONSchemaType<MyTuple> = {
type: "array",
items: [{type: "string"}, {type: "number"}],
additionalItems: false,
type: "object",
properties: {
test: {
type: "array",
items: [{type: "string"}, {type: "number"}],
additionalItems: false,
},
},
}
should.throw(() => {
ajv.compile(badSchema1)
}, / minItems or maxItems\/additionalItems are not specified or different/)
}, / minItems or maxItems\/additionalItems are not specified or different at path "#\/properties\/test"/)

//@ts-expect-error
const badSchema2: JSONSchemaType<MyTuple> = {
type: "array",
items: [{type: "string"}, {type: "number"}],
minItems: 2,
type: "object",
properties: {
test: {
type: "array",
items: [{type: "string"}, {type: "number"}],
minItems: 2,
},
},
}
should.throw(() => {
ajv.compile(badSchema2)
}, / minItems or maxItems\/additionalItems are not specified or different/)
}, / minItems or maxItems\/additionalItems are not specified or different at path "#\/properties\/test"/)

//@ts-expect-error
const badSchema3: JSONSchemaType<MyTuple> = {
type: "array",
items: [{type: "string"}, {type: "number"}],
minItems: 2,
maxItems: 3,
type: "object",
properties: {
test: {
type: "array",
items: [{type: "string"}, {type: "number"}],
minItems: 2,
maxItems: 3,
},
},
}
should.throw(() => {
ajv.compile(badSchema3)
}, / minItems or maxItems\/additionalItems are not specified or different/)
}, / minItems or maxItems\/additionalItems are not specified or different at path "#\/properties\/test"/)
})
})

32 changes: 32 additions & 0 deletions spec/standalone.spec.ts
Original file line number Diff line number Diff line change
@@ -273,4 +273,36 @@ describe("standalone code generation", () => {
assert.strictEqual(validateUser({email: "foo@bar.com"}), true)
})
})

describe("standalone code with RegExp format", () => {
const schema = {
$schema: "http://json-schema.org/draft-07/schema#",
definitions: {
User: {
type: "object",
properties: {
username: {
type: "string",
format: "username",
},
},
required: ["username"],
additionalProperties: false,
},
},
}

it("should support RegExp format with standalone code", () => {
const ajv = new _Ajv({code: {source: true}})
ajv.addFormat("username", /[a-z][a-z0-9_]*/i)
ajv.addSchema(schema)
const moduleCode = standaloneCode(ajv, {validateUser: "#/definitions/User"})
const {validateUser} = requireFromString(moduleCode)

assert(typeof validateUser == "function")
assert.strictEqual(validateUser({}), false)
assert.strictEqual(validateUser({username: "foo_bar"}), true)
assert.strictEqual(validateUser({email: "foo bar"}), false)
})
})
})