diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index c77c8e485..88fc551fa 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,2 +1,3 @@ +github: epoberezkin tidelift: "npm/ajv" open_collective: "ajv" diff --git a/.github/ISSUE_TEMPLATE/typescript.md b/.github/ISSUE_TEMPLATE/typescript.md new file mode 100644 index 000000000..7385349b0 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/typescript.md @@ -0,0 +1,42 @@ +--- +name: Missing or incorrect type definition +about: Please use for issues related to typescript types +title: '' +labels: 'typescript' +assignees: '' + +--- + + + +**What version of Ajv are you using? Does the issue happen if you use the latest version?** + + +**Your typescript code** + + + +```typescript + + +``` + + +**Typescript compiler error messages** + +``` + + +``` + +**Describe the change that should be made to address the issue?** + + +**Are you going to resolve the issue?** diff --git a/FAQ.md b/FAQ.md index 472a5edb0..36ac967cd 100644 --- a/FAQ.md +++ b/FAQ.md @@ -83,7 +83,7 @@ There are several ways to implement the described logic that would allow two pro ##### Why the validation fails when I use option `removeAdditional` with the keyword `anyOf`/etc.? -This problem is related to the problem explained above - properties treated as additional in the sence of `additionalProperties` keyword, based on `properties`/`patternProperties` keyword in the same schema object. +This problem is related to the problem explained above - properties treated as additional in the sense of `additionalProperties` keyword, based on `properties`/`patternProperties` keyword in the same schema object. See the exemple in [Filtering Data](https://github.com/epoberezkin/ajv#filtering-data) section of readme. diff --git a/README.md b/README.md index be013ebb0..75e5c850b 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,49 @@ The fastest JSON Schema validator for Node.js and browser. Supports draft-04/06/ [![Financial Contributors on Open Collective](https://opencollective.com/ajv/all/badge.svg?label=financial+contributors)](https://opencollective.com/ajv) [![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/epoberezkin/ajv/badge.svg?branch=master&service=github)](https://coveralls.io/github/epoberezkin/ajv?branch=master) -[![Greenkeeper badge](https://badges.greenkeeper.io/epoberezkin/ajv.svg)](https://greenkeeper.io/) [![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) + +## Please [sponsor Ajv](https://github.com/sponsors/epoberezkin) + +Dear Ajv users! ❤️ + +I ask you to support the development of Ajv with donations. 🙏 + +Since 2015 Ajv has become widely used, thanks to your help and contributions: + +- **90** contributors 🏗 +- **5,000** dependent npm packages ⚙️ +- **7,000** github stars, from GitHub users [all over the world](https://www.google.com/maps/d/u/0/viewer?mid=1MGRV8ciFUGIbO1l0EKFWNJGYE7iSkDxP&ll=-3.81666561775622e-14%2C4.821737100000007&z=2) ⭐️ +- **5,000,000** dependent repositories on GitHub 🚀 +- **120,000,000** npm downloads per month! 💯 + +Your donations will fund futher development - small and large improvements, support of the next versions of JSON Schema specification, and, possibly, the code should be migrated to TypeScript to make it more maintainable. + +I will greatly appreciate anything you can help with to make it happen: + +- a **personal** donation - from $2 ☕️ +- your **company** donation - from $10 🍔 +- a **sponsorship** to get promoted on Ajv or related packages - from $50 💰 +- an **introduction** to a sponsor who would benefit from the promotion on Ajv page 🤝 + +| Please [make donations via my GitHub sponsors page](https://github.com/sponsors/epoberezkin)
‼️ **GitHub will DOUBLE them** ‼️ | +|---| + +#### Open Collective sponsors + + + + + + + + + + + + + ## Using version 6 @@ -131,7 +172,11 @@ Try it in the Node.js REPL: https://tonicdev.com/npm/ajv The fastest validation call: ```javascript +// Node.js require: var Ajv = require('ajv'); +// or ESM/TypeScript import +import Ajv from 'ajv'; + var ajv = new Ajv(); // options can be passed, e.g. {allErrors: true} var validate = ajv.compile(schema); var valid = validate(data); @@ -165,6 +210,10 @@ The best performance is achieved when using compiled functions returned by `comp __Please note__: every time a validation function or `ajv.validate` are called `errors` property is overwritten. You need to copy `errors` array reference to another variable if you want to use it later (e.g., in the callback). See [Validation errors](#validation-errors) +__Note for TypeScript users__: `ajv` provides its own TypeScript declarations +out of the box, so you don't need to install the deprecated `@types/ajv` +module. + ## Using in browser @@ -328,7 +377,7 @@ __Please note__: ## $data reference -With `$data` option you can use values from the validated data as the values for the schema keywords. See [proposal](https://github.com/json-schema/json-schema/wiki/$data-(v5-proposal)) for more information about how it works. +With `$data` option you can use values from the validated data as the values for the schema keywords. See [proposal](https://github.com/json-schema-org/json-schema-spec/issues/51) for more information about how it works. `$data` reference is supported in the keywords: const, enum, format, maximum/minimum, exclusiveMaximum / exclusiveMinimum, maxLength / minLength, maxItems / minItems, maxProperties / minProperties, formatMaximum / formatMinimum, formatExclusiveMaximum / formatExclusiveMinimum, multipleOf, pattern, required, uniqueItems. @@ -1144,12 +1193,13 @@ Defaults: - `"full"` - more restrictive and slow validation. E.g., 25:00:00 and 2015/14/33 will be invalid time and date in 'full' mode but it will be valid in 'fast' mode. - `false` - ignore all format keywords. - _formats_: an object with custom formats. Keys and values will be passed to `addFormat` method. +- _keywords_: an object with custom keywords. Keys and values will be passed to `addKeyword` method. - _unknownFormats_: handling of unknown formats. Option values: - `true` (default) - if an unknown format is encountered the exception is thrown during schema compilation. If `format` keyword value is [$data reference](#data-reference) and it is unknown the validation will fail. - `[String]` - an array of unknown format names that will be ignored. This option can be used to allow usage of third party schemas with format(s) for which you don't have definitions, but still fail if another unknown format is used. If `format` keyword value is [$data reference](#data-reference) and it is not in this array the validation will fail. - `"ignore"` - to log warning during schema compilation and always pass validation (the default behaviour in versions before 5.0.0). This option is not recommended, as it allows to mistype format name and it won't be validated without any error message. This behaviour is required by JSON Schema specification. - _schemas_: an array or object of schemas that will be added to the instance. In case you pass the array the schemas must have IDs in them. When the object is passed the method `addSchema(value, key)` will be called for each schema in this object. -- _logger_: sets the logging method. Default is the global `console` object that should have methods `log`, `warn` and `error`. Option values: +- _logger_: sets the logging method. Default is the global `console` object that should have methods `log`, `warn` and `error`. See [Error logging](#error-logging). Option values: - custom logger - it should have methods `log`, `warn` and `error`. If any of these methods is missing an exception will be thrown. - `false` - logging is disabled. @@ -1287,6 +1337,28 @@ Properties of `params` object in errors depend on the keyword that failed valida - custom keywords (in case keyword definition doesn't create errors) - property `keyword` (the keyword name). +### Error logging + +Using the `logger` option when initiallizing Ajv will allow you to define custom logging. Here you can build upon the exisiting logging. The use of other logging packages is supported as long as the package or its associated wrapper exposes the required methods. If any of the required methods are missing an exception will be thrown. +- **Required Methods**: `log`, `warn`, `error` + +```javascript +var otherLogger = new OtherLogger(); +var ajv = new Ajv({ + logger: { + log: console.log.bind(console), + warn: function warn() { + otherLogger.logWarn.apply(otherLogger, arguments); + }, + error: function error() { + otherLogger.logError.apply(otherLogger, arguments); + console.error.apply(console, arguments); + } + } +}); +``` + + ## Plugins Ajv can be extended with plugins that add custom keywords, formats or functions to process generated code. When such plugin is published as npm package it is recommended that it follows these conventions: @@ -1309,7 +1381,7 @@ If you have published a useful plugin please submit a PR to add it to the next s - [ajv-keywords](https://github.com/epoberezkin/ajv-keywords) - plugin with custom validation keywords (select, typeof, etc.) - [ajv-merge-patch](https://github.com/epoberezkin/ajv-merge-patch) - plugin with keywords $merge and $patch - [ajv-pack](https://github.com/epoberezkin/ajv-pack) - produces a compact module exporting validation functions - +- [ajv-formats-draft2019](https://github.com/luzlab/ajv-formats-draft2019) - format validators for draft2019 that aren't already included in ajv (ie. `idn-hostname`, `idn-email`, `iri`, `iri-reference` and `duration`). ## Some packages using Ajv diff --git a/lib/ajv.d.ts b/lib/ajv.d.ts index 5e7cfa7da..cb600e168 100644 --- a/lib/ajv.d.ts +++ b/lib/ajv.d.ts @@ -80,9 +80,9 @@ declare namespace ajv { /** * Get compiled schema from the instance by `key` or `ref`. * @param {string} keyRef `key` that was passed to `addSchema` or full schema reference (`schema.id` or resolved id). - * @return {Function} schema validating function (with property `schema`). + * @return {Function} schema validating function (with property `schema`). Returns undefined if keyRef can't be resolved to an existing schema. */ - getSchema(keyRef: string): ValidateFunction; + getSchema(keyRef: string): ValidateFunction | undefined; /** * Remove cached schema(s). * If no parameter is passed all schemas but meta-schemas are removed. @@ -169,8 +169,9 @@ declare namespace ajv { jsonPointers?: boolean; uniqueItems?: boolean; unicode?: boolean; - format?: string; + format?: false | string; formats?: object; + keywords?: object; unknownFormats?: true | string[] | 'ignore'; schemas?: Array | object; schemaId?: '$id' | 'id' | 'auto'; @@ -243,6 +244,7 @@ declare namespace ajv { interface CompilationContext { level: number; dataLevel: number; + dataPathArr: string[]; schema: any; schemaPath: string; baseId: string; @@ -251,6 +253,9 @@ declare namespace ajv { formats: { [index: string]: FormatDefinition | undefined; }; + keywords: { + [index: string]: KeywordDefinition | undefined; + }; compositeRule: boolean; validate: (schema: object) => boolean; util: { diff --git a/lib/ajv.js b/lib/ajv.js index 611b93835..06a45b650 100644 --- a/lib/ajv.js +++ b/lib/ajv.js @@ -69,6 +69,7 @@ function Ajv(opts) { this._metaOpts = getMetaSchemaOptions(this); if (opts.formats) addInitialFormats(this); + if (opts.keywords) addInitialKeywords(this); addDefaultMetaSchema(this); if (typeof opts.meta == 'object') this.addMetaSchema(opts.meta); if (opts.nullable) this.addKeyword('nullable', {metaSchema: {type: 'boolean'}}); @@ -467,6 +468,14 @@ function addInitialFormats(self) { } +function addInitialKeywords(self) { + for (var name in self._opts.keywords) { + var keyword = self._opts.keywords[name]; + self.addKeyword(name, keyword); + } +} + + function checkUnique(self, id) { if (self._schemas[id] || self._refs[id]) throw new Error('schema with key or id "' + id + '" already exists'); diff --git a/lib/compile/formats.js b/lib/compile/formats.js index d06792aa4..44895b0b4 100644 --- a/lib/compile/formats.js +++ b/lib/compile/formats.js @@ -5,7 +5,7 @@ var util = require('./util'); var DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/; var DAYS = [0,31,28,31,30,31,30,31,31,30,31,30,31]; var TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d(?::?\d\d)?)?$/i; -var HOSTNAME = /^[a-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=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*)?(?:\?(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i; // uri-template: https://tools.ietf.org/html/rfc6570 @@ -70,7 +70,7 @@ formats.full = { 'uri-template': URITEMPLATE, url: URL, email: /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i, - hostname: hostname, + hostname: HOSTNAME, ipv4: /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/, ipv6: /^\s*(?:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a-f]{1,4}|:))|(?:(?:[0-9a-f]{1,4}:){6}(?::[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){5}(?:(?:(?::[0-9a-f]{1,4}){1,2})|:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(?:(?:[0-9a-f]{1,4}:){4}(?:(?:(?::[0-9a-f]{1,4}){1,3})|(?:(?::[0-9a-f]{1,4})?:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){3}(?:(?:(?::[0-9a-f]{1,4}){1,4})|(?:(?::[0-9a-f]{1,4}){0,2}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){2}(?:(?:(?::[0-9a-f]{1,4}){1,5})|(?:(?::[0-9a-f]{1,4}){0,3}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?:(?:[0-9a-f]{1,4}:){1}(?:(?:(?::[0-9a-f]{1,4}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(?:%.+)?\s*$/i, regex: regex, @@ -123,13 +123,6 @@ function date_time(str) { } -function hostname(str) { - // https://tools.ietf.org/html/rfc1034#section-3.5 - // https://tools.ietf.org/html/rfc1123#section-2 - return str.length <= 255 && HOSTNAME.test(str); -} - - var NOT_URI_FRAGMENT = /\/|:/; function uri(str) { // http://jmrware.com/articles/2009/uri_regexp/URI_regex.html + optional protocol + required "." diff --git a/package.json b/package.json index c2f5240d8..4642ba914 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ajv", - "version": "6.10.2", + "version": "6.12.0", "description": "Another JSON Schema Validator", "main": "lib/ajv.js", "typings": "lib/ajv.d.ts", @@ -25,7 +25,7 @@ "build": "del-cli lib/dotjs/*.js \"!lib/dotjs/index.js\" && node scripts/compile-dots.js", "test-karma": "karma start", "test-browser": "del-cli .browser && npm run bundle && scripts/prepare-tests && npm run test-karma", - "test-all": "npm run test-ts && npm run test-cov && if-node-version 10 npm run test-browser", + "test-all": "npm run test-cov && if-node-version 10 npm run test-browser", "test": "npm run lint && npm run build && npm run test-all", "prepublish": "npm run build && npm run bundle", "watch": "watch \"npm run build\" ./lib/dot", @@ -63,7 +63,7 @@ "homepage": "https://github.com/epoberezkin/ajv", "tonicExampleFilename": ".tonic_example.js", "dependencies": { - "fast-deep-equal": "^2.0.1", + "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", "json-schema-traverse": "^0.4.1", "opencollective-postinstall": "^2.0.2", @@ -76,7 +76,7 @@ "browserify": "^16.2.0", "chai": "^4.0.1", "coveralls": "^3.0.1", - "del-cli": "^2.0.0", + "del-cli": "^3.0.0", "dot": "^1.0.3", "eslint": "^6.0.0", "gh-pages-generator": "^0.2.3", @@ -89,12 +89,12 @@ "karma-chrome-launcher": "^3.0.0", "karma-mocha": "^1.1.1", "karma-sauce-launcher": "^2.0.0", - "mocha": "^6.0.0", - "nyc": "^14.0.0", + "mocha": "^7.0.1", + "nyc": "^15.0.0", "pre-commit": "^1.1.1", "require-globify": "^1.3.0", "typescript": "^2.8.3", - "uglify-js": "^3.3.24", + "uglify-js": "^3.6.9", "watch": "^1.0.0" }, "collective": { diff --git a/spec/options/options_validation.spec.js b/spec/options/options_validation.spec.js index e362826bc..950a0c289 100644 --- a/spec/options/options_validation.spec.js +++ b/spec/options/options_validation.spec.js @@ -26,7 +26,29 @@ describe('validation options', function() { }}); var validate = ajv.compile({ format: 'identifier' }); + + validate('Abc1') .should.equal(true); + validate('foo bar') .should.equal(false); + validate('123') .should.equal(false); + validate(123) .should.equal(true); + }); + }); + + describe('keywords', function() { + it('should add keywords from options', function() { + var ajv = new Ajv({ keywords: { + identifier: { + type: 'string', + validate: function (schema, data ) { + return /^[a-z_$][a-z0-9_$]*$/i.test(data); + } + } + }}); + + var validate = ajv.compile({ identifier: true }); + validate('Abc1') .should.equal(true); + validate('foo bar') .should.equal(false); validate('123') .should.equal(false); validate(123) .should.equal(true); }); diff --git a/spec/tests/rules/format.json b/spec/tests/rules/format.json index 226608bd4..c316a6ee5 100644 --- a/spec/tests/rules/format.json +++ b/spec/tests/rules/format.json @@ -82,10 +82,55 @@ "data": "123.example.com", "valid": true }, + { + "description": "valid hostname - trailing dot", + "data": "123.example.com.", + "valid": true + }, + { + "description": "valid hostname - single label", + "data": "localhost", + "valid": true + }, + { + "description": "valid hostname - single label with trailing dot", + "data": "localhost.", + "valid": true + }, { "description": "valid hostname #312", "data": "lead-routing-qa.lvuucj.0001.use1.cache.amazonaws.com", "valid": true + }, + { + "description": "valid hostname - maximum length label (63 chars)", + "data": "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk.example.com", + "valid": true + }, + { + "description": "invalid hostname - label too long (64 chars)", + "data": "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl.example.com", + "valid": false + }, + { + "description": "valid hostname - maximum length hostname (255 octets)", + "data": "abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy.example.com", + "valid": true + }, + { + "description": "valid hostname - maximum length hostname (255 octets) with trailing dot", + "data": "abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy.example.com.", + "valid": true + }, + { + "description": "invalid hostname - hostname too long (256 octets)", + "data": "abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.example.com", + "valid": false + }, + { + "description": "invalid hostname - hostname too long (256 octets) with trailing dot", + "data": "abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.example.com.", + "valid": false } ] },