Socket
Socket
Sign inDemoInstall

ajv

Package Overview
Dependencies
5
Maintainers
2
Versions
351
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 7.0.0-rc.1 to 7.0.0-rc.2

docs/components.md

6

dist/compile/index.d.ts

@@ -29,5 +29,2 @@ import type { AnySchema, AnySchemaObject, AnyValidateFunction, EvaluatedProperties, EvaluatedItems } from "../types";

baseId: string;
dynamicAnchors: {
[Ref in string]?: true;
};
readonly schemaPath: Code;

@@ -62,2 +59,5 @@ readonly errSchemaPath: string;

readonly refs: SchemaRefs;
readonly dynamicAnchors: {
[Ref in string]?: true;
};
validate?: AnyValidateFunction;

@@ -64,0 +64,0 @@ validateName?: Name;

@@ -15,2 +15,3 @@ "use strict";

this.refs = {};
this.dynamicAnchors = {};
let schema;

@@ -70,3 +71,2 @@ if (typeof env.schema == "object")

baseId: sch.baseId || rootId,
dynamicAnchors: {},
schemaPath: codegen_1.nil,

@@ -73,0 +73,0 @@ errSchemaPath: "#",

@@ -53,3 +53,3 @@ "use strict";

}
else if (n.prefix === "root" && typeof vRef == "object") {
else if ((n.prefix === "root" || n.prefix === "wrapper") && typeof vRef == "object") {
const { validate, validateName } = vRef;

@@ -56,0 +56,0 @@ const vCode = validateCode(usedValues, validate === null || validate === void 0 ? void 0 : validate.source);

@@ -6,4 +6,4 @@ import Ajv, { AnySchema, AnyValidateFunction, ErrorObject } from "../core";

constructor(ajv: Ajv);
validate<T = unknown>(schemaKeyRef: AnySchema | string, data: unknown | T): boolean | Promise<T>;
compile<T = unknown>(schema: AnySchema, _meta?: boolean): AnyValidateFunction<T>;
validate(schemaKeyRef: AnySchema | string, data: unknown): boolean | Promise<unknown>;
compile<T = unknown>(schema: AnySchema, meta?: boolean): AnyValidateFunction<T>;
getSchema<T = unknown>(keyRef: string): AnyValidateFunction<T> | undefined;

@@ -10,0 +10,0 @@ private getStandalone;

@@ -13,4 +13,4 @@ "use strict";

}
compile(schema, _meta) {
return this.getStandalone(this.ajv.compile(schema));
compile(schema, meta) {
return this.getStandalone(this.ajv.compile(schema, meta));
}

@@ -17,0 +17,0 @@ getSchema(keyRef) {

@@ -6,3 +6,4 @@ import type { CodeKeywordDefinition } from "../../types";

declare const def: CodeKeywordDefinition;
export declare function getValidate(cxt: KeywordCxt, sch: SchemaEnv): Code;
export declare function callRef(cxt: KeywordCxt, v: Code, sch?: SchemaEnv, $async?: boolean): void;
export default def;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.callRef = void 0;
exports.callRef = exports.getValidate = void 0;
const error_classes_1 = require("../../compile/error_classes");

@@ -16,2 +16,4 @@ const code_1 = require("../code");

const { baseId, schemaEnv: env, validateName, opts, self } = it;
// TODO See comment in dynamicRef.ts
// This has to be improved to resolve #815.
if (schema === "#" || schema === "#/")

@@ -32,11 +34,3 @@ return callRootRef();

function callValidate(sch) {
let v;
if (sch.validate) {
v = gen.scopeValue("validate", { ref: sch.validate });
}
else {
const code = codegen_1._ `{validate: ${sch.validateName}}`;
const wrapper = gen.scopeValue("wrapper", { ref: sch, code });
v = codegen_1._ `${wrapper}.validate`;
}
const v = getValidate(cxt, sch);
callRef(cxt, v, sch, sch.$async);

@@ -60,2 +54,9 @@ }

};
function getValidate(cxt, sch) {
const { gen } = cxt;
return sch.validate
? gen.scopeValue("validate", { ref: sch.validate })
: codegen_1._ `${gen.scopeValue("wrapper", { ref: sch })}.validate`;
}
exports.getValidate = getValidate;
function callRef(cxt, v, sch, $async) {

@@ -62,0 +63,0 @@ const { gen, it } = cxt;

@@ -6,2 +6,4 @@ "use strict";

const names_1 = require("../../compile/names");
const compile_1 = require("../../compile");
const ref_1 = require("../core/ref");
const def = {

@@ -13,16 +15,17 @@ keyword: "$dynamicAnchor",

function dynamicAnchor(cxt, anchor) {
const { gen, keyword, it } = cxt;
it.dynamicAnchors[anchor] = true;
const { gen, it } = cxt;
it.schemaEnv.root.dynamicAnchors[anchor] = true;
const v = codegen_1._ `${names_1.default.dynamicAnchors}${codegen_1.getProperty(anchor)}`;
if (it.errSchemaPath === "#") {
gen.if(codegen_1._ `!${v}`, () => gen.assign(v, it.validateName));
}
else {
// TODO add support for dynamicRef/recursiveRef not in schema root
// const validate = it.self.getSchema()
throw new Error(`"${keyword}" is only supported in schema root`);
}
const validate = it.errSchemaPath === "#" ? it.validateName : _getValidate(cxt);
gen.if(codegen_1._ `!${v}`, () => gen.assign(v, validate));
}
exports.dynamicAnchor = dynamicAnchor;
function _getValidate(cxt) {
const { schemaEnv, schema, self } = cxt.it;
const { root, baseId, localRefs, meta } = schemaEnv.root;
const sch = new compile_1.SchemaEnv({ schema, root, baseId, localRefs, meta });
compile_1.compileSchema.call(self, sch);
return ref_1.getValidate(cxt, sch);
}
exports.default = def;
//# sourceMappingURL=dynamicAnchor.js.map

@@ -17,11 +17,25 @@ "use strict";

const anchor = ref.slice(1);
const v = gen.let("_v", codegen_1._ `${names_1.default.dynamicAnchors}${codegen_1.getProperty(anchor)}`);
if (it.allErrors) {
gen.if(v, _callRef(v), _callRef(it.validateName));
_dynamicRef();
}
else {
const valid = gen.let("valid", false);
gen.if(v, _callRef(v, valid), _callRef(it.validateName, valid));
_dynamicRef(valid);
cxt.ok(valid);
}
function _dynamicRef(valid) {
// TODO the assumption here is that `recursiveRef: #` always points to the root
// of the schema object, which is not correct, because there may be $id that
// makes # point to it, and the target schema may not contain dynamic/recursiveAnchor.
// Because of that 2 tests in recursiveRef.json fail.
// This is a similar problem to #815 (`$id` doesn't alter resolution scope for `{ "$ref": "#" }`).
// (This problem is not tested in JSON-Schema-Test-Suite)
if (it.schemaEnv.root.dynamicAnchors[anchor]) {
const v = gen.let("_v", codegen_1._ `${names_1.default.dynamicAnchors}${codegen_1.getProperty(anchor)}`);
gen.if(v, _callRef(v, valid), _callRef(it.validateName, valid));
}
else {
_callRef(it.validateName, valid)();
}
}
function _callRef(validate, valid) {

@@ -28,0 +42,0 @@ return valid

@@ -44,3 +44,2 @@ import type {

baseId: string // the current schema base URI that should be used as the base for resolving URIs in references (\$ref)
dynamicAnchors: {[Ref in string]?: true}
readonly schemaPath: Code // the run-time expression that evaluates to the property name of the current schema

@@ -80,2 +79,3 @@ readonly errSchemaPath: string // this is actual string, should not be changed to Code

readonly refs: SchemaRefs = {}
readonly dynamicAnchors: {[Ref in string]?: true} = {}
validate?: AnyValidateFunction

@@ -143,3 +143,2 @@ validateName?: Name

baseId: sch.baseId || rootId,
dynamicAnchors: {},
schemaPath: nil,

@@ -146,0 +145,0 @@ errSchemaPath: "#",

@@ -303,3 +303,3 @@ export {

if (typeof schemaKeyRef == "string") {
v = this.getSchema(schemaKeyRef)
v = this.getSchema<T>(schemaKeyRef)
if (!v) throw new Error(`no schema with key or ref "${schemaKeyRef}"`)

@@ -306,0 +306,0 @@ } else {

import type AjvCore from "../core"
import type {AnyValidateFunction, SourceCode} from "../types"
import type {SchemaEnv} from "../compile"
import {ScopeValueSets, ValueScopeName, varKinds} from "../compile/codegen/scope"
import {_, _Code, Code, getProperty} from "../compile/codegen/code"
import {SchemaEnv} from "../compile"

@@ -63,3 +63,3 @@ export default function standaloneCode(

return validateCode(usedValues, v.source)
} else if (n.prefix === "root" && typeof vRef == "object") {
} else if ((n.prefix === "root" || n.prefix === "wrapper") && typeof vRef == "object") {
const {validate, validateName} = vRef as SchemaEnv

@@ -66,0 +66,0 @@ const vCode = validateCode(usedValues, validate?.source)

@@ -9,8 +9,8 @@ import Ajv, {AnySchema, AnyValidateFunction, ErrorObject} from "../core"

validate<T = unknown>(schemaKeyRef: AnySchema | string, data: unknown | T): boolean | Promise<T> {
validate(schemaKeyRef: AnySchema | string, data: unknown): boolean | Promise<unknown> {
return Ajv.prototype.validate.call(this, schemaKeyRef, data)
}
compile<T = unknown>(schema: AnySchema, _meta?: boolean): AnyValidateFunction<T> {
return this.getStandalone(this.ajv.compile<T>(schema))
compile<T = unknown>(schema: AnySchema, meta?: boolean): AnyValidateFunction<T> {
return this.getStandalone(this.ajv.compile<T>(schema, meta))
}

@@ -17,0 +17,0 @@

@@ -16,2 +16,4 @@ import type {CodeKeywordDefinition, AnySchema} from "../../types"

const {baseId, schemaEnv: env, validateName, opts, self} = it
// TODO See comment in dynamicRef.ts
// This has to be improved to resolve #815.
if (schema === "#" || schema === "#/") return callRootRef()

@@ -30,10 +32,3 @@ const schOrEnv = resolveRef.call(self, env.root, baseId, schema)

function callValidate(sch: SchemaEnv): void {
let v: Code
if (sch.validate) {
v = gen.scopeValue("validate", {ref: sch.validate})
} else {
const code = _`{validate: ${sch.validateName}}`
const wrapper = gen.scopeValue("wrapper", {ref: sch, code})
v = _`${wrapper}.validate`
}
const v = getValidate(cxt, sch)
callRef(cxt, v, sch, sch.$async)

@@ -65,2 +60,9 @@ }

export function getValidate(cxt: KeywordCxt, sch: SchemaEnv): Code {
const {gen} = cxt
return sch.validate
? gen.scopeValue("validate", {ref: sch.validate})
: _`${gen.scopeValue("wrapper", {ref: sch})}.validate`
}
export function callRef(cxt: KeywordCxt, v: Code, sch?: SchemaEnv, $async?: boolean): void {

@@ -67,0 +69,0 @@ const {gen, it} = cxt

import type {CodeKeywordDefinition} from "../../types"
import type KeywordCxt from "../../compile/context"
import {_, getProperty} from "../../compile/codegen"
import {_, getProperty, Code} from "../../compile/codegen"
import N from "../../compile/names"
import {SchemaEnv, compileSchema} from "../../compile"
import {getValidate} from "../core/ref"

@@ -13,14 +15,17 @@ const def: CodeKeywordDefinition = {

export function dynamicAnchor(cxt: KeywordCxt, anchor: string): void {
const {gen, keyword, it} = cxt
it.dynamicAnchors[anchor] = true
const {gen, it} = cxt
it.schemaEnv.root.dynamicAnchors[anchor] = true
const v = _`${N.dynamicAnchors}${getProperty(anchor)}`
if (it.errSchemaPath === "#") {
gen.if(_`!${v}`, () => gen.assign(v, it.validateName))
} else {
// TODO add support for dynamicRef/recursiveRef not in schema root
// const validate = it.self.getSchema()
throw new Error(`"${keyword}" is only supported in schema root`)
}
const validate = it.errSchemaPath === "#" ? it.validateName : _getValidate(cxt)
gen.if(_`!${v}`, () => gen.assign(v, validate))
}
function _getValidate(cxt: KeywordCxt): Code {
const {schemaEnv, schema, self} = cxt.it
const {root, baseId, localRefs, meta} = schemaEnv.root
const sch = new SchemaEnv({schema, root, baseId, localRefs, meta})
compileSchema.call(self, sch)
return getValidate(cxt, sch)
}
export default def

@@ -17,11 +17,25 @@ import type {CodeKeywordDefinition} from "../../types"

const anchor = ref.slice(1)
const v = gen.let("_v", _`${N.dynamicAnchors}${getProperty(anchor)}`)
if (it.allErrors) {
gen.if(v, _callRef(v), _callRef(it.validateName))
_dynamicRef()
} else {
const valid = gen.let("valid", false)
gen.if(v, _callRef(v, valid), _callRef(it.validateName, valid))
_dynamicRef(valid)
cxt.ok(valid)
}
function _dynamicRef(valid?: Name): void {
// TODO the assumption here is that `recursiveRef: #` always points to the root
// of the schema object, which is not correct, because there may be $id that
// makes # point to it, and the target schema may not contain dynamic/recursiveAnchor.
// Because of that 2 tests in recursiveRef.json fail.
// This is a similar problem to #815 (`$id` doesn't alter resolution scope for `{ "$ref": "#" }`).
// (This problem is not tested in JSON-Schema-Test-Suite)
if (it.schemaEnv.root.dynamicAnchors[anchor]) {
const v = gen.let("_v", _`${N.dynamicAnchors}${getProperty(anchor)}`)
gen.if(v, _callRef(v, valid), _callRef(it.validateName, valid))
} else {
_callRef(it.validateName, valid)()
}
}
function _callRef(validate: Code, valid?: Name): () => void {

@@ -28,0 +42,0 @@ return valid

{
"name": "ajv",
"version": "7.0.0-rc.1",
"version": "7.0.0-rc.2",
"description": "Another JSON Schema Validator",

@@ -22,3 +22,3 @@ "main": "dist/ajv.js",

"test-cov": "nyc npm run test-spec",
"bundle": "rm -rf bundle && node ./scripts/bundle.js ajv ajv Ajv && node ./scripts/bundle.js 2019 ajv2019 Ajv2019",
"bundle": "rm -rf bundle && node ./scripts/bundle.js ajv ajv7 ajv7 && node ./scripts/bundle.js 2019 ajv2019 ajv2019",
"build": "rm -rf dist && tsc && cp -r lib/refs dist && rm dist/refs/json-schema-2019-09/index.ts",

@@ -31,4 +31,3 @@ "json-tests": "rm -rf spec/_json/*.js && node scripts/jsontests",

"test-ci": "AJV_FULL_TEST=true npm test",
"prepublish": "npm run build",
"watch": "watch \"npm run build\" ./lib"
"prepublish": "npm run build"
},

@@ -68,3 +67,3 @@ "nyc": {

"fast-deep-equal": "^3.1.1",
"json-schema-traverse": "^0.5.0",
"json-schema-traverse": "^1.0.0",
"require-from-string": "^2.0.2",

@@ -81,3 +80,3 @@ "uri-js": "^4.2.2"

"@typescript-eslint/parser": "^3.8.0",
"ajv-formats": "^0.6.1",
"ajv-formats": "^1.5.0",
"browserify": "^17.0.0",

@@ -87,3 +86,3 @@ "chai": "^4.0.1",

"eslint": "^7.8.1",
"eslint-config-prettier": "^6.11.0",
"eslint-config-prettier": "^7.0.0",
"gh-pages-generator": "^0.2.3",

@@ -105,4 +104,3 @@ "glob": "^7.0.0",

"tsify": "^5.0.2",
"typescript": "^4.0.0",
"watch": "^1.0.0"
"typescript": "^4.0.0"
},

@@ -109,0 +107,0 @@ "collective": {

@@ -9,3 +9,3 @@ <img align="right" alt="Ajv logo" width="160" src="https://ajv.js.org/images/ajv_logo.png">

[![npm](https://img.shields.io/npm/v/ajv.svg)](https://www.npmjs.com/package/ajv)
[![npm (beta)](https://img.shields.io/npm/v/ajv/beta)](https://www.npmjs.com/package/ajv/v/7.0.0-rc.1)
[![npm (beta)](https://img.shields.io/npm/v/ajv/beta)](https://www.npmjs.com/package/ajv/v/7.0.0-rc.2)
[![npm downloads](https://img.shields.io/npm/dm/ajv.svg)](https://www.npmjs.com/package/ajv)

@@ -20,3 +20,3 @@ [![Coverage Status](https://coveralls.io/repos/github/ajv-validator/ajv/badge.svg?branch=master)](https://coveralls.io/github/ajv-validator/ajv?branch=master)

## Using version 7 (beta)
## Using version 7

@@ -47,4 +47,27 @@ Ajv version 7 (beta) is released with these changes:

## Contributing
100+ people contributed to Ajv. You are very welcome to join by implementing new features that are valuable to many users and by improving documentation.
Please do not be disappointed if your suggestion is not accepted - it is important to maintain the balance between the library size, performance and functionality. If it seems that a feature would benefit only a small number of users, its addition may be delayed until there is more support from the users community - so please submit the issue first to explain why this feature is important.
Please include documentation and test coverage with any new feature implementations.
To run tests:
```bash
npm install
git submodule update --init
npm test
```
`npm run build` - compiles typescript to `dist` folder.
Please review [Contributing guidelines](./CONTRIBUTING.md) and [Code components](./docs/components.md).
## Contents
- [Platinum sponsors](#platinum-sponsors)
- [Using version 7](#using-version-7)
- [Contributing](#contributing)
- [Mozilla MOSS grant and OpenJS Foundation](#mozilla-moss-grant-and-openjs-foundation)

@@ -75,3 +98,8 @@ - [Sponsors](#sponsors)

- [Modifying data](./docs/validation.md#modifying-data-during-validation): [additional properties](./docs/validation.md#removing-additional-properties), [defaults](./docs/validation.md#assigning-defaults), [type coercion](./docs/validation.md#coercing-data-types)
- [User-defined keywords](./docs/keywords.md)
- [Extending Ajv](#extending-ajv)
- User-defined keywords:
- [basics](./docs/validation.md#user-defined-keywords)
- [guide](./docs/keywords.md)
- [Plugins](#plugins)
- [Related packages](#related-packages)
- [Security considerations](./docs/security.md)

@@ -84,6 +112,4 @@ - [Security contact](./docs/security.md#security-contact)

- [Content Security Policy](./docs/security.md#content-security-policy)
- [Plugins](#plugins)
- [Related packages](#related-packages)
- [Some packages using Ajv](#some-packages-using-ajv)
- [Tests, Contributing, Changes history](#tests)
- [Changes history](#changes-history)
- [Support, Code of conduct, Contacts, License](#open-source-software-support)

@@ -264,15 +290,33 @@

If you need to use Ajv in several bundles you can create a separate UMD bundle using `npm run bundle` script (thanks to [siddo420](https://github.com/siddo420)).
If you need to use Ajv in several bundles you can create a separate UMD bundles using `npm run bundle` script.
Then you need to load Ajv in the browser:
Then you need to load Ajv with support of JSON Schema draft-07 in the browser:
```html
<script src="ajv.min.js"></script>
<script src="bundle/ajv7.min.js"></script>
<script>
;(function () {
const Ajv = window.ajv7.default
const ajv = new Ajv()
})()
</script>
```
This bundle can be used with different module systems; it creates global `Ajv` if no module system is found.
or to load the bundle that supports JSONSchema draft-2019-09:
```html
<script src="bundle/ajv2019.min.js"></script>
<script>
;(function () {
const Ajv = window.ajv2019.default
const ajv = new Ajv()
})()
</script>
```
This bundle can be used with different module systems; it creates global `ajv` (or `ajv2019`) if no module system is found.
The browser bundle is available on [cdnjs](https://cdnjs.com/libraries/ajv).
**Please note**: some frameworks, e.g. Dojo, may redefine global require in such way that is not compatible with CommonJS module format. In such case Ajv bundle has to be loaded before the framework and then you can use global Ajv (see issue [#234](https://github.com/ajv-validator/ajv/issues/234)).
**Please note**: some frameworks, e.g. Dojo, may redefine global require in a way that is not compatible with CommonJS module format. In this case Ajv bundle has to be loaded before the framework and then you can use global `ajv` (see issue [#234](https://github.com/ajv-validator/ajv/issues/234)).

@@ -285,7 +329,7 @@ ## Command line interface

- generating [standalone validation code](./docs/standalone.md) that exports validation function(s) to be used without Ajv
- migrate schemas to draft-07 (using [json-schema-migrate](https://github.com/epoberezkin/json-schema-migrate))
- migrating schemas to draft-07 and draft-2019-09 (using [json-schema-migrate](https://github.com/epoberezkin/json-schema-migrate))
- validating data file(s) against JSON Schema
- testing expected validity of data against JSON Schema
- referenced schemas
- user-defined meta-schemas
- user-defined meta-schemas, validation keywords and formats
- files in JSON, JSON5, YAML, and JavaScript format

@@ -295,19 +339,25 @@ - all Ajv options

## Plugins
## Extending Ajv
### User defined keywords
See section in [data validation](./docs/validation.md#user-defined-keywords) and the [detailed guide](./docs/keywords.md).
### Plugins
Ajv can be extended with plugins that add keywords, formats or functions to process generated code. When such plugin is published as npm package it is recommended that it follows these conventions:
- it exports a function
- this function accepts ajv instance as the first parameter and returns the same instance to allow chaining
- this function can accept an optional configuration as the second parameter
- it exports a function that accepts ajv instance as the first parameter - it allows using plugins with [ajv-cli](#command-line-interface).
- this function returns the same instance to allow chaining.
- this function can accept an optional configuration as the second parameter.
Youcan import `Plugin` interface from ajv if you use Typescript.
You can import `Plugin` interface from ajv if you use Typescript.
If you have published a useful plugin please submit a PR to add it to the next section.
## Related packages
### Related packages
- [ajv-bsontype](https://github.com/BoLaMN/ajv-bsontype) - plugin to validate mongodb's bsonType formats
- [ajv-cli](https://github.com/jessedc/ajv-cli) - command line interface
- [ajv-formats](https://github.com/ajv-validator/ajv-formats) - formats defined in JSON Schema specification.
- [ajv-formats](https://github.com/ajv-validator/ajv-formats) - formats defined in JSON Schema specification
- [ajv-errors](https://github.com/ajv-validator/ajv-errors) - plugin for defining error messages in the schema

@@ -318,3 +368,3 @@ - [ajv-i18n](https://github.com/ajv-validator/ajv-i18n) - internationalised error messages

- [ajv-merge-patch](https://github.com/ajv-validator/ajv-merge-patch) - plugin with keywords $merge and $patch
- [ajv-formats-draft2019](https://github.com/luzlab/ajv-formats-draft2019) - format validators for draft2019 that aren't included in [ajv-formats](https://github.com/ajv-validator/ajv-formats) (ie. `idn-hostname`, `idn-email`, `iri`, `iri-reference` and `duration`).
- [ajv-formats-draft2019](https://github.com/luzlab/ajv-formats-draft2019) - format validators for draft2019 that aren't included in [ajv-formats](https://github.com/ajv-validator/ajv-formats) (ie. `idn-hostname`, `idn-email`, `iri`, `iri-reference` and `duration`)

@@ -346,18 +396,2 @@ ## Some packages using Ajv

## Tests
```
npm install
git submodule update --init
npm test
```
## Contributing
`npm run build` - compiles typescript to dist folder.
`npm run watch` - automatically compiles typescript when files in lib folder change
Please see [Contributing guidelines](./CONTRIBUTING.md)
## Changes history

@@ -364,0 +398,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc