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: v7.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: v7.0.2
Choose a head ref
  • 8 commits
  • 8 files changed
  • 2 contributors

Commits on Dec 17, 2020

  1. README: Fix broken links

    orgads committed Dec 17, 2020
    Copy the full SHA
    5302271 View commit details
  2. Merge pull request #1355 from orgads/readme-links

    README: Fix broken links
    epoberezkin authored Dec 17, 2020
    Copy the full SHA
    bd95783 View commit details

Commits on Dec 18, 2020

  1. Copy the full SHA
    5fe4bc0 View commit details

Commits on Dec 19, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e446893 View commit details
  2. Copy the full SHA
    eae2d5d View commit details
  3. Copy the full SHA
    0b89a00 View commit details
  4. Merge pull request #1362 from ajv-validator/fix-standalone

    Fix for standalone code generation creating duplicate functions
    epoberezkin authored Dec 19, 2020
    Copy the full SHA
    832fee1 View commit details
  5. 7.0.2

    epoberezkin authored Dec 19, 2020
    Copy the full SHA
    5c28d85 View commit details
Showing with 88 additions and 8 deletions.
  1. +1 −1 .github/workflows/build.yml
  2. +1 −1 README.md
  3. +1 −1 lib/compile/codegen/index.ts
  4. +2 −2 lib/compile/index.ts
  5. +4 −0 lib/standalone/index.ts
  6. +2 −2 lib/types/index.ts
  7. +1 −1 package.json
  8. +76 −0 spec/standalone.spec.ts
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ jobs:
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: update website
if: ${{ matrix.node-version == '14.x' }}
if: ${{ github.event_name == 'push' && matrix.node-version == '14.x' }}
run: ./scripts/publish-gh-pages
env:
GH_TOKEN_PUBLIC: ${{ secrets.GH_TOKEN_PUBLIC }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ The fastest JSON Schema validator for Node.js and browser. Supports draft-06/07/

Ajv version 7 is released with these changes:

- support of JSON Schema draft-2019-09 features: [`unevaluatedProperties`](./json-schema.md#unevaluatedproperties) and [`unevaluatedItems`](./json-schema.md#unevaluateditems), [dynamic recursive references](./validation.md#extending-recursive-schemas) and other [additional keywords](./json-schema.md#json-schema-draft-2019-09).
- support of JSON Schema draft-2019-09 features: [`unevaluatedProperties`](./docs/json-schema.md#unevaluatedproperties) and [`unevaluatedItems`](./docs/json-schema.md#unevaluateditems), [dynamic recursive references](./docs/validation.md#extending-recursive-schemas) and other [additional keywords](./docs/json-schema.md#json-schema-draft-2019-09).
- to reduce the mistakes in JSON schemas and unexpected validation results, [strict mode](./docs/strict-mode.md) is added - it prohibits ignored or ambiguous JSON Schema elements.
- to make code injection from untrusted schemas impossible, [code generation](./docs/codegen.md) is fully re-written to be safe and to allow code optimization (compiled schema code size is reduced by more than 10%).
- to simplify Ajv extensions, the new keyword API that is used by pre-defined keywords is available to user-defined keywords - it is much easier to define any keywords now, especially with subschemas. [ajv-keywords](https://github.com/ajv-validator/ajv-keywords) package was updated to use the new API (in [v4.0.0](https://github.com/ajv-validator/ajv-keywords/releases/tag/v4.0.0))
2 changes: 1 addition & 1 deletion lib/compile/codegen/index.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import {_, nil, _Code, Code, Name, UsedNames, CodeItem, addCodeArg, _CodeOrName}
import {Scope, varKinds} from "./scope"

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

// type for expressions that can be safely inserted in code without quotes
export type SafeExpr = Code | number | boolean | null
4 changes: 2 additions & 2 deletions lib/compile/index.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import type {
} from "../types"
import type Ajv from "../core"
import type {InstanceOptions} from "../core"
import {CodeGen, _, nil, stringify, Name, Code} from "./codegen"
import {CodeGen, _, nil, stringify, Name, Code, ValueScopeName} from "./codegen"
import {ValidationError} from "./error_classes"
import N from "./names"
import {LocalRefs, getFullPath, _getFullPath, inlineRef, normalizeId, resolveUrl} from "./resolve"
@@ -77,7 +77,7 @@ export class SchemaEnv implements SchemaEnvArgs {
readonly refs: SchemaRefs = {}
readonly dynamicAnchors: {[Ref in string]?: true} = {}
validate?: AnyValidateFunction
validateName?: Name
validateName?: ValueScopeName

constructor(env: SchemaEnvArgs) {
let schema: AnySchemaObject | undefined
4 changes: 4 additions & 0 deletions lib/standalone/index.ts
Original file line number Diff line number Diff line change
@@ -51,6 +51,10 @@ export default function standaloneCode(

function validateCode(usedValues: ScopeValueSets, s?: SourceCode): Code {
if (!s) throw new Error('moduleCode: function does not have "source" property')
const {prefix} = s.validateName
const nameSet = (usedValues[prefix] = usedValues[prefix] || new Set())
nameSet.add(s.validateName)

const scopeCode = ajv.scope.scopeCode(s.scopeValues, usedValues, refValidateCode)
const code = new _Code(`${scopeCode}${_n}${s.validateCode}`)
return s.evaluated ? _`${code}${s.validateName}.evaluated = ${s.evaluated};${_n}` : code
4 changes: 2 additions & 2 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {CodeGen, Code, Name, ScopeValueSets} from "../compile/codegen"
import type {CodeGen, Code, Name, ScopeValueSets, ValueScopeName} from "../compile/codegen"
import type {SchemaEnv, SchemaCxt, SchemaObjCxt} from "../compile"
import type {JSONType} from "../compile/rules"
import type KeywordCxt from "../compile/context"
@@ -30,7 +30,7 @@ export type AnySchema = Schema | AsyncSchema
export type SchemaMap = {[Key in string]?: AnySchema}

export interface SourceCode {
validateName: Name
validateName: ValueScopeName
validateCode: string
scopeValues: ScopeValueSets
evaluated?: Code
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": "7.0.1",
"version": "7.0.2",
"description": "Another JSON Schema Validator",
"main": "dist/ajv.js",
"types": "dist/ajv.d.ts",
76 changes: 76 additions & 0 deletions spec/standalone.spec.ts
Original file line number Diff line number Diff line change
@@ -85,6 +85,82 @@ describe("standalone code generation", () => {
}
})

describe("two refs to the same schema (issue #1361)", () => {
const userSchema = {
$id: "user.json",
type: "object",
properties: {
name: {type: "string"},
},
required: ["name"],
}

const infoSchema = {
$id: "info.json",
type: "object",
properties: {
author: {$ref: "user.json"},
contributors: {
type: "array",
items: {$ref: "user.json"},
},
},
required: ["author", "contributors"],
}

describe("all exports", () => {
it("should not have duplicate functions", () => {
const ajv = new _Ajv({
allErrors: true,
code: {optimize: false, source: true},
inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
schemas: [userSchema, infoSchema],
})

const moduleCode = standaloneCode(ajv)
assertNoDuplicateFunctions(moduleCode)
const {"user.json": user, "info.json": info} = requireFromString(moduleCode)
testExports({user, info})
})
})

describe("named exports", () => {
it("should not have duplicate functions", () => {
const ajv = new _Ajv({
allErrors: true,
code: {optimize: false, source: true},
inlineRefs: false, // it is needed to show the issue, schemas with refs won't be inlined anyway
schemas: [userSchema, infoSchema],
})

const moduleCode = standaloneCode(ajv, {user: "user.json", info: "info.json"})
assertNoDuplicateFunctions(moduleCode)
testExports(requireFromString(moduleCode))
})
})

function assertNoDuplicateFunctions(code: string): void {
const funcs = code.match(/function\s+([a-z0-9_$]+)/gi)
assert(Array.isArray(funcs))
assert(funcs.length > 0)
assert.strictEqual(funcs.length, new Set(funcs).size, "should have no duplicates")
}

function testExports(validate: {[n: string]: AnyValidateFunction<unknown>}): void {
assert.strictEqual(validate.user({}), false)
assert.strictEqual(validate.user({name: "usr1"}), true)

assert.strictEqual(validate.info({}), false)
assert.strictEqual(
validate.info({
author: {name: "usr1"},
contributors: [{name: "usr2"}],
}),
true
)
}
})

it("should generate module code with a single export (ESM compatible)", () => {
const ajv = new _Ajv({code: {source: true}})
const v = ajv.compile({