This commit is contained in:
lalBi94
2023-03-05 13:23:23 +01:00
commit 7bc56c09b5
14034 changed files with 1834369 additions and 0 deletions

21
node_modules/ajv-formats/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Evgeny Poberezkin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

123
node_modules/ajv-formats/README.md generated vendored Normal file
View File

@@ -0,0 +1,123 @@
# ajv-formats
JSON Schema formats for Ajv
[![Build Status](https://travis-ci.org/ajv-validator/ajv-formats.svg?branch=master)](https://travis-ci.org/ajv-validator/ajv-formats)
[![npm](https://img.shields.io/npm/v/ajv-formats.svg)](https://www.npmjs.com/package/ajv-formats)
[![Gitter](https://img.shields.io/gitter/room/ajv-validator/ajv.svg)](https://gitter.im/ajv-validator/ajv)
[![GitHub Sponsors](https://img.shields.io/badge/$-sponsors-brightgreen)](https://github.com/sponsors/epoberezkin)
## Usage
```javascript
// ESM/TypeScript import
import Ajv from "ajv"
import addFormats from "ajv-formats"
// Node.js require:
const Ajv = require("ajv")
const addFormats = require("ajv-formats")
const ajv = new Ajv()
addFormats(ajv)
```
## Formats
The package defines these formats:
- _date_: full-date according to [RFC3339](http://tools.ietf.org/html/rfc3339#section-5.6).
- _time_: time with optional time-zone.
- _date-time_: date-time from the same source (time-zone is mandatory).
- _duration_: duration from [RFC3339](https://tools.ietf.org/html/rfc3339#appendix-A)
- _uri_: full URI.
- _uri-reference_: URI reference, including full and relative URIs.
- _uri-template_: URI template according to [RFC6570](https://tools.ietf.org/html/rfc6570)
- _url_ (deprecated): [URL record](https://url.spec.whatwg.org/#concept-url).
- _email_: email address.
- _hostname_: host name according to [RFC1034](http://tools.ietf.org/html/rfc1034#section-3.5).
- _ipv4_: IP address v4.
- _ipv6_: IP address v6.
- _regex_: tests whether a string is a valid regular expression by passing it to RegExp constructor.
- _uuid_: Universally Unique IDentifier according to [RFC4122](http://tools.ietf.org/html/rfc4122).
- _json-pointer_: JSON-pointer according to [RFC6901](https://tools.ietf.org/html/rfc6901).
- _relative-json-pointer_: relative JSON-pointer according to [this draft](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00).
- _byte_: base64 encoded data according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
- _int32_: signed 32 bits integer according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
- _int64_: signed 64 bits according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
- _float_: float according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
- _double_: double according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
- _password_: password string according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
- _binary_: binary string according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
See regular expressions used for format validation and the sources that were used in [formats.ts](https://github.com/ajv-validator/ajv-formats/blob/master/src/formats.ts).
**Please note**: JSON Schema draft-07 also defines formats `iri`, `iri-reference`, `idn-hostname` and `idn-email` for URLs, hostnames and emails with international characters. These formats are available in [ajv-formats-draft2019](https://github.com/luzlab/ajv-formats-draft2019) plugin.
## Keywords to compare values: `formatMaximum` / `formatMinimum` and `formatExclusiveMaximum` / `formatExclusiveMinimum`
These keywords allow to define minimum/maximum constraints when the format keyword defines ordering (`compare` function in format definition).
These keywords are added to ajv instance when ajv-formats is used without options or with option `keywords: true`.
These keywords apply only to strings. If the data is not a string, the validation succeeds.
The value of keywords `formatMaximum`/`formatMinimum` and `formatExclusiveMaximum`/`formatExclusiveMinimum` should be a string or [\$data reference](https://github.com/ajv-validator/ajv/blob/master/docs/validation.md#data-reference). This value is the maximum (minimum) allowed value for the data to be valid as determined by `format` keyword. If `format` keyword is not present schema compilation will throw exception.
When these keyword are added, they also add comparison functions to formats `"date"`, `"time"` and `"date-time"`. User-defined formats also can have comparison functions. See [addFormat](https://github.com/ajv-validator/ajv/blob/master/docs/api.md#api-addformat) method.
```javascript
require("ajv-formats")(ajv)
const schema = {
type: "string",
format: "date",
formatMinimum: "2016-02-06",
formatExclusiveMaximum: "2016-12-27",
}
const validDataList = ["2016-02-06", "2016-12-26"]
const invalidDataList = ["2016-02-05", "2016-12-27", "abc"]
```
## Options
Options can be passed via the second parameter. Options value can be
1. The list of format names that will be added to ajv instance:
```javascript
addFormats(ajv, ["date", "time"])
```
**Please note**: when ajv encounters an undefined format it throws exception (unless ajv instance was configured with `strict: false` option). To allow specific undefined formats they have to be passed to ajv instance via `formats` option with `true` value:
```javascript
const ajv = new Ajv((formats: {date: true, time: true})) // to ignore "date" and "time" formats in schemas.
```
2. Format validation mode (default is `"full"`) with optional list of format names and `keywords` option to add additional format comparison keywords:
```javascript
addFormats(ajv, {mode: "fast"})
```
or
```javascript
addFormats(ajv, {mode: "fast", formats: ["date", "time"], keywords: true})
```
In `"fast"` mode the following formats are simplified: `"date"`, `"time"`, `"date-time"`, `"uri"`, `"uri-reference"`, `"email"`. For example `"date"`, `"time"` and `"date-time"` do not validate ranges in `"fast"` mode, only string structure, and other formats have simplified regular expressions.
## Tests
```bash
npm install
git submodule update --init
npm test
```
## License
[MIT](https://github.com/ajv-validator/ajv-formats/blob/master/LICENSE)

9
node_modules/ajv-formats/dist/formats.d.ts generated vendored Normal file
View File

@@ -0,0 +1,9 @@
import type { Format } from "ajv";
export declare type FormatMode = "fast" | "full";
export declare type FormatName = "date" | "time" | "date-time" | "duration" | "uri" | "uri-reference" | "uri-template" | "url" | "email" | "hostname" | "ipv4" | "ipv6" | "regex" | "uuid" | "json-pointer" | "json-pointer-uri-fragment" | "relative-json-pointer" | "byte" | "int32" | "int64" | "float" | "double" | "password" | "binary";
export declare type DefinedFormats = {
[key in FormatName]: Format;
};
export declare const fullFormats: DefinedFormats;
export declare const fastFormats: DefinedFormats;
export declare const formatNames: FormatName[];

173
node_modules/ajv-formats/dist/formats.js generated vendored Normal file

File diff suppressed because one or more lines are too long

1
node_modules/ajv-formats/dist/formats.js.map generated vendored Normal file

File diff suppressed because one or more lines are too long

15
node_modules/ajv-formats/dist/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,15 @@
import { FormatMode, FormatName } from "./formats";
import type { Plugin, Format } from "ajv";
export { FormatMode, FormatName } from "./formats";
export { LimitFormatError } from "./limit";
export interface FormatOptions {
mode?: FormatMode;
formats?: FormatName[];
keywords?: boolean;
}
export declare type FormatsPluginOptions = FormatName[] | FormatOptions;
export interface FormatsPlugin extends Plugin<FormatsPluginOptions> {
get: (format: FormatName, mode?: FormatMode) => Format;
}
declare const formatsPlugin: FormatsPlugin;
export default formatsPlugin;

37
node_modules/ajv-formats/dist/index.js generated vendored Normal file
View File

@@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const formats_1 = require("./formats");
const limit_1 = require("./limit");
const codegen_1 = require("ajv/dist/compile/codegen");
const fullName = new codegen_1.Name("fullFormats");
const fastName = new codegen_1.Name("fastFormats");
const formatsPlugin = (ajv, opts = { keywords: true }) => {
if (Array.isArray(opts)) {
addFormats(ajv, opts, formats_1.fullFormats, fullName);
return ajv;
}
const [formats, exportName] = opts.mode === "fast" ? [formats_1.fastFormats, fastName] : [formats_1.fullFormats, fullName];
const list = opts.formats || formats_1.formatNames;
addFormats(ajv, list, formats, exportName);
if (opts.keywords)
limit_1.default(ajv);
return ajv;
};
formatsPlugin.get = (name, mode = "full") => {
const formats = mode === "fast" ? formats_1.fastFormats : formats_1.fullFormats;
const f = formats[name];
if (!f)
throw new Error(`Unknown format "${name}"`);
return f;
};
function addFormats(ajv, list, fs, exportName) {
var _a;
var _b;
(_a = (_b = ajv.opts.code).formats) !== null && _a !== void 0 ? _a : (_b.formats = codegen_1._ `require("ajv-formats/dist/formats").${exportName}`);
for (const f of list)
ajv.addFormat(f, fs[f]);
}
module.exports = exports = formatsPlugin;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = formatsPlugin;
//# sourceMappingURL=index.js.map

1
node_modules/ajv-formats/dist/index.js.map generated vendored Normal file

File diff suppressed because one or more lines are too long

10
node_modules/ajv-formats/dist/limit.d.ts generated vendored Normal file
View File

@@ -0,0 +1,10 @@
import type { Plugin, CodeKeywordDefinition, ErrorObject } from "ajv";
declare type Kwd = "formatMaximum" | "formatMinimum" | "formatExclusiveMaximum" | "formatExclusiveMinimum";
declare type Comparison = "<=" | ">=" | "<" | ">";
export declare type LimitFormatError = ErrorObject<Kwd, {
limit: string;
comparison: Comparison;
}>;
export declare const formatLimitDefinition: CodeKeywordDefinition;
declare const formatLimitPlugin: Plugin<undefined>;
export default formatLimitPlugin;

69
node_modules/ajv-formats/dist/limit.js generated vendored Normal file
View File

@@ -0,0 +1,69 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatLimitDefinition = void 0;
const ajv_1 = require("ajv");
const codegen_1 = require("ajv/dist/compile/codegen");
const ops = codegen_1.operators;
const KWDs = {
formatMaximum: { okStr: "<=", ok: ops.LTE, fail: ops.GT },
formatMinimum: { okStr: ">=", ok: ops.GTE, fail: ops.LT },
formatExclusiveMaximum: { okStr: "<", ok: ops.LT, fail: ops.GTE },
formatExclusiveMinimum: { okStr: ">", ok: ops.GT, fail: ops.LTE },
};
const error = {
message: ({ keyword, schemaCode }) => codegen_1.str `should be ${KWDs[keyword].okStr} ${schemaCode}`,
params: ({ keyword, schemaCode }) => codegen_1._ `{comparison: ${KWDs[keyword].okStr}, limit: ${schemaCode}}`,
};
exports.formatLimitDefinition = {
keyword: Object.keys(KWDs),
type: "string",
schemaType: "string",
$data: true,
error,
code(cxt) {
const { gen, data, schemaCode, keyword, it } = cxt;
const { opts, self } = it;
if (!opts.validateFormats)
return;
const fCxt = new ajv_1.KeywordCxt(it, self.RULES.all.format.definition, "format");
if (fCxt.$data)
validate$DataFormat();
else
validateFormat();
function validate$DataFormat() {
const fmts = gen.scopeValue("formats", {
ref: self.formats,
code: opts.code.formats,
});
const fmt = gen.const("fmt", codegen_1._ `${fmts}[${fCxt.schemaCode}]`);
cxt.fail$data(codegen_1.or(codegen_1._ `typeof ${fmt} != "object"`, codegen_1._ `${fmt} instanceof RegExp`, codegen_1._ `typeof ${fmt}.compare != "function"`, compareCode(fmt)));
}
function validateFormat() {
const format = fCxt.schema;
const fmtDef = self.formats[format];
if (!fmtDef || fmtDef === true)
return;
if (typeof fmtDef != "object" ||
fmtDef instanceof RegExp ||
typeof fmtDef.compare != "function") {
throw new Error(`"${keyword}": format "${format}" does not define "compare" function`);
}
const fmt = gen.scopeValue("formats", {
key: format,
ref: fmtDef,
code: opts.code.formats ? codegen_1._ `${opts.code.formats}${codegen_1.getProperty(format)}` : undefined,
});
cxt.fail$data(compareCode(fmt));
}
function compareCode(fmt) {
return codegen_1._ `${fmt}.compare(${data}, ${schemaCode}) ${KWDs[keyword].fail} 0`;
}
},
dependencies: ["format"],
};
const formatLimitPlugin = (ajv) => {
ajv.addKeyword(exports.formatLimitDefinition);
return ajv;
};
exports.default = formatLimitPlugin;
//# sourceMappingURL=limit.js.map

1
node_modules/ajv-formats/dist/limit.js.map generated vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,23 @@
const Ajv = require("ajv")
const ajv = new Ajv({allErrors: true})
const schema = {
type: "object",
properties: {
foo: {type: "string"},
bar: {type: "number", maximum: 3},
},
required: ["foo", "bar"],
additionalProperties: false,
}
const validate = ajv.compile(schema)
test({foo: "abc", bar: 2})
test({foo: 2, bar: 4})
function test(data) {
const valid = validate(data)
if (valid) console.log("Valid!")
else console.log("Invalid: " + ajv.errorsText(validate.errors))
}

22
node_modules/ajv-formats/node_modules/ajv/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015-2021 Evgeny Poberezkin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

193
node_modules/ajv-formats/node_modules/ajv/README.md generated vendored Normal file
View File

@@ -0,0 +1,193 @@
<img align="right" alt="Ajv logo" width="160" src="https://ajv.js.org/img/ajv.svg">
&nbsp;
# Ajv JSON schema validator
The fastest JSON validator for Node.js and browser.
Supports JSON Schema draft-04/06/07/2019-09/2020-12 ([draft-04 support](https://ajv.js.org/json-schema.html#draft-04) requires ajv-draft-04 package) and JSON Type Definition [RFC8927](https://datatracker.ietf.org/doc/rfc8927/).
[![build](https://github.com/ajv-validator/ajv/workflows/build/badge.svg)](https://github.com/ajv-validator/ajv/actions?query=workflow%3Abuild)
[![npm](https://img.shields.io/npm/v/ajv.svg)](https://www.npmjs.com/package/ajv)
[![npm downloads](https://img.shields.io/npm/dm/ajv.svg)](https://www.npmjs.com/package/ajv)
[![Coverage Status](https://coveralls.io/repos/github/ajv-validator/ajv/badge.svg?branch=master)](https://coveralls.io/github/ajv-validator/ajv?branch=master)
[![SimpleX](https://img.shields.io/badge/chat-on%20SimpleX-%2307b4b9)](https://simplex.chat/contact#/?v=1&smp=smp%3A%2F%2Fu2dS9sG8nMNURyZwqASV4yROM28Er0luVTx5X1CsMrU%3D%40smp4.simplex.im%2Fap4lMFzfXF8Hzmh-Vz0WNxp_1jKiOa-h%23MCowBQYDK2VuAyEAcdefddRvDfI8iAuBpztm_J3qFucj8MDZoVs_2EcMTzU%3D)
[![Gitter](https://img.shields.io/gitter/room/ajv-validator/ajv.svg)](https://gitter.im/ajv-validator/ajv)
[![GitHub Sponsors](https://img.shields.io/badge/$-sponsors-brightgreen)](https://github.com/sponsors/epoberezkin)
## Ajv sponsors
[<img src="https://ajv.js.org/img/mozilla.svg" width="45%" alt="Mozilla">](https://www.mozilla.org)<img src="https://ajv.js.org/img/gap.svg" width="9%">[<img src="https://ajv.js.org/img/reserved.svg" width="45%">](https://opencollective.com/ajv)
[<img src="https://ajv.js.org/img/microsoft.png" width="31%" alt="Microsoft">](https://opensource.microsoft.com)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/reserved.svg" width="31%">](https://opencollective.com/ajv)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/reserved.svg" width="31%">](https://opencollective.com/ajv)
[<img src="https://ajv.js.org/img/retool.svg" width="22.5%" alt="Retool">](https://retool.com/?utm_source=sponsor&utm_campaign=ajv)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/tidelift.svg" width="22.5%" alt="Tidelift">](https://tidelift.com/subscription/pkg/npm-ajv?utm_source=npm-ajv&utm_medium=referral&utm_campaign=enterprise)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/simplex.svg" width="22.5%" alt="SimpleX">](https://github.com/simplex-chat/simplex-chat)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/reserved.svg" width="22.5%">](https://opencollective.com/ajv)
## Contributing
More than 100 people contributed to Ajv, and we would love to have you join the development. We welcome implementing new features that will benefit many users and ideas to improve our documentation.
Please review [Contributing guidelines](./CONTRIBUTING.md) and [Code components](https://ajv.js.org/components.html).
## Documentation
All documentation is available on the [Ajv website](https://ajv.js.org).
Some useful site links:
- [Getting started](https://ajv.js.org/guide/getting-started.html)
- [JSON Schema vs JSON Type Definition](https://ajv.js.org/guide/schema-language.html)
- [API reference](https://ajv.js.org/api.html)
- [Strict mode](https://ajv.js.org/strict-mode.html)
- [Standalone validation code](https://ajv.js.org/standalone.html)
- [Security considerations](https://ajv.js.org/security.html)
- [Command line interface](https://ajv.js.org/packages/ajv-cli.html)
- [Frequently Asked Questions](https://ajv.js.org/faq.html)
## <a name="sponsors"></a>Please [sponsor Ajv development](https://github.com/sponsors/epoberezkin)
Since I asked to support Ajv development 40 people and 6 organizations contributed via GitHub and OpenCollective - this support helped receiving the MOSS grant!
Your continuing support is very important - the funds will be used to develop and maintain Ajv once the next major version is released.
Please sponsor Ajv via:
- [GitHub sponsors page](https://github.com/sponsors/epoberezkin) (GitHub will match it)
- [Ajv Open Collective](https://opencollective.com/ajv)
Thank you.
#### Open Collective sponsors
<a href="https://opencollective.com/ajv"><img src="https://opencollective.com/ajv/individuals.svg?width=890"></a>
<a href="https://opencollective.com/ajv/organization/0/website"><img src="https://opencollective.com/ajv/organization/0/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/1/website"><img src="https://opencollective.com/ajv/organization/1/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/2/website"><img src="https://opencollective.com/ajv/organization/2/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/3/website"><img src="https://opencollective.com/ajv/organization/3/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/4/website"><img src="https://opencollective.com/ajv/organization/4/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/5/website"><img src="https://opencollective.com/ajv/organization/5/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/6/website"><img src="https://opencollective.com/ajv/organization/6/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/7/website"><img src="https://opencollective.com/ajv/organization/7/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/8/website"><img src="https://opencollective.com/ajv/organization/8/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/9/website"><img src="https://opencollective.com/ajv/organization/9/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/10/website"><img src="https://opencollective.com/ajv/organization/10/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/11/website"><img src="https://opencollective.com/ajv/organization/11/avatar.svg"></a>
## Performance
Ajv generates code to turn JSON Schemas into super-fast validation functions that are efficient for v8 optimization.
Currently Ajv is the fastest and the most standard compliant validator according to these benchmarks:
- [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark) - 50% faster than the second place
- [jsck benchmark](https://github.com/pandastrike/jsck#benchmarks) - 20-190% faster
- [z-schema benchmark](https://rawgit.com/zaggino/z-schema/master/benchmark/results.html)
- [themis benchmark](https://cdn.rawgit.com/playlyfe/themis/master/benchmark/results.html)
Performance of different validators by [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark):
[![performance](https://chart.googleapis.com/chart?chxt=x,y&cht=bhs&chco=76A4FB&chls=2.0&chbh=62,4,1&chs=600x416&chxl=-1:|ajv|@exodus&#x2F;schemasafe|is-my-json-valid|djv|@cfworker&#x2F;json-schema|jsonschema&chd=t:100,69.2,51.5,13.1,5.1,1.2)](https://github.com/ebdrup/json-schema-benchmark/blob/master/README.md#performance)
## Features
- Ajv implements JSON Schema [draft-06/07/2019-09/2020-12](http://json-schema.org/) standards (draft-04 is supported in v6):
- all validation keywords (see [JSON Schema validation keywords](https://ajv.js.org/json-schema.html))
- [OpenAPI](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md) extensions:
- NEW: keyword [discriminator](https://ajv.js.org/json-schema.html#discriminator).
- keyword [nullable](https://ajv.js.org/json-schema.html#nullable).
- full support of remote references (remote schemas have to be added with `addSchema` or compiled to be available)
- support of recursive references between schemas
- correct string lengths for strings with unicode pairs
- JSON Schema [formats](https://ajv.js.org/guide/formats.html) (with [ajv-formats](https://github.com/ajv-validator/ajv-formats) plugin).
- [validates schemas against meta-schema](https://ajv.js.org/api.html#api-validateschema)
- NEW: supports [JSON Type Definition](https://datatracker.ietf.org/doc/rfc8927/):
- all keywords (see [JSON Type Definition schema forms](https://ajv.js.org/json-type-definition.html))
- meta-schema for JTD schemas
- "union" keyword and user-defined keywords (can be used inside "metadata" member of the schema)
- supports [browsers](https://ajv.js.org/guide/environments.html#browsers) and Node.js 10.x - current
- [asynchronous loading](https://ajv.js.org/guide/managing-schemas.html#asynchronous-schema-loading) of referenced schemas during compilation
- "All errors" validation mode with [option allErrors](https://ajv.js.org/options.html#allerrors)
- [error messages with parameters](https://ajv.js.org/api.html#validation-errors) describing error reasons to allow error message generation
- i18n error messages support with [ajv-i18n](https://github.com/ajv-validator/ajv-i18n) package
- [removing-additional-properties](https://ajv.js.org/guide/modifying-data.html#removing-additional-properties)
- [assigning defaults](https://ajv.js.org/guide/modifying-data.html#assigning-defaults) to missing properties and items
- [coercing data](https://ajv.js.org/guide/modifying-data.html#coercing-data-types) to the types specified in `type` keywords
- [user-defined keywords](https://ajv.js.org/guide/user-keywords.html)
- additional extension keywords with [ajv-keywords](https://github.com/ajv-validator/ajv-keywords) package
- [\$data reference](https://ajv.js.org/guide/combining-schemas.html#data-reference) to use values from the validated data as values for the schema keywords
- [asynchronous validation](https://ajv.js.org/guide/async-validation.html) of user-defined formats and keywords
## Install
To install version 8:
```
npm install ajv
```
## <a name="usage"></a>Getting started
Try it in the Node.js REPL: https://runkit.com/npm/ajv
In JavaScript:
```javascript
// or ESM/TypeScript import
import Ajv from "ajv"
// Node.js require:
const Ajv = require("ajv")
const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}
const schema = {
type: "object",
properties: {
foo: {type: "integer"},
bar: {type: "string"}
},
required: ["foo"],
additionalProperties: false,
}
const data = {
foo: 1,
bar: "abc"
}
const validate = ajv.compile(schema)
const valid = validate(data)
if (!valid) console.log(validate.errors)
```
Learn how to use Ajv and see more examples in the [Guide: getting started](https://ajv.js.org/guide/getting-started.html)
## Changes history
See [https://github.com/ajv-validator/ajv/releases](https://github.com/ajv-validator/ajv/releases)
**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).
## Code of conduct
Please review and follow the [Code of conduct](./CODE_OF_CONDUCT.md).
Please report any unacceptable behaviour to ajv.validator@gmail.com - it will be reviewed by the project team.
## Security contact
To report a security vulnerability, please use the
[Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerabilities via GitHub issues.
## Open-source software support
Ajv is a part of [Tidelift subscription](https://tidelift.com/subscription/pkg/npm-ajv?utm_source=npm-ajv&utm_medium=referral&utm_campaign=readme) - it provides a centralised support to open-source software users, in addition to the support provided by software maintainers.
## License
[MIT](./LICENSE)

View File

@@ -0,0 +1,17 @@
import type { AnySchemaObject } from "./types";
import AjvCore, { Options } from "./core";
declare class Ajv2019 extends AjvCore {
constructor(opts?: Options);
_addVocabularies(): void;
_addDefaultMetaSchema(): void;
defaultMeta(): string | AnySchemaObject | undefined;
}
export default Ajv2019;
export { Format, FormatDefinition, AsyncFormatDefinition, KeywordDefinition, KeywordErrorDefinition, CodeKeywordDefinition, MacroKeywordDefinition, FuncKeywordDefinition, Vocabulary, Schema, SchemaObject, AnySchemaObject, AsyncSchema, AnySchema, ValidateFunction, AsyncValidateFunction, ErrorObject, ErrorNoParams, } from "./types";
export { Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions } from "./core";
export { SchemaCxt, SchemaObjCxt } from "./compile";
export { KeywordCxt } from "./compile/validate";
export { DefinedError } from "./vocabularies/errors";
export { JSONType } from "./compile/rules";
export { JSONSchemaType } from "./types/json-schema";
export { _, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions } from "./compile/codegen";

55
node_modules/ajv-formats/node_modules/ajv/dist/2019.js generated vendored Normal file
View File

@@ -0,0 +1,55 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeGen = exports.Name = exports.nil = exports.stringify = exports.str = exports._ = exports.KeywordCxt = void 0;
const core_1 = require("./core");
const draft7_1 = require("./vocabularies/draft7");
const dynamic_1 = require("./vocabularies/dynamic");
const next_1 = require("./vocabularies/next");
const unevaluated_1 = require("./vocabularies/unevaluated");
const discriminator_1 = require("./vocabularies/discriminator");
const json_schema_2019_09_1 = require("./refs/json-schema-2019-09");
const META_SCHEMA_ID = "https://json-schema.org/draft/2019-09/schema";
class Ajv2019 extends core_1.default {
constructor(opts = {}) {
super({
...opts,
dynamicRef: true,
next: true,
unevaluated: true,
});
}
_addVocabularies() {
super._addVocabularies();
this.addVocabulary(dynamic_1.default);
draft7_1.default.forEach((v) => this.addVocabulary(v));
this.addVocabulary(next_1.default);
this.addVocabulary(unevaluated_1.default);
if (this.opts.discriminator)
this.addKeyword(discriminator_1.default);
}
_addDefaultMetaSchema() {
super._addDefaultMetaSchema();
const { $data, meta } = this.opts;
if (!meta)
return;
json_schema_2019_09_1.default.call(this, $data);
this.refs["http://json-schema.org/schema"] = META_SCHEMA_ID;
}
defaultMeta() {
return (this.opts.defaultMeta =
super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined));
}
}
module.exports = exports = Ajv2019;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Ajv2019;
var validate_1 = require("./compile/validate");
Object.defineProperty(exports, "KeywordCxt", { enumerable: true, get: function () { return validate_1.KeywordCxt; } });
var codegen_1 = require("./compile/codegen");
Object.defineProperty(exports, "_", { enumerable: true, get: function () { return codegen_1._; } });
Object.defineProperty(exports, "str", { enumerable: true, get: function () { return codegen_1.str; } });
Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return codegen_1.stringify; } });
Object.defineProperty(exports, "nil", { enumerable: true, get: function () { return codegen_1.nil; } });
Object.defineProperty(exports, "Name", { enumerable: true, get: function () { return codegen_1.Name; } });
Object.defineProperty(exports, "CodeGen", { enumerable: true, get: function () { return codegen_1.CodeGen; } });
//# sourceMappingURL=2019.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,17 @@
import type { AnySchemaObject } from "./types";
import AjvCore, { Options } from "./core";
declare class Ajv2020 extends AjvCore {
constructor(opts?: Options);
_addVocabularies(): void;
_addDefaultMetaSchema(): void;
defaultMeta(): string | AnySchemaObject | undefined;
}
export default Ajv2020;
export { Format, FormatDefinition, AsyncFormatDefinition, KeywordDefinition, KeywordErrorDefinition, CodeKeywordDefinition, MacroKeywordDefinition, FuncKeywordDefinition, Vocabulary, Schema, SchemaObject, AnySchemaObject, AsyncSchema, AnySchema, ValidateFunction, AsyncValidateFunction, ErrorObject, ErrorNoParams, } from "./types";
export { Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions } from "./core";
export { SchemaCxt, SchemaObjCxt } from "./compile";
export { KeywordCxt } from "./compile/validate";
export { DefinedError } from "./vocabularies/errors";
export { JSONType } from "./compile/rules";
export { JSONSchemaType } from "./types/json-schema";
export { _, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions } from "./compile/codegen";

49
node_modules/ajv-formats/node_modules/ajv/dist/2020.js generated vendored Normal file
View File

@@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeGen = exports.Name = exports.nil = exports.stringify = exports.str = exports._ = exports.KeywordCxt = void 0;
const core_1 = require("./core");
const draft2020_1 = require("./vocabularies/draft2020");
const discriminator_1 = require("./vocabularies/discriminator");
const json_schema_2020_12_1 = require("./refs/json-schema-2020-12");
const META_SCHEMA_ID = "https://json-schema.org/draft/2020-12/schema";
class Ajv2020 extends core_1.default {
constructor(opts = {}) {
super({
...opts,
dynamicRef: true,
next: true,
unevaluated: true,
});
}
_addVocabularies() {
super._addVocabularies();
draft2020_1.default.forEach((v) => this.addVocabulary(v));
if (this.opts.discriminator)
this.addKeyword(discriminator_1.default);
}
_addDefaultMetaSchema() {
super._addDefaultMetaSchema();
const { $data, meta } = this.opts;
if (!meta)
return;
json_schema_2020_12_1.default.call(this, $data);
this.refs["http://json-schema.org/schema"] = META_SCHEMA_ID;
}
defaultMeta() {
return (this.opts.defaultMeta =
super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined));
}
}
module.exports = exports = Ajv2020;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Ajv2020;
var validate_1 = require("./compile/validate");
Object.defineProperty(exports, "KeywordCxt", { enumerable: true, get: function () { return validate_1.KeywordCxt; } });
var codegen_1 = require("./compile/codegen");
Object.defineProperty(exports, "_", { enumerable: true, get: function () { return codegen_1._; } });
Object.defineProperty(exports, "str", { enumerable: true, get: function () { return codegen_1.str; } });
Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return codegen_1.stringify; } });
Object.defineProperty(exports, "nil", { enumerable: true, get: function () { return codegen_1.nil; } });
Object.defineProperty(exports, "Name", { enumerable: true, get: function () { return codegen_1.Name; } });
Object.defineProperty(exports, "CodeGen", { enumerable: true, get: function () { return codegen_1.CodeGen; } });
//# sourceMappingURL=2020.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,16 @@
import type { AnySchemaObject } from "./types";
import AjvCore from "./core";
declare class Ajv extends AjvCore {
_addVocabularies(): void;
_addDefaultMetaSchema(): void;
defaultMeta(): string | AnySchemaObject | undefined;
}
export default Ajv;
export { Format, FormatDefinition, AsyncFormatDefinition, KeywordDefinition, KeywordErrorDefinition, CodeKeywordDefinition, MacroKeywordDefinition, FuncKeywordDefinition, Vocabulary, Schema, SchemaObject, AnySchemaObject, AsyncSchema, AnySchema, ValidateFunction, AsyncValidateFunction, SchemaValidateFunction, ErrorObject, ErrorNoParams, } from "./types";
export { Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions } from "./core";
export { SchemaCxt, SchemaObjCxt } from "./compile";
export { KeywordCxt } from "./compile/validate";
export { DefinedError } from "./vocabularies/errors";
export { JSONType } from "./compile/rules";
export { JSONSchemaType } from "./types/json-schema";
export { _, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions } from "./compile/codegen";

44
node_modules/ajv-formats/node_modules/ajv/dist/ajv.js generated vendored Normal file
View File

@@ -0,0 +1,44 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeGen = exports.Name = exports.nil = exports.stringify = exports.str = exports._ = exports.KeywordCxt = void 0;
const core_1 = require("./core");
const draft7_1 = require("./vocabularies/draft7");
const discriminator_1 = require("./vocabularies/discriminator");
const draft7MetaSchema = require("./refs/json-schema-draft-07.json");
const META_SUPPORT_DATA = ["/properties"];
const META_SCHEMA_ID = "http://json-schema.org/draft-07/schema";
class Ajv extends core_1.default {
_addVocabularies() {
super._addVocabularies();
draft7_1.default.forEach((v) => this.addVocabulary(v));
if (this.opts.discriminator)
this.addKeyword(discriminator_1.default);
}
_addDefaultMetaSchema() {
super._addDefaultMetaSchema();
if (!this.opts.meta)
return;
const metaSchema = this.opts.$data
? this.$dataMetaSchema(draft7MetaSchema, META_SUPPORT_DATA)
: draft7MetaSchema;
this.addMetaSchema(metaSchema, META_SCHEMA_ID, false);
this.refs["http://json-schema.org/schema"] = META_SCHEMA_ID;
}
defaultMeta() {
return (this.opts.defaultMeta =
super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined));
}
}
module.exports = exports = Ajv;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Ajv;
var validate_1 = require("./compile/validate");
Object.defineProperty(exports, "KeywordCxt", { enumerable: true, get: function () { return validate_1.KeywordCxt; } });
var codegen_1 = require("./compile/codegen");
Object.defineProperty(exports, "_", { enumerable: true, get: function () { return codegen_1._; } });
Object.defineProperty(exports, "str", { enumerable: true, get: function () { return codegen_1.str; } });
Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return codegen_1.stringify; } });
Object.defineProperty(exports, "nil", { enumerable: true, get: function () { return codegen_1.nil; } });
Object.defineProperty(exports, "Name", { enumerable: true, get: function () { return codegen_1.Name; } });
Object.defineProperty(exports, "CodeGen", { enumerable: true, get: function () { return codegen_1.CodeGen; } });
//# sourceMappingURL=ajv.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,40 @@
export declare abstract class _CodeOrName {
abstract readonly str: string;
abstract readonly names: UsedNames;
abstract toString(): string;
abstract emptyStr(): boolean;
}
export declare const IDENTIFIER: RegExp;
export declare class Name extends _CodeOrName {
readonly str: string;
constructor(s: string);
toString(): string;
emptyStr(): boolean;
get names(): UsedNames;
}
export declare class _Code extends _CodeOrName {
readonly _items: readonly CodeItem[];
private _str?;
private _names?;
constructor(code: string | readonly CodeItem[]);
toString(): string;
emptyStr(): boolean;
get str(): string;
get names(): UsedNames;
}
export declare type CodeItem = Name | string | number | boolean | null;
export declare type UsedNames = Record<string, number | undefined>;
export declare type Code = _Code | Name;
export declare type SafeExpr = Code | number | boolean | null;
export declare const nil: _Code;
declare type CodeArg = SafeExpr | string | undefined;
export declare function _(strs: TemplateStringsArray, ...args: CodeArg[]): _Code;
export declare function str(strs: TemplateStringsArray, ...args: (CodeArg | string[])[]): _Code;
export declare function addCodeArg(code: CodeItem[], arg: CodeArg | string[]): void;
export declare function strConcat(c1: Code, c2: Code): Code;
export declare function stringify(x: unknown): Code;
export declare function safeStringify(x: unknown): string;
export declare function getProperty(key: Code | string | number): Code;
export declare function getEsmExportName(key: Code | string | number): Code;
export declare function regexpCode(rx: RegExp): Code;
export {};

View File

@@ -0,0 +1,155 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.regexpCode = exports.getEsmExportName = exports.getProperty = exports.safeStringify = exports.stringify = exports.strConcat = exports.addCodeArg = exports.str = exports._ = exports.nil = exports._Code = exports.Name = exports.IDENTIFIER = exports._CodeOrName = void 0;
class _CodeOrName {
}
exports._CodeOrName = _CodeOrName;
exports.IDENTIFIER = /^[a-z$_][a-z$_0-9]*$/i;
class Name extends _CodeOrName {
constructor(s) {
super();
if (!exports.IDENTIFIER.test(s))
throw new Error("CodeGen: name must be a valid identifier");
this.str = s;
}
toString() {
return this.str;
}
emptyStr() {
return false;
}
get names() {
return { [this.str]: 1 };
}
}
exports.Name = Name;
class _Code extends _CodeOrName {
constructor(code) {
super();
this._items = typeof code === "string" ? [code] : code;
}
toString() {
return this.str;
}
emptyStr() {
if (this._items.length > 1)
return false;
const item = this._items[0];
return item === "" || item === '""';
}
get str() {
var _a;
return ((_a = this._str) !== null && _a !== void 0 ? _a : (this._str = this._items.reduce((s, c) => `${s}${c}`, "")));
}
get names() {
var _a;
return ((_a = this._names) !== null && _a !== void 0 ? _a : (this._names = this._items.reduce((names, c) => {
if (c instanceof Name)
names[c.str] = (names[c.str] || 0) + 1;
return names;
}, {})));
}
}
exports._Code = _Code;
exports.nil = new _Code("");
function _(strs, ...args) {
const code = [strs[0]];
let i = 0;
while (i < args.length) {
addCodeArg(code, args[i]);
code.push(strs[++i]);
}
return new _Code(code);
}
exports._ = _;
const plus = new _Code("+");
function str(strs, ...args) {
const expr = [safeStringify(strs[0])];
let i = 0;
while (i < args.length) {
expr.push(plus);
addCodeArg(expr, args[i]);
expr.push(plus, safeStringify(strs[++i]));
}
optimize(expr);
return new _Code(expr);
}
exports.str = str;
function addCodeArg(code, arg) {
if (arg instanceof _Code)
code.push(...arg._items);
else if (arg instanceof Name)
code.push(arg);
else
code.push(interpolate(arg));
}
exports.addCodeArg = addCodeArg;
function optimize(expr) {
let i = 1;
while (i < expr.length - 1) {
if (expr[i] === plus) {
const res = mergeExprItems(expr[i - 1], expr[i + 1]);
if (res !== undefined) {
expr.splice(i - 1, 3, res);
continue;
}
expr[i++] = "+";
}
i++;
}
}
function mergeExprItems(a, b) {
if (b === '""')
return a;
if (a === '""')
return b;
if (typeof a == "string") {
if (b instanceof Name || a[a.length - 1] !== '"')
return;
if (typeof b != "string")
return `${a.slice(0, -1)}${b}"`;
if (b[0] === '"')
return a.slice(0, -1) + b.slice(1);
return;
}
if (typeof b == "string" && b[0] === '"' && !(a instanceof Name))
return `"${a}${b.slice(1)}`;
return;
}
function strConcat(c1, c2) {
return c2.emptyStr() ? c1 : c1.emptyStr() ? c2 : str `${c1}${c2}`;
}
exports.strConcat = strConcat;
// TODO do not allow arrays here
function interpolate(x) {
return typeof x == "number" || typeof x == "boolean" || x === null
? x
: safeStringify(Array.isArray(x) ? x.join(",") : x);
}
function stringify(x) {
return new _Code(safeStringify(x));
}
exports.stringify = stringify;
function safeStringify(x) {
return JSON.stringify(x)
.replace(/\u2028/g, "\\u2028")
.replace(/\u2029/g, "\\u2029");
}
exports.safeStringify = safeStringify;
function getProperty(key) {
return typeof key == "string" && exports.IDENTIFIER.test(key) ? new _Code(`.${key}`) : _ `[${key}]`;
}
exports.getProperty = getProperty;
//Does best effort to format the name properly
function getEsmExportName(key) {
if (typeof key == "string" && exports.IDENTIFIER.test(key)) {
return new _Code(`${key}`);
}
throw new Error(`CodeGen: invalid export name: ${key}, use explicit $id name mapping`);
}
exports.getEsmExportName = getEsmExportName;
function regexpCode(rx) {
return new _Code(rx.toString());
}
exports.regexpCode = regexpCode;
//# sourceMappingURL=code.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,79 @@
import type { ScopeValueSets, NameValue, ValueScope, ValueScopeName } from "./scope";
import { _Code, Code, Name } from "./code";
import { Scope } from "./scope";
export { _, str, strConcat, nil, getProperty, stringify, regexpCode, Name, Code } from "./code";
export { Scope, ScopeStore, ValueScope, ValueScopeName, ScopeValueSets, varKinds } from "./scope";
export declare type SafeExpr = Code | number | boolean | null;
export declare type Block = Code | (() => void);
export declare const operators: {
GT: _Code;
GTE: _Code;
LT: _Code;
LTE: _Code;
EQ: _Code;
NEQ: _Code;
NOT: _Code;
OR: _Code;
AND: _Code;
ADD: _Code;
};
export interface CodeGenOptions {
es5?: boolean;
lines?: boolean;
ownProperties?: boolean;
}
export declare class CodeGen {
readonly _scope: Scope;
readonly _extScope: ValueScope;
readonly _values: ScopeValueSets;
private readonly _nodes;
private readonly _blockStarts;
private readonly _constants;
private readonly opts;
constructor(extScope: ValueScope, opts?: CodeGenOptions);
toString(): string;
name(prefix: string): Name;
scopeName(prefix: string): ValueScopeName;
scopeValue(prefixOrName: ValueScopeName | string, value: NameValue): Name;
getScopeValue(prefix: string, keyOrRef: unknown): ValueScopeName | undefined;
scopeRefs(scopeName: Name): Code;
scopeCode(): Code;
private _def;
const(nameOrPrefix: Name | string, rhs: SafeExpr, _constant?: boolean): Name;
let(nameOrPrefix: Name | string, rhs?: SafeExpr, _constant?: boolean): Name;
var(nameOrPrefix: Name | string, rhs?: SafeExpr, _constant?: boolean): Name;
assign(lhs: Code, rhs: SafeExpr, sideEffects?: boolean): CodeGen;
add(lhs: Code, rhs: SafeExpr): CodeGen;
code(c: Block | SafeExpr): CodeGen;
object(...keyValues: [Name | string, SafeExpr | string][]): _Code;
if(condition: Code | boolean, thenBody?: Block, elseBody?: Block): CodeGen;
elseIf(condition: Code | boolean): CodeGen;
else(): CodeGen;
endIf(): CodeGen;
private _for;
for(iteration: Code, forBody?: Block): CodeGen;
forRange(nameOrPrefix: Name | string, from: SafeExpr, to: SafeExpr, forBody: (index: Name) => void, varKind?: Code): CodeGen;
forOf(nameOrPrefix: Name | string, iterable: Code, forBody: (item: Name) => void, varKind?: Code): CodeGen;
forIn(nameOrPrefix: Name | string, obj: Code, forBody: (item: Name) => void, varKind?: Code): CodeGen;
endFor(): CodeGen;
label(label: Name): CodeGen;
break(label?: Code): CodeGen;
return(value: Block | SafeExpr): CodeGen;
try(tryBody: Block, catchCode?: (e: Name) => void, finallyCode?: Block): CodeGen;
throw(error: Code): CodeGen;
block(body?: Block, nodeCount?: number): CodeGen;
endBlock(nodeCount?: number): CodeGen;
func(name: Name, args?: Code, async?: boolean, funcBody?: Block): CodeGen;
endFunc(): CodeGen;
optimize(n?: number): void;
private _leafNode;
private _blockNode;
private _endBlockNode;
private _elseNode;
private get _root();
private get _currNode();
private set _currNode(value);
}
export declare function not<T extends Code | SafeExpr>(x: T): T;
export declare function and(...args: Code[]): Code;
export declare function or(...args: Code[]): Code;

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,79 @@
import { Code, Name } from "./code";
interface NameGroup {
prefix: string;
index: number;
}
export interface NameValue {
ref: ValueReference;
key?: unknown;
code?: Code;
}
export declare type ValueReference = unknown;
interface ScopeOptions {
prefixes?: Set<string>;
parent?: Scope;
}
interface ValueScopeOptions extends ScopeOptions {
scope: ScopeStore;
es5?: boolean;
lines?: boolean;
}
export declare type ScopeStore = Record<string, ValueReference[] | undefined>;
declare type ScopeValues = {
[Prefix in string]?: Map<unknown, ValueScopeName>;
};
export declare type ScopeValueSets = {
[Prefix in string]?: Set<ValueScopeName>;
};
export declare enum UsedValueState {
Started = 0,
Completed = 1
}
export declare type UsedScopeValues = {
[Prefix in string]?: Map<ValueScopeName, UsedValueState | undefined>;
};
export declare const varKinds: {
const: Name;
let: Name;
var: Name;
};
export declare class Scope {
protected readonly _names: {
[Prefix in string]?: NameGroup;
};
protected readonly _prefixes?: Set<string>;
protected readonly _parent?: Scope;
constructor({ prefixes, parent }?: ScopeOptions);
toName(nameOrPrefix: Name | string): Name;
name(prefix: string): Name;
protected _newName(prefix: string): string;
private _nameGroup;
}
interface ScopePath {
property: string;
itemIndex: number;
}
export declare class ValueScopeName extends Name {
readonly prefix: string;
value?: NameValue;
scopePath?: Code;
constructor(prefix: string, nameStr: string);
setValue(value: NameValue, { property, itemIndex }: ScopePath): void;
}
interface VSOptions extends ValueScopeOptions {
_n: Code;
}
export declare class ValueScope extends Scope {
protected readonly _values: ScopeValues;
protected readonly _scope: ScopeStore;
readonly opts: VSOptions;
constructor(opts: ValueScopeOptions);
get(): ScopeStore;
name(prefix: string): ValueScopeName;
value(nameOrPrefix: ValueScopeName | string, value: NameValue): ValueScopeName;
getValue(prefix: string, keyOrRef: unknown): ValueScopeName | undefined;
scopeRefs(scopeName: Name, values?: ScopeValues | ScopeValueSets): Code;
scopeCode(values?: ScopeValues | ScopeValueSets, usedValues?: UsedScopeValues, getCode?: (n: ValueScopeName) => Code | undefined): Code;
private _reduceValues;
}
export {};

View File

@@ -0,0 +1,143 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValueScope = exports.ValueScopeName = exports.Scope = exports.varKinds = exports.UsedValueState = void 0;
const code_1 = require("./code");
class ValueError extends Error {
constructor(name) {
super(`CodeGen: "code" for ${name} not defined`);
this.value = name.value;
}
}
var UsedValueState;
(function (UsedValueState) {
UsedValueState[UsedValueState["Started"] = 0] = "Started";
UsedValueState[UsedValueState["Completed"] = 1] = "Completed";
})(UsedValueState = exports.UsedValueState || (exports.UsedValueState = {}));
exports.varKinds = {
const: new code_1.Name("const"),
let: new code_1.Name("let"),
var: new code_1.Name("var"),
};
class Scope {
constructor({ prefixes, parent } = {}) {
this._names = {};
this._prefixes = prefixes;
this._parent = parent;
}
toName(nameOrPrefix) {
return nameOrPrefix instanceof code_1.Name ? nameOrPrefix : this.name(nameOrPrefix);
}
name(prefix) {
return new code_1.Name(this._newName(prefix));
}
_newName(prefix) {
const ng = this._names[prefix] || this._nameGroup(prefix);
return `${prefix}${ng.index++}`;
}
_nameGroup(prefix) {
var _a, _b;
if (((_b = (_a = this._parent) === null || _a === void 0 ? void 0 : _a._prefixes) === null || _b === void 0 ? void 0 : _b.has(prefix)) || (this._prefixes && !this._prefixes.has(prefix))) {
throw new Error(`CodeGen: prefix "${prefix}" is not allowed in this scope`);
}
return (this._names[prefix] = { prefix, index: 0 });
}
}
exports.Scope = Scope;
class ValueScopeName extends code_1.Name {
constructor(prefix, nameStr) {
super(nameStr);
this.prefix = prefix;
}
setValue(value, { property, itemIndex }) {
this.value = value;
this.scopePath = (0, code_1._) `.${new code_1.Name(property)}[${itemIndex}]`;
}
}
exports.ValueScopeName = ValueScopeName;
const line = (0, code_1._) `\n`;
class ValueScope extends Scope {
constructor(opts) {
super(opts);
this._values = {};
this._scope = opts.scope;
this.opts = { ...opts, _n: opts.lines ? line : code_1.nil };
}
get() {
return this._scope;
}
name(prefix) {
return new ValueScopeName(prefix, this._newName(prefix));
}
value(nameOrPrefix, value) {
var _a;
if (value.ref === undefined)
throw new Error("CodeGen: ref must be passed in value");
const name = this.toName(nameOrPrefix);
const { prefix } = name;
const valueKey = (_a = value.key) !== null && _a !== void 0 ? _a : value.ref;
let vs = this._values[prefix];
if (vs) {
const _name = vs.get(valueKey);
if (_name)
return _name;
}
else {
vs = this._values[prefix] = new Map();
}
vs.set(valueKey, name);
const s = this._scope[prefix] || (this._scope[prefix] = []);
const itemIndex = s.length;
s[itemIndex] = value.ref;
name.setValue(value, { property: prefix, itemIndex });
return name;
}
getValue(prefix, keyOrRef) {
const vs = this._values[prefix];
if (!vs)
return;
return vs.get(keyOrRef);
}
scopeRefs(scopeName, values = this._values) {
return this._reduceValues(values, (name) => {
if (name.scopePath === undefined)
throw new Error(`CodeGen: name "${name}" has no value`);
return (0, code_1._) `${scopeName}${name.scopePath}`;
});
}
scopeCode(values = this._values, usedValues, getCode) {
return this._reduceValues(values, (name) => {
if (name.value === undefined)
throw new Error(`CodeGen: name "${name}" has no value`);
return name.value.code;
}, usedValues, getCode);
}
_reduceValues(values, valueCode, usedValues = {}, getCode) {
let code = code_1.nil;
for (const prefix in values) {
const vs = values[prefix];
if (!vs)
continue;
const nameSet = (usedValues[prefix] = usedValues[prefix] || new Map());
vs.forEach((name) => {
if (nameSet.has(name))
return;
nameSet.set(name, UsedValueState.Started);
let c = valueCode(name);
if (c) {
const def = this.opts.es5 ? exports.varKinds.var : exports.varKinds.const;
code = (0, code_1._) `${code}${def} ${name} = ${c};${this.opts._n}`;
}
else if ((c = getCode === null || getCode === void 0 ? void 0 : getCode(name))) {
code = (0, code_1._) `${code}${c}${this.opts._n}`;
}
else {
throw new ValueError(name);
}
nameSet.set(name, UsedValueState.Completed);
});
}
return code;
}
}
exports.ValueScope = ValueScope;
//# sourceMappingURL=scope.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,13 @@
import type { KeywordErrorCxt, KeywordErrorDefinition } from "../types";
import { CodeGen, Code, Name } from "./codegen";
export declare const keywordError: KeywordErrorDefinition;
export declare const keyword$DataError: KeywordErrorDefinition;
export interface ErrorPaths {
instancePath?: Code;
schemaPath?: string;
parentSchema?: boolean;
}
export declare function reportError(cxt: KeywordErrorCxt, error?: KeywordErrorDefinition, errorPaths?: ErrorPaths, overrideAllErrors?: boolean): void;
export declare function reportExtraError(cxt: KeywordErrorCxt, error?: KeywordErrorDefinition, errorPaths?: ErrorPaths): void;
export declare function resetErrorsCount(gen: CodeGen, errsCount: Name): void;
export declare function extendErrors({ gen, keyword, schemaValue, data, errsCount, it, }: KeywordErrorCxt): void;

View File

@@ -0,0 +1,123 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extendErrors = exports.resetErrorsCount = exports.reportExtraError = exports.reportError = exports.keyword$DataError = exports.keywordError = void 0;
const codegen_1 = require("./codegen");
const util_1 = require("./util");
const names_1 = require("./names");
exports.keywordError = {
message: ({ keyword }) => (0, codegen_1.str) `must pass "${keyword}" keyword validation`,
};
exports.keyword$DataError = {
message: ({ keyword, schemaType }) => schemaType
? (0, codegen_1.str) `"${keyword}" keyword must be ${schemaType} ($data)`
: (0, codegen_1.str) `"${keyword}" keyword is invalid ($data)`,
};
function reportError(cxt, error = exports.keywordError, errorPaths, overrideAllErrors) {
const { it } = cxt;
const { gen, compositeRule, allErrors } = it;
const errObj = errorObjectCode(cxt, error, errorPaths);
if (overrideAllErrors !== null && overrideAllErrors !== void 0 ? overrideAllErrors : (compositeRule || allErrors)) {
addError(gen, errObj);
}
else {
returnErrors(it, (0, codegen_1._) `[${errObj}]`);
}
}
exports.reportError = reportError;
function reportExtraError(cxt, error = exports.keywordError, errorPaths) {
const { it } = cxt;
const { gen, compositeRule, allErrors } = it;
const errObj = errorObjectCode(cxt, error, errorPaths);
addError(gen, errObj);
if (!(compositeRule || allErrors)) {
returnErrors(it, names_1.default.vErrors);
}
}
exports.reportExtraError = reportExtraError;
function resetErrorsCount(gen, errsCount) {
gen.assign(names_1.default.errors, errsCount);
gen.if((0, codegen_1._) `${names_1.default.vErrors} !== null`, () => gen.if(errsCount, () => gen.assign((0, codegen_1._) `${names_1.default.vErrors}.length`, errsCount), () => gen.assign(names_1.default.vErrors, null)));
}
exports.resetErrorsCount = resetErrorsCount;
function extendErrors({ gen, keyword, schemaValue, data, errsCount, it, }) {
/* istanbul ignore if */
if (errsCount === undefined)
throw new Error("ajv implementation error");
const err = gen.name("err");
gen.forRange("i", errsCount, names_1.default.errors, (i) => {
gen.const(err, (0, codegen_1._) `${names_1.default.vErrors}[${i}]`);
gen.if((0, codegen_1._) `${err}.instancePath === undefined`, () => gen.assign((0, codegen_1._) `${err}.instancePath`, (0, codegen_1.strConcat)(names_1.default.instancePath, it.errorPath)));
gen.assign((0, codegen_1._) `${err}.schemaPath`, (0, codegen_1.str) `${it.errSchemaPath}/${keyword}`);
if (it.opts.verbose) {
gen.assign((0, codegen_1._) `${err}.schema`, schemaValue);
gen.assign((0, codegen_1._) `${err}.data`, data);
}
});
}
exports.extendErrors = extendErrors;
function addError(gen, errObj) {
const err = gen.const("err", errObj);
gen.if((0, codegen_1._) `${names_1.default.vErrors} === null`, () => gen.assign(names_1.default.vErrors, (0, codegen_1._) `[${err}]`), (0, codegen_1._) `${names_1.default.vErrors}.push(${err})`);
gen.code((0, codegen_1._) `${names_1.default.errors}++`);
}
function returnErrors(it, errs) {
const { gen, validateName, schemaEnv } = it;
if (schemaEnv.$async) {
gen.throw((0, codegen_1._) `new ${it.ValidationError}(${errs})`);
}
else {
gen.assign((0, codegen_1._) `${validateName}.errors`, errs);
gen.return(false);
}
}
const E = {
keyword: new codegen_1.Name("keyword"),
schemaPath: new codegen_1.Name("schemaPath"),
params: new codegen_1.Name("params"),
propertyName: new codegen_1.Name("propertyName"),
message: new codegen_1.Name("message"),
schema: new codegen_1.Name("schema"),
parentSchema: new codegen_1.Name("parentSchema"),
};
function errorObjectCode(cxt, error, errorPaths) {
const { createErrors } = cxt.it;
if (createErrors === false)
return (0, codegen_1._) `{}`;
return errorObject(cxt, error, errorPaths);
}
function errorObject(cxt, error, errorPaths = {}) {
const { gen, it } = cxt;
const keyValues = [
errorInstancePath(it, errorPaths),
errorSchemaPath(cxt, errorPaths),
];
extraErrorProps(cxt, error, keyValues);
return gen.object(...keyValues);
}
function errorInstancePath({ errorPath }, { instancePath }) {
const instPath = instancePath
? (0, codegen_1.str) `${errorPath}${(0, util_1.getErrorPath)(instancePath, util_1.Type.Str)}`
: errorPath;
return [names_1.default.instancePath, (0, codegen_1.strConcat)(names_1.default.instancePath, instPath)];
}
function errorSchemaPath({ keyword, it: { errSchemaPath } }, { schemaPath, parentSchema }) {
let schPath = parentSchema ? errSchemaPath : (0, codegen_1.str) `${errSchemaPath}/${keyword}`;
if (schemaPath) {
schPath = (0, codegen_1.str) `${schPath}${(0, util_1.getErrorPath)(schemaPath, util_1.Type.Str)}`;
}
return [E.schemaPath, schPath];
}
function extraErrorProps(cxt, { params, message }, keyValues) {
const { keyword, data, schemaValue, it } = cxt;
const { opts, propertyName, topSchemaRef, schemaPath } = it;
keyValues.push([E.keyword, keyword], [E.params, typeof params == "function" ? params(cxt) : params || (0, codegen_1._) `{}`]);
if (opts.messages) {
keyValues.push([E.message, typeof message == "function" ? message(cxt) : message]);
}
if (opts.verbose) {
keyValues.push([E.schema, schemaValue], [E.parentSchema, (0, codegen_1._) `${topSchemaRef}${schemaPath}`], [names_1.default.data, data]);
}
if (propertyName)
keyValues.push([E.propertyName, propertyName]);
}
//# sourceMappingURL=errors.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,80 @@
import type { AnySchema, AnySchemaObject, AnyValidateFunction, EvaluatedProperties, EvaluatedItems } from "../types";
import type Ajv from "../core";
import type { InstanceOptions } from "../core";
import { CodeGen, Name, Code, ValueScopeName } from "./codegen";
import { LocalRefs } from "./resolve";
import { JSONType } from "./rules";
export declare type SchemaRefs = {
[Ref in string]?: SchemaEnv | AnySchema;
};
export interface SchemaCxt {
readonly gen: CodeGen;
readonly allErrors?: boolean;
readonly data: Name;
readonly parentData: Name;
readonly parentDataProperty: Code | number;
readonly dataNames: Name[];
readonly dataPathArr: (Code | number)[];
readonly dataLevel: number;
dataTypes: JSONType[];
definedProperties: Set<string>;
readonly topSchemaRef: Code;
readonly validateName: Name;
evaluated?: Name;
readonly ValidationError?: Name;
readonly schema: AnySchema;
readonly schemaEnv: SchemaEnv;
readonly rootId: string;
baseId: string;
readonly schemaPath: Code;
readonly errSchemaPath: string;
readonly errorPath: Code;
readonly propertyName?: Name;
readonly compositeRule?: boolean;
props?: EvaluatedProperties | Name;
items?: EvaluatedItems | Name;
jtdDiscriminator?: string;
jtdMetadata?: boolean;
readonly createErrors?: boolean;
readonly opts: InstanceOptions;
readonly self: Ajv;
}
export interface SchemaObjCxt extends SchemaCxt {
readonly schema: AnySchemaObject;
}
interface SchemaEnvArgs {
readonly schema: AnySchema;
readonly schemaId?: "$id" | "id";
readonly root?: SchemaEnv;
readonly baseId?: string;
readonly schemaPath?: string;
readonly localRefs?: LocalRefs;
readonly meta?: boolean;
}
export declare class SchemaEnv implements SchemaEnvArgs {
readonly schema: AnySchema;
readonly schemaId?: "$id" | "id";
readonly root: SchemaEnv;
baseId: string;
schemaPath?: string;
localRefs?: LocalRefs;
readonly meta?: boolean;
readonly $async?: boolean;
readonly refs: SchemaRefs;
readonly dynamicAnchors: {
[Ref in string]?: true;
};
validate?: AnyValidateFunction;
validateName?: ValueScopeName;
serialize?: (data: unknown) => string;
serializeName?: ValueScopeName;
parse?: (data: string) => unknown;
parseName?: ValueScopeName;
constructor(env: SchemaEnvArgs);
}
export declare function compileSchema(this: Ajv, sch: SchemaEnv): SchemaEnv;
export declare function resolveRef(this: Ajv, root: SchemaEnv, baseId: string, ref: string): AnySchema | SchemaEnv | undefined;
export declare function getCompilingSchema(this: Ajv, schEnv: SchemaEnv): SchemaEnv | void;
export declare function resolveSchema(this: Ajv, root: SchemaEnv, // root object with properties schema, refs TODO below SchemaEnv is assigned to it
ref: string): SchemaEnv | undefined;
export {};

View File

@@ -0,0 +1,242 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveSchema = exports.getCompilingSchema = exports.resolveRef = exports.compileSchema = exports.SchemaEnv = void 0;
const codegen_1 = require("./codegen");
const validation_error_1 = require("../runtime/validation_error");
const names_1 = require("./names");
const resolve_1 = require("./resolve");
const util_1 = require("./util");
const validate_1 = require("./validate");
class SchemaEnv {
constructor(env) {
var _a;
this.refs = {};
this.dynamicAnchors = {};
let schema;
if (typeof env.schema == "object")
schema = env.schema;
this.schema = env.schema;
this.schemaId = env.schemaId;
this.root = env.root || this;
this.baseId = (_a = env.baseId) !== null && _a !== void 0 ? _a : (0, resolve_1.normalizeId)(schema === null || schema === void 0 ? void 0 : schema[env.schemaId || "$id"]);
this.schemaPath = env.schemaPath;
this.localRefs = env.localRefs;
this.meta = env.meta;
this.$async = schema === null || schema === void 0 ? void 0 : schema.$async;
this.refs = {};
}
}
exports.SchemaEnv = SchemaEnv;
// let codeSize = 0
// let nodeCount = 0
// Compiles schema in SchemaEnv
function compileSchema(sch) {
// TODO refactor - remove compilations
const _sch = getCompilingSchema.call(this, sch);
if (_sch)
return _sch;
const rootId = (0, resolve_1.getFullPath)(this.opts.uriResolver, sch.root.baseId); // TODO if getFullPath removed 1 tests fails
const { es5, lines } = this.opts.code;
const { ownProperties } = this.opts;
const gen = new codegen_1.CodeGen(this.scope, { es5, lines, ownProperties });
let _ValidationError;
if (sch.$async) {
_ValidationError = gen.scopeValue("Error", {
ref: validation_error_1.default,
code: (0, codegen_1._) `require("ajv/dist/runtime/validation_error").default`,
});
}
const validateName = gen.scopeName("validate");
sch.validateName = validateName;
const schemaCxt = {
gen,
allErrors: this.opts.allErrors,
data: names_1.default.data,
parentData: names_1.default.parentData,
parentDataProperty: names_1.default.parentDataProperty,
dataNames: [names_1.default.data],
dataPathArr: [codegen_1.nil],
dataLevel: 0,
dataTypes: [],
definedProperties: new Set(),
topSchemaRef: gen.scopeValue("schema", this.opts.code.source === true
? { ref: sch.schema, code: (0, codegen_1.stringify)(sch.schema) }
: { ref: sch.schema }),
validateName,
ValidationError: _ValidationError,
schema: sch.schema,
schemaEnv: sch,
rootId,
baseId: sch.baseId || rootId,
schemaPath: codegen_1.nil,
errSchemaPath: sch.schemaPath || (this.opts.jtd ? "" : "#"),
errorPath: (0, codegen_1._) `""`,
opts: this.opts,
self: this,
};
let sourceCode;
try {
this._compilations.add(sch);
(0, validate_1.validateFunctionCode)(schemaCxt);
gen.optimize(this.opts.code.optimize);
// gen.optimize(1)
const validateCode = gen.toString();
sourceCode = `${gen.scopeRefs(names_1.default.scope)}return ${validateCode}`;
// console.log((codeSize += sourceCode.length), (nodeCount += gen.nodeCount))
if (this.opts.code.process)
sourceCode = this.opts.code.process(sourceCode, sch);
// console.log("\n\n\n *** \n", sourceCode)
const makeValidate = new Function(`${names_1.default.self}`, `${names_1.default.scope}`, sourceCode);
const validate = makeValidate(this, this.scope.get());
this.scope.value(validateName, { ref: validate });
validate.errors = null;
validate.schema = sch.schema;
validate.schemaEnv = sch;
if (sch.$async)
validate.$async = true;
if (this.opts.code.source === true) {
validate.source = { validateName, validateCode, scopeValues: gen._values };
}
if (this.opts.unevaluated) {
const { props, items } = schemaCxt;
validate.evaluated = {
props: props instanceof codegen_1.Name ? undefined : props,
items: items instanceof codegen_1.Name ? undefined : items,
dynamicProps: props instanceof codegen_1.Name,
dynamicItems: items instanceof codegen_1.Name,
};
if (validate.source)
validate.source.evaluated = (0, codegen_1.stringify)(validate.evaluated);
}
sch.validate = validate;
return sch;
}
catch (e) {
delete sch.validate;
delete sch.validateName;
if (sourceCode)
this.logger.error("Error compiling schema, function code:", sourceCode);
// console.log("\n\n\n *** \n", sourceCode, this.opts)
throw e;
}
finally {
this._compilations.delete(sch);
}
}
exports.compileSchema = compileSchema;
function resolveRef(root, baseId, ref) {
var _a;
ref = (0, resolve_1.resolveUrl)(this.opts.uriResolver, baseId, ref);
const schOrFunc = root.refs[ref];
if (schOrFunc)
return schOrFunc;
let _sch = resolve.call(this, root, ref);
if (_sch === undefined) {
const schema = (_a = root.localRefs) === null || _a === void 0 ? void 0 : _a[ref]; // TODO maybe localRefs should hold SchemaEnv
const { schemaId } = this.opts;
if (schema)
_sch = new SchemaEnv({ schema, schemaId, root, baseId });
}
if (_sch === undefined)
return;
return (root.refs[ref] = inlineOrCompile.call(this, _sch));
}
exports.resolveRef = resolveRef;
function inlineOrCompile(sch) {
if ((0, resolve_1.inlineRef)(sch.schema, this.opts.inlineRefs))
return sch.schema;
return sch.validate ? sch : compileSchema.call(this, sch);
}
// Index of schema compilation in the currently compiled list
function getCompilingSchema(schEnv) {
for (const sch of this._compilations) {
if (sameSchemaEnv(sch, schEnv))
return sch;
}
}
exports.getCompilingSchema = getCompilingSchema;
function sameSchemaEnv(s1, s2) {
return s1.schema === s2.schema && s1.root === s2.root && s1.baseId === s2.baseId;
}
// resolve and compile the references ($ref)
// TODO returns AnySchemaObject (if the schema can be inlined) or validation function
function resolve(root, // information about the root schema for the current schema
ref // reference to resolve
) {
let sch;
while (typeof (sch = this.refs[ref]) == "string")
ref = sch;
return sch || this.schemas[ref] || resolveSchema.call(this, root, ref);
}
// Resolve schema, its root and baseId
function resolveSchema(root, // root object with properties schema, refs TODO below SchemaEnv is assigned to it
ref // reference to resolve
) {
const p = this.opts.uriResolver.parse(ref);
const refPath = (0, resolve_1._getFullPath)(this.opts.uriResolver, p);
let baseId = (0, resolve_1.getFullPath)(this.opts.uriResolver, root.baseId, undefined);
// TODO `Object.keys(root.schema).length > 0` should not be needed - but removing breaks 2 tests
if (Object.keys(root.schema).length > 0 && refPath === baseId) {
return getJsonPointer.call(this, p, root);
}
const id = (0, resolve_1.normalizeId)(refPath);
const schOrRef = this.refs[id] || this.schemas[id];
if (typeof schOrRef == "string") {
const sch = resolveSchema.call(this, root, schOrRef);
if (typeof (sch === null || sch === void 0 ? void 0 : sch.schema) !== "object")
return;
return getJsonPointer.call(this, p, sch);
}
if (typeof (schOrRef === null || schOrRef === void 0 ? void 0 : schOrRef.schema) !== "object")
return;
if (!schOrRef.validate)
compileSchema.call(this, schOrRef);
if (id === (0, resolve_1.normalizeId)(ref)) {
const { schema } = schOrRef;
const { schemaId } = this.opts;
const schId = schema[schemaId];
if (schId)
baseId = (0, resolve_1.resolveUrl)(this.opts.uriResolver, baseId, schId);
return new SchemaEnv({ schema, schemaId, root, baseId });
}
return getJsonPointer.call(this, p, schOrRef);
}
exports.resolveSchema = resolveSchema;
const PREVENT_SCOPE_CHANGE = new Set([
"properties",
"patternProperties",
"enum",
"dependencies",
"definitions",
]);
function getJsonPointer(parsedRef, { baseId, schema, root }) {
var _a;
if (((_a = parsedRef.fragment) === null || _a === void 0 ? void 0 : _a[0]) !== "/")
return;
for (const part of parsedRef.fragment.slice(1).split("/")) {
if (typeof schema === "boolean")
return;
const partSchema = schema[(0, util_1.unescapeFragment)(part)];
if (partSchema === undefined)
return;
schema = partSchema;
// TODO PREVENT_SCOPE_CHANGE could be defined in keyword def?
const schId = typeof schema === "object" && schema[this.opts.schemaId];
if (!PREVENT_SCOPE_CHANGE.has(part) && schId) {
baseId = (0, resolve_1.resolveUrl)(this.opts.uriResolver, baseId, schId);
}
}
let env;
if (typeof schema != "boolean" && schema.$ref && !(0, util_1.schemaHasRulesButRef)(schema, this.RULES)) {
const $ref = (0, resolve_1.resolveUrl)(this.opts.uriResolver, baseId, schema.$ref);
env = resolveSchema.call(this, root, $ref);
}
// even though resolution failed we need to return SchemaEnv to throw exception
// so that compileAsync loads missing schema.
const { schemaId } = this.opts;
env = env || new SchemaEnv({ schema, schemaId, root, baseId });
if (env.schema !== env.root.schema)
return env;
return undefined;
}
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,4 @@
import type Ajv from "../../core";
import { SchemaObjectMap } from "./types";
import { SchemaEnv } from "..";
export default function compileParser(this: Ajv, sch: SchemaEnv, definitions: SchemaObjectMap): SchemaEnv;

View File

@@ -0,0 +1,350 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("./types");
const __1 = require("..");
const codegen_1 = require("../codegen");
const ref_error_1 = require("../ref_error");
const names_1 = require("../names");
const code_1 = require("../../vocabularies/code");
const ref_1 = require("../../vocabularies/jtd/ref");
const type_1 = require("../../vocabularies/jtd/type");
const parseJson_1 = require("../../runtime/parseJson");
const util_1 = require("../util");
const timestamp_1 = require("../../runtime/timestamp");
const genParse = {
elements: parseElements,
values: parseValues,
discriminator: parseDiscriminator,
properties: parseProperties,
optionalProperties: parseProperties,
enum: parseEnum,
type: parseType,
ref: parseRef,
};
function compileParser(sch, definitions) {
const _sch = __1.getCompilingSchema.call(this, sch);
if (_sch)
return _sch;
const { es5, lines } = this.opts.code;
const { ownProperties } = this.opts;
const gen = new codegen_1.CodeGen(this.scope, { es5, lines, ownProperties });
const parseName = gen.scopeName("parse");
const cxt = {
self: this,
gen,
schema: sch.schema,
schemaEnv: sch,
definitions,
data: names_1.default.data,
parseName,
char: gen.name("c"),
};
let sourceCode;
try {
this._compilations.add(sch);
sch.parseName = parseName;
parserFunction(cxt);
gen.optimize(this.opts.code.optimize);
const parseFuncCode = gen.toString();
sourceCode = `${gen.scopeRefs(names_1.default.scope)}return ${parseFuncCode}`;
const makeParse = new Function(`${names_1.default.scope}`, sourceCode);
const parse = makeParse(this.scope.get());
this.scope.value(parseName, { ref: parse });
sch.parse = parse;
}
catch (e) {
if (sourceCode)
this.logger.error("Error compiling parser, function code:", sourceCode);
delete sch.parse;
delete sch.parseName;
throw e;
}
finally {
this._compilations.delete(sch);
}
return sch;
}
exports.default = compileParser;
const undef = (0, codegen_1._) `undefined`;
function parserFunction(cxt) {
const { gen, parseName, char } = cxt;
gen.func(parseName, (0, codegen_1._) `${names_1.default.json}, ${names_1.default.jsonPos}, ${names_1.default.jsonPart}`, false, () => {
gen.let(names_1.default.data);
gen.let(char);
gen.assign((0, codegen_1._) `${parseName}.message`, undef);
gen.assign((0, codegen_1._) `${parseName}.position`, undef);
gen.assign(names_1.default.jsonPos, (0, codegen_1._) `${names_1.default.jsonPos} || 0`);
gen.const(names_1.default.jsonLen, (0, codegen_1._) `${names_1.default.json}.length`);
parseCode(cxt);
skipWhitespace(cxt);
gen.if(names_1.default.jsonPart, () => {
gen.assign((0, codegen_1._) `${parseName}.position`, names_1.default.jsonPos);
gen.return(names_1.default.data);
});
gen.if((0, codegen_1._) `${names_1.default.jsonPos} === ${names_1.default.jsonLen}`, () => gen.return(names_1.default.data));
jsonSyntaxError(cxt);
});
}
function parseCode(cxt) {
let form;
for (const key of types_1.jtdForms) {
if (key in cxt.schema) {
form = key;
break;
}
}
if (form)
parseNullable(cxt, genParse[form]);
else
parseEmpty(cxt);
}
const parseBoolean = parseBooleanToken(true, parseBooleanToken(false, jsonSyntaxError));
function parseNullable(cxt, parseForm) {
const { gen, schema, data } = cxt;
if (!schema.nullable)
return parseForm(cxt);
tryParseToken(cxt, "null", parseForm, () => gen.assign(data, null));
}
function parseElements(cxt) {
const { gen, schema, data } = cxt;
parseToken(cxt, "[");
const ix = gen.let("i", 0);
gen.assign(data, (0, codegen_1._) `[]`);
parseItems(cxt, "]", () => {
const el = gen.let("el");
parseCode({ ...cxt, schema: schema.elements, data: el });
gen.assign((0, codegen_1._) `${data}[${ix}++]`, el);
});
}
function parseValues(cxt) {
const { gen, schema, data } = cxt;
parseToken(cxt, "{");
gen.assign(data, (0, codegen_1._) `{}`);
parseItems(cxt, "}", () => parseKeyValue(cxt, schema.values));
}
function parseItems(cxt, endToken, block) {
tryParseItems(cxt, endToken, block);
parseToken(cxt, endToken);
}
function tryParseItems(cxt, endToken, block) {
const { gen } = cxt;
gen.for((0, codegen_1._) `;${names_1.default.jsonPos}<${names_1.default.jsonLen} && ${jsonSlice(1)}!==${endToken};`, () => {
block();
tryParseToken(cxt, ",", () => gen.break(), hasItem);
});
function hasItem() {
tryParseToken(cxt, endToken, () => { }, jsonSyntaxError);
}
}
function parseKeyValue(cxt, schema) {
const { gen } = cxt;
const key = gen.let("key");
parseString({ ...cxt, data: key });
parseToken(cxt, ":");
parsePropertyValue(cxt, key, schema);
}
function parseDiscriminator(cxt) {
const { gen, data, schema } = cxt;
const { discriminator, mapping } = schema;
parseToken(cxt, "{");
gen.assign(data, (0, codegen_1._) `{}`);
const startPos = gen.const("pos", names_1.default.jsonPos);
const value = gen.let("value");
const tag = gen.let("tag");
tryParseItems(cxt, "}", () => {
const key = gen.let("key");
parseString({ ...cxt, data: key });
parseToken(cxt, ":");
gen.if((0, codegen_1._) `${key} === ${discriminator}`, () => {
parseString({ ...cxt, data: tag });
gen.assign((0, codegen_1._) `${data}[${key}]`, tag);
gen.break();
}, () => parseEmpty({ ...cxt, data: value }) // can be discarded/skipped
);
});
gen.assign(names_1.default.jsonPos, startPos);
gen.if((0, codegen_1._) `${tag} === undefined`);
parsingError(cxt, (0, codegen_1.str) `discriminator tag not found`);
for (const tagValue in mapping) {
gen.elseIf((0, codegen_1._) `${tag} === ${tagValue}`);
parseSchemaProperties({ ...cxt, schema: mapping[tagValue] }, discriminator);
}
gen.else();
parsingError(cxt, (0, codegen_1.str) `discriminator value not in schema`);
gen.endIf();
}
function parseProperties(cxt) {
const { gen, data } = cxt;
parseToken(cxt, "{");
gen.assign(data, (0, codegen_1._) `{}`);
parseSchemaProperties(cxt);
}
function parseSchemaProperties(cxt, discriminator) {
const { gen, schema, data } = cxt;
const { properties, optionalProperties, additionalProperties } = schema;
parseItems(cxt, "}", () => {
const key = gen.let("key");
parseString({ ...cxt, data: key });
parseToken(cxt, ":");
gen.if(false);
parseDefinedProperty(cxt, key, properties);
parseDefinedProperty(cxt, key, optionalProperties);
if (discriminator) {
gen.elseIf((0, codegen_1._) `${key} === ${discriminator}`);
const tag = gen.let("tag");
parseString({ ...cxt, data: tag }); // can be discarded, it is already assigned
}
gen.else();
if (additionalProperties) {
parseEmpty({ ...cxt, data: (0, codegen_1._) `${data}[${key}]` });
}
else {
parsingError(cxt, (0, codegen_1.str) `property ${key} not allowed`);
}
gen.endIf();
});
if (properties) {
const hasProp = (0, code_1.hasPropFunc)(gen);
const allProps = (0, codegen_1.and)(...Object.keys(properties).map((p) => (0, codegen_1._) `${hasProp}.call(${data}, ${p})`));
gen.if((0, codegen_1.not)(allProps), () => parsingError(cxt, (0, codegen_1.str) `missing required properties`));
}
}
function parseDefinedProperty(cxt, key, schemas = {}) {
const { gen } = cxt;
for (const prop in schemas) {
gen.elseIf((0, codegen_1._) `${key} === ${prop}`);
parsePropertyValue(cxt, key, schemas[prop]);
}
}
function parsePropertyValue(cxt, key, schema) {
parseCode({ ...cxt, schema, data: (0, codegen_1._) `${cxt.data}[${key}]` });
}
function parseType(cxt) {
const { gen, schema, data, self } = cxt;
switch (schema.type) {
case "boolean":
parseBoolean(cxt);
break;
case "string":
parseString(cxt);
break;
case "timestamp": {
parseString(cxt);
const vts = (0, util_1.useFunc)(gen, timestamp_1.default);
const { allowDate, parseDate } = self.opts;
const notValid = allowDate ? (0, codegen_1._) `!${vts}(${data}, true)` : (0, codegen_1._) `!${vts}(${data})`;
const fail = parseDate
? (0, codegen_1.or)(notValid, (0, codegen_1._) `(${data} = new Date(${data}), false)`, (0, codegen_1._) `isNaN(${data}.valueOf())`)
: notValid;
gen.if(fail, () => parsingError(cxt, (0, codegen_1.str) `invalid timestamp`));
break;
}
case "float32":
case "float64":
parseNumber(cxt);
break;
default: {
const t = schema.type;
if (!self.opts.int32range && (t === "int32" || t === "uint32")) {
parseNumber(cxt, 16); // 2 ** 53 - max safe integer
if (t === "uint32") {
gen.if((0, codegen_1._) `${data} < 0`, () => parsingError(cxt, (0, codegen_1.str) `integer out of range`));
}
}
else {
const [min, max, maxDigits] = type_1.intRange[t];
parseNumber(cxt, maxDigits);
gen.if((0, codegen_1._) `${data} < ${min} || ${data} > ${max}`, () => parsingError(cxt, (0, codegen_1.str) `integer out of range`));
}
}
}
}
function parseString(cxt) {
parseToken(cxt, '"');
parseWith(cxt, parseJson_1.parseJsonString);
}
function parseEnum(cxt) {
const { gen, data, schema } = cxt;
const enumSch = schema.enum;
parseToken(cxt, '"');
// TODO loopEnum
gen.if(false);
for (const value of enumSch) {
const valueStr = JSON.stringify(value).slice(1); // remove starting quote
gen.elseIf((0, codegen_1._) `${jsonSlice(valueStr.length)} === ${valueStr}`);
gen.assign(data, (0, codegen_1.str) `${value}`);
gen.add(names_1.default.jsonPos, valueStr.length);
}
gen.else();
jsonSyntaxError(cxt);
gen.endIf();
}
function parseNumber(cxt, maxDigits) {
const { gen } = cxt;
skipWhitespace(cxt);
gen.if((0, codegen_1._) `"-0123456789".indexOf(${jsonSlice(1)}) < 0`, () => jsonSyntaxError(cxt), () => parseWith(cxt, parseJson_1.parseJsonNumber, maxDigits));
}
function parseBooleanToken(bool, fail) {
return (cxt) => {
const { gen, data } = cxt;
tryParseToken(cxt, `${bool}`, () => fail(cxt), () => gen.assign(data, bool));
};
}
function parseRef(cxt) {
const { gen, self, definitions, schema, schemaEnv } = cxt;
const { ref } = schema;
const refSchema = definitions[ref];
if (!refSchema)
throw new ref_error_1.default(self.opts.uriResolver, "", ref, `No definition ${ref}`);
if (!(0, ref_1.hasRef)(refSchema))
return parseCode({ ...cxt, schema: refSchema });
const { root } = schemaEnv;
const sch = compileParser.call(self, new __1.SchemaEnv({ schema: refSchema, root }), definitions);
partialParse(cxt, getParser(gen, sch), true);
}
function getParser(gen, sch) {
return sch.parse
? gen.scopeValue("parse", { ref: sch.parse })
: (0, codegen_1._) `${gen.scopeValue("wrapper", { ref: sch })}.parse`;
}
function parseEmpty(cxt) {
parseWith(cxt, parseJson_1.parseJson);
}
function parseWith(cxt, parseFunc, args) {
partialParse(cxt, (0, util_1.useFunc)(cxt.gen, parseFunc), args);
}
function partialParse(cxt, parseFunc, args) {
const { gen, data } = cxt;
gen.assign(data, (0, codegen_1._) `${parseFunc}(${names_1.default.json}, ${names_1.default.jsonPos}${args ? (0, codegen_1._) `, ${args}` : codegen_1.nil})`);
gen.assign(names_1.default.jsonPos, (0, codegen_1._) `${parseFunc}.position`);
gen.if((0, codegen_1._) `${data} === undefined`, () => parsingError(cxt, (0, codegen_1._) `${parseFunc}.message`));
}
function parseToken(cxt, tok) {
tryParseToken(cxt, tok, jsonSyntaxError);
}
function tryParseToken(cxt, tok, fail, success) {
const { gen } = cxt;
const n = tok.length;
skipWhitespace(cxt);
gen.if((0, codegen_1._) `${jsonSlice(n)} === ${tok}`, () => {
gen.add(names_1.default.jsonPos, n);
success === null || success === void 0 ? void 0 : success(cxt);
}, () => fail(cxt));
}
function skipWhitespace({ gen, char: c }) {
gen.code((0, codegen_1._) `while((${c}=${names_1.default.json}[${names_1.default.jsonPos}],${c}===" "||${c}==="\\n"||${c}==="\\r"||${c}==="\\t"))${names_1.default.jsonPos}++;`);
}
function jsonSlice(len) {
return len === 1
? (0, codegen_1._) `${names_1.default.json}[${names_1.default.jsonPos}]`
: (0, codegen_1._) `${names_1.default.json}.slice(${names_1.default.jsonPos}, ${names_1.default.jsonPos}+${len})`;
}
function jsonSyntaxError(cxt) {
parsingError(cxt, (0, codegen_1._) `"unexpected token " + ${names_1.default.json}[${names_1.default.jsonPos}]`);
}
function parsingError({ gen, parseName }, msg) {
gen.assign((0, codegen_1._) `${parseName}.message`, msg);
gen.assign((0, codegen_1._) `${parseName}.position`, names_1.default.jsonPos);
gen.return(undef);
}
//# sourceMappingURL=parse.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,4 @@
import type Ajv from "../../core";
import { SchemaObjectMap } from "./types";
import { SchemaEnv } from "..";
export default function compileSerializer(this: Ajv, sch: SchemaEnv, definitions: SchemaObjectMap): SchemaEnv;

View File

@@ -0,0 +1,218 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("./types");
const __1 = require("..");
const codegen_1 = require("../codegen");
const ref_error_1 = require("../ref_error");
const names_1 = require("../names");
const code_1 = require("../../vocabularies/code");
const ref_1 = require("../../vocabularies/jtd/ref");
const util_1 = require("../util");
const quote_1 = require("../../runtime/quote");
const genSerialize = {
elements: serializeElements,
values: serializeValues,
discriminator: serializeDiscriminator,
properties: serializeProperties,
optionalProperties: serializeProperties,
enum: serializeString,
type: serializeType,
ref: serializeRef,
};
function compileSerializer(sch, definitions) {
const _sch = __1.getCompilingSchema.call(this, sch);
if (_sch)
return _sch;
const { es5, lines } = this.opts.code;
const { ownProperties } = this.opts;
const gen = new codegen_1.CodeGen(this.scope, { es5, lines, ownProperties });
const serializeName = gen.scopeName("serialize");
const cxt = {
self: this,
gen,
schema: sch.schema,
schemaEnv: sch,
definitions,
data: names_1.default.data,
};
let sourceCode;
try {
this._compilations.add(sch);
sch.serializeName = serializeName;
gen.func(serializeName, names_1.default.data, false, () => {
gen.let(names_1.default.json, (0, codegen_1.str) ``);
serializeCode(cxt);
gen.return(names_1.default.json);
});
gen.optimize(this.opts.code.optimize);
const serializeFuncCode = gen.toString();
sourceCode = `${gen.scopeRefs(names_1.default.scope)}return ${serializeFuncCode}`;
const makeSerialize = new Function(`${names_1.default.scope}`, sourceCode);
const serialize = makeSerialize(this.scope.get());
this.scope.value(serializeName, { ref: serialize });
sch.serialize = serialize;
}
catch (e) {
if (sourceCode)
this.logger.error("Error compiling serializer, function code:", sourceCode);
delete sch.serialize;
delete sch.serializeName;
throw e;
}
finally {
this._compilations.delete(sch);
}
return sch;
}
exports.default = compileSerializer;
function serializeCode(cxt) {
let form;
for (const key of types_1.jtdForms) {
if (key in cxt.schema) {
form = key;
break;
}
}
serializeNullable(cxt, form ? genSerialize[form] : serializeEmpty);
}
function serializeNullable(cxt, serializeForm) {
const { gen, schema, data } = cxt;
if (!schema.nullable)
return serializeForm(cxt);
gen.if((0, codegen_1._) `${data} === undefined || ${data} === null`, () => gen.add(names_1.default.json, (0, codegen_1._) `"null"`), () => serializeForm(cxt));
}
function serializeElements(cxt) {
const { gen, schema, data } = cxt;
gen.add(names_1.default.json, (0, codegen_1.str) `[`);
const first = gen.let("first", true);
gen.forOf("el", data, (el) => {
addComma(cxt, first);
serializeCode({ ...cxt, schema: schema.elements, data: el });
});
gen.add(names_1.default.json, (0, codegen_1.str) `]`);
}
function serializeValues(cxt) {
const { gen, schema, data } = cxt;
gen.add(names_1.default.json, (0, codegen_1.str) `{`);
const first = gen.let("first", true);
gen.forIn("key", data, (key) => serializeKeyValue(cxt, key, schema.values, first));
gen.add(names_1.default.json, (0, codegen_1.str) `}`);
}
function serializeKeyValue(cxt, key, schema, first) {
const { gen, data } = cxt;
addComma(cxt, first);
serializeString({ ...cxt, data: key });
gen.add(names_1.default.json, (0, codegen_1.str) `:`);
const value = gen.const("value", (0, codegen_1._) `${data}${(0, codegen_1.getProperty)(key)}`);
serializeCode({ ...cxt, schema, data: value });
}
function serializeDiscriminator(cxt) {
const { gen, schema, data } = cxt;
const { discriminator } = schema;
gen.add(names_1.default.json, (0, codegen_1.str) `{${JSON.stringify(discriminator)}:`);
const tag = gen.const("tag", (0, codegen_1._) `${data}${(0, codegen_1.getProperty)(discriminator)}`);
serializeString({ ...cxt, data: tag });
gen.if(false);
for (const tagValue in schema.mapping) {
gen.elseIf((0, codegen_1._) `${tag} === ${tagValue}`);
const sch = schema.mapping[tagValue];
serializeSchemaProperties({ ...cxt, schema: sch }, discriminator);
}
gen.endIf();
gen.add(names_1.default.json, (0, codegen_1.str) `}`);
}
function serializeProperties(cxt) {
const { gen } = cxt;
gen.add(names_1.default.json, (0, codegen_1.str) `{`);
serializeSchemaProperties(cxt);
gen.add(names_1.default.json, (0, codegen_1.str) `}`);
}
function serializeSchemaProperties(cxt, discriminator) {
const { gen, schema, data } = cxt;
const { properties, optionalProperties } = schema;
const props = keys(properties);
const optProps = keys(optionalProperties);
const allProps = allProperties(props.concat(optProps));
let first = !discriminator;
for (const key of props) {
serializeProperty(key, properties[key], keyValue(key));
}
for (const key of optProps) {
const value = keyValue(key);
gen.if((0, codegen_1.and)((0, codegen_1._) `${value} !== undefined`, (0, code_1.isOwnProperty)(gen, data, key)), () => serializeProperty(key, optionalProperties[key], value));
}
if (schema.additionalProperties) {
gen.forIn("key", data, (key) => gen.if(isAdditional(key, allProps), () => serializeKeyValue(cxt, key, {}, gen.let("first", first))));
}
function keys(ps) {
return ps ? Object.keys(ps) : [];
}
function allProperties(ps) {
if (discriminator)
ps.push(discriminator);
if (new Set(ps).size !== ps.length) {
throw new Error("JTD: properties/optionalProperties/disciminator overlap");
}
return ps;
}
function keyValue(key) {
return gen.const("value", (0, codegen_1._) `${data}${(0, codegen_1.getProperty)(key)}`);
}
function serializeProperty(key, propSchema, value) {
if (first)
first = false;
else
gen.add(names_1.default.json, (0, codegen_1.str) `,`);
gen.add(names_1.default.json, (0, codegen_1.str) `${JSON.stringify(key)}:`);
serializeCode({ ...cxt, schema: propSchema, data: value });
}
function isAdditional(key, ps) {
return ps.length ? (0, codegen_1.and)(...ps.map((p) => (0, codegen_1._) `${key} !== ${p}`)) : true;
}
}
function serializeType(cxt) {
const { gen, schema, data } = cxt;
switch (schema.type) {
case "boolean":
gen.add(names_1.default.json, (0, codegen_1._) `${data} ? "true" : "false"`);
break;
case "string":
serializeString(cxt);
break;
case "timestamp":
gen.if((0, codegen_1._) `${data} instanceof Date`, () => gen.add(names_1.default.json, (0, codegen_1._) `'"' + ${data}.toISOString() + '"'`), () => serializeString(cxt));
break;
default:
serializeNumber(cxt);
}
}
function serializeString({ gen, data }) {
gen.add(names_1.default.json, (0, codegen_1._) `${(0, util_1.useFunc)(gen, quote_1.default)}(${data})`);
}
function serializeNumber({ gen, data }) {
gen.add(names_1.default.json, (0, codegen_1._) `"" + ${data}`);
}
function serializeRef(cxt) {
const { gen, self, data, definitions, schema, schemaEnv } = cxt;
const { ref } = schema;
const refSchema = definitions[ref];
if (!refSchema)
throw new ref_error_1.default(self.opts.uriResolver, "", ref, `No definition ${ref}`);
if (!(0, ref_1.hasRef)(refSchema))
return serializeCode({ ...cxt, schema: refSchema });
const { root } = schemaEnv;
const sch = compileSerializer.call(self, new __1.SchemaEnv({ schema: refSchema, root }), definitions);
gen.add(names_1.default.json, (0, codegen_1._) `${getSerialize(gen, sch)}(${data})`);
}
function getSerialize(gen, sch) {
return sch.serialize
? gen.scopeValue("serialize", { ref: sch.serialize })
: (0, codegen_1._) `${gen.scopeValue("wrapper", { ref: sch })}.serialize`;
}
function serializeEmpty({ gen, data }) {
gen.add(names_1.default.json, (0, codegen_1._) `JSON.stringify(${data})`);
}
function addComma({ gen }, first) {
gen.if(first, () => gen.assign(first, false), () => gen.add(names_1.default.json, (0, codegen_1.str) `,`));
}
//# sourceMappingURL=serialize.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,6 @@
import type { SchemaObject } from "../../types";
export declare type SchemaObjectMap = {
[Ref in string]?: SchemaObject;
};
export declare const jtdForms: readonly ["elements", "values", "discriminator", "properties", "optionalProperties", "enum", "type", "ref"];
export declare type JTDForm = typeof jtdForms[number];

View File

@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.jtdForms = void 0;
exports.jtdForms = [
"elements",
"values",
"discriminator",
"properties",
"optionalProperties",
"enum",
"type",
"ref",
];
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../lib/compile/jtd/types.ts"],"names":[],"mappings":";;;AAIa,QAAA,QAAQ,GAAG;IACtB,UAAU;IACV,QAAQ;IACR,eAAe;IACf,YAAY;IACZ,oBAAoB;IACpB,MAAM;IACN,MAAM;IACN,KAAK;CACG,CAAA"}

View File

@@ -0,0 +1,20 @@
import { Name } from "./codegen";
declare const names: {
data: Name;
valCxt: Name;
instancePath: Name;
parentData: Name;
parentDataProperty: Name;
rootData: Name;
dynamicAnchors: Name;
vErrors: Name;
errors: Name;
this: Name;
self: Name;
scope: Name;
json: Name;
jsonPos: Name;
jsonLen: Name;
jsonPart: Name;
};
export default names;

View File

@@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const codegen_1 = require("./codegen");
const names = {
// validation function arguments
data: new codegen_1.Name("data"),
// args passed from referencing schema
valCxt: new codegen_1.Name("valCxt"),
instancePath: new codegen_1.Name("instancePath"),
parentData: new codegen_1.Name("parentData"),
parentDataProperty: new codegen_1.Name("parentDataProperty"),
rootData: new codegen_1.Name("rootData"),
dynamicAnchors: new codegen_1.Name("dynamicAnchors"),
// function scoped variables
vErrors: new codegen_1.Name("vErrors"),
errors: new codegen_1.Name("errors"),
this: new codegen_1.Name("this"),
// "globals"
self: new codegen_1.Name("self"),
scope: new codegen_1.Name("scope"),
// JTD serialize/parse name for JSON string and position
json: new codegen_1.Name("json"),
jsonPos: new codegen_1.Name("jsonPos"),
jsonLen: new codegen_1.Name("jsonLen"),
jsonPart: new codegen_1.Name("jsonPart"),
};
exports.default = names;
//# sourceMappingURL=names.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"names.js","sourceRoot":"","sources":["../../lib/compile/names.ts"],"names":[],"mappings":";;AAAA,uCAA8B;AAE9B,MAAM,KAAK,GAAG;IACZ,gCAAgC;IAChC,IAAI,EAAE,IAAI,cAAI,CAAC,MAAM,CAAC;IACtB,sCAAsC;IACtC,MAAM,EAAE,IAAI,cAAI,CAAC,QAAQ,CAAC;IAC1B,YAAY,EAAE,IAAI,cAAI,CAAC,cAAc,CAAC;IACtC,UAAU,EAAE,IAAI,cAAI,CAAC,YAAY,CAAC;IAClC,kBAAkB,EAAE,IAAI,cAAI,CAAC,oBAAoB,CAAC;IAClD,QAAQ,EAAE,IAAI,cAAI,CAAC,UAAU,CAAC;IAC9B,cAAc,EAAE,IAAI,cAAI,CAAC,gBAAgB,CAAC;IAC1C,4BAA4B;IAC5B,OAAO,EAAE,IAAI,cAAI,CAAC,SAAS,CAAC;IAC5B,MAAM,EAAE,IAAI,cAAI,CAAC,QAAQ,CAAC;IAC1B,IAAI,EAAE,IAAI,cAAI,CAAC,MAAM,CAAC;IACtB,YAAY;IACZ,IAAI,EAAE,IAAI,cAAI,CAAC,MAAM,CAAC;IACtB,KAAK,EAAE,IAAI,cAAI,CAAC,OAAO,CAAC;IACxB,wDAAwD;IACxD,IAAI,EAAE,IAAI,cAAI,CAAC,MAAM,CAAC;IACtB,OAAO,EAAE,IAAI,cAAI,CAAC,SAAS,CAAC;IAC5B,OAAO,EAAE,IAAI,cAAI,CAAC,SAAS,CAAC;IAC5B,QAAQ,EAAE,IAAI,cAAI,CAAC,UAAU,CAAC;CAC/B,CAAA;AAED,kBAAe,KAAK,CAAA"}

Some files were not shown because too many files have changed in this diff Show More