Skip to content
Permalink

Comparing changes

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

Open a pull request

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

Commits on Apr 3, 2021

  1. Add unicodeRegExp option to pass or remove RegExp unicode flag from p…

    …attern and patternProperties
    asaid-0 committed Apr 3, 2021
    Copy the full SHA
    a385dbc View commit details
  2. Unverified

    The email in this signature doesn’t match the committer email.
    Copy the full SHA
    80b9c70 View commit details

Commits on Apr 7, 2021

  1. 1

    Verified

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

Commits on Apr 8, 2021

  1. fix #1537

    erikbrinkman committed Apr 8, 2021

    Verified

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

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    6e8440f View commit details
  3. Re-formatted test file

    pcwiek committed Apr 8, 2021
    Copy the full SHA
    6508a03 View commit details

Commits on Apr 11, 2021

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    27f056f View commit details
  2. Copy the full SHA
    c6f5e3c View commit details
  3. Verified

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

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

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

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

    epoberezkin committed Apr 11, 2021
    Copy the full SHA
    df07f66 View commit details
16 changes: 8 additions & 8 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<!--
Frequently Asked Questions: https://github.com/ajv-validator/ajv/blob/master/docs/faq.md
Frequently Asked Questions: https://ajv.js.org/faq.html
Please provide all info and reduce your schema and data to the smallest possible size.
This template is for bug or error reports. For other issues please use:
- security vulnerability: https://tidelift.com/security)
- a new feature/improvement: https://ajv.js.org/contribute.html#changes
- browser/compatibility issues: https://ajv.js.org/contribute.html#compatibility
- JSON-Schema standard: https://ajv.js.org/contribute.html#json-schema
- a new feature/improvement: https://ajv.js.org/contributing/#changes
- browser/compatibility issues: https://ajv.js.org/contributing/#compatibility
- JSON-Schema standard: https://ajv.js.org/contributing/#json-schema
- Ajv usage questions: https://gitter.im/ajv-validator/ajv
-->

**What version of Ajv are you using? Does the issue happen if you use the latest version?**

**Ajv options object**

<!-- See https://github.com/ajv-validator/ajv/api.md#options -->
<!-- See https://ajv.js.org/options.html -->

```javascript

@@ -30,7 +30,7 @@ This template is for bug or error reports. For other issues please use:

**Sample data**

<!-- Please make it as small as posssible to reproduce the issue -->
<!-- Please make it as small as possible to reproduce the issue -->

```json

@@ -40,8 +40,8 @@ This template is for bug or error reports. For other issues please use:

<!--
Please:
- make it as small as posssible to reproduce the issue
- use one of the usage patterns from https://github.com/ajv-validator/ajv#getting-started
- make it as small as possible to reproduce the issue
- use one of the usage patterns from https://ajv.js.org/guide/getting-started.html
- use `options`, `schema` and `data` as variables, do not repeat their values here
- post a working code sample in RunKit notebook cloned from https://runkit.com/esp/ajv-issue and include the link here.
12 changes: 6 additions & 6 deletions .github/ISSUE_TEMPLATE/bug-or-error-report.md
Original file line number Diff line number Diff line change
@@ -7,18 +7,18 @@ assignees: ""
---

<!--
Frequently Asked Questions: https://github.com/ajv-validator/ajv/blob/master/docs/faq.md
Frequently Asked Questions: https://ajv.js.org/faq.html
Please provide all info and reduce your schema and data to the smallest possible size.
This template is for bug or error reports.
For other issues please see https://github.com/ajv-validator/ajv/blob/master/CONTRIBUTING.md
For other issues please see https://ajv.js.org/contributing/
-->

**What version of Ajv are you using? Does the issue happen if you use the latest version?**

**Ajv options object**

<!-- See https://github.com/ajv-validator/ajv/api.md/api.md#options -->
<!-- See https://ajv.js.org/options.html -->

```javascript

@@ -34,7 +34,7 @@ For other issues please see https://github.com/ajv-validator/ajv/blob/master/CON

**Sample data**

<!-- Please make it as small as posssible to reproduce the issue -->
<!-- Please make it as small as possible to reproduce the issue -->

```json

@@ -44,8 +44,8 @@ For other issues please see https://github.com/ajv-validator/ajv/blob/master/CON

<!--
Please:
- make it as small as posssible to reproduce the issue
- use one of the usage patterns from https://github.com/ajv-validator/ajv#getting-started
- make it as small as possible to reproduce the issue
- use one of the usage patterns from https://ajv.js.org/guide/getting-started.html
- use `options`, `schema` and `data` as variables, do not repeat their values here
- post a working code sample in RunKit notebook cloned from https://runkit.com/esp/ajv-issue and include the link here.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/change.md
Original file line number Diff line number Diff line change
@@ -7,11 +7,11 @@ assignees: ""
---

<!--
Frequently Asked Questions: https://github.com/ajv-validator/ajv/blob/master/docs/faq.md
Frequently Asked Questions: https://ajv.js.org/faq.html
Please provide all info and reduce your schema and data to the smallest possible size.
This template is for change proposals.
For other issues please see https://github.com/ajv-validator/ajv/blob/master/CONTRIBUTING.md
For other issues please see https://ajv.js.org/contributing/
-->

**What version of Ajv you are you using?**
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/compatibility.md
Original file line number Diff line number Diff line change
@@ -7,11 +7,11 @@ assignees: ""
---

<!--
Frequently Asked Questions: https://github.com/ajv-validator/ajv/blob/master/docs/faq.md
Frequently Asked Questions: https://ajv.js.org/faq.html
Please provide all info and reduce your schema and data to the smallest possible size.
This template is for compatibility issues.
For other issues please see https://github.com/ajv-validator/ajv/blob/master/CONTRIBUTING.md
For other issues please see https://ajv.js.org/contributing/
-->

**The version of Ajv you are using**
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/installation.md
Original file line number Diff line number Diff line change
@@ -7,11 +7,11 @@ assignees: ""
---

<!--
Frequently Asked Questions: https://github.com/ajv-validator/ajv/blob/master/docs/faq.md
Frequently Asked Questions: https://ajv.js.org/faq.html
Please provide all info and reduce your schema and data to the smallest possible size.
This template is for installation and dependency issues.
For other issues please see https://github.com/ajv-validator/ajv/blob/master/CONTRIBUTING.md
For other issues please see https://ajv.js.org/contributing/
Before submitting the issue, please try the following:
- use the latest stable Node.js and npm
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/typescript.md
Original file line number Diff line number Diff line change
@@ -7,18 +7,18 @@ assignees: ""
---

<!--
Frequently Asked Questions: https://github.com/ajv-validator/ajv/blob/master/docs/faq.md
Frequently Asked Questions: https://ajv.js.org/faq.html
This template is for issues about missing or incorrect type definition and other typescript-related issues.
For other issues please see https://github.com/ajv-validator/ajv/blob/master/CONTRIBUTING.md
For other issues please see https://ajv.js.org/contributing/
-->

**What version of Ajv are you using? Does the issue happen if you use the latest version?**

**Your typescript code**

<!--
Please make it as small as posssible to reproduce the issue
Please make it as small as possible to reproduce the issue
-->

```typescript
20 changes: 15 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -58,7 +58,9 @@ Tidelift will coordinate the fix and disclosure.

Please do NOT report security vulnerabilities via GitHub issues.

### <a name="changes"></a>Change proposals
<a name="changes"></a>

### Change proposals

[Create a proposal](https://github.com/ajv-validator/ajv/issues/new?template=change.md) for a new feature, option or some other improvement.

@@ -77,7 +79,9 @@ If you’re requesting a change, it would be helpful to include this as well:

Please include as much details as possible - the more information, the better.

### <a name="compatibility"></a>Browser and compatibility issues
<a name="compatibility"></a>

### Browser and compatibility issues

[Create an issue](https://github.com/ajv-validator/ajv/issues/new?template=compatibility.md) to report a compatibility problem that only happens in a particular environment (when your code works correctly in the latest stable Node.js in linux systems but fails in some other environment).

@@ -90,7 +94,9 @@ Please include this information:
5. Results in the latest stable Node.js.
6. Results and error messages in your platform.

### <a name="installation"></a>Installation and dependency issues
<a name="installation"></a>

### Installation and dependency issues

[Create an issue](https://github.com/ajv-validator/ajv/issues/new?template=installation.md) to report problems that happen during Ajv installation or when Ajv is missing some dependency.

@@ -109,13 +115,17 @@ If nothing helps, please submit:
5. Error messages
6. The output of `npm ls`

### <a name="json-schema"></a>Using JSON Schema standard
<a name="json-schema"></a>

### Using JSON Schema standard

Ajv implements JSON Schema standard draft-04 and draft-06/07.

If it is a general issue related to using the standard keywords included in JSON Schema specification or implementing some advanced validation logic please ask the question on [Stack Overflow](https://stackoverflow.com/questions/ask?tags=jsonschema,ajv) (my account is [esp](https://stackoverflow.com/users/1816503/esp)) or submit the question to [json-schema.org](https://github.com/json-schema-org/json-schema-spec/issues/new). Please mention @epoberezkin.

### <a name="usage"></a>Ajv usage questions
<a name="usage"></a>

### Ajv usage questions

The best place to ask a question about using Ajv is [Gitter chat](https://gitter.im/ajv-validator/ajv).

10 changes: 10 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ const defaultOptions = {
allErrors: false,
verbose: false,
discriminator: false, // *
unicodeRegExp: true // *
$comment: false, // *
formats: {},
keywords: {},
@@ -167,6 +168,15 @@ Include the reference to the part of the schema (`schema` and `parentSchema`) an

Support [discriminator keyword](./json-schema.md#discriminator) from [OpenAPI specification](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md).

### unicodeRegExp

By default Ajv uses unicode flag "u" with "pattern" and "patternProperties", as per JSON Schema spec. See [RegExp.prototype.unicode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode) .

Option values:

- `true` (default) - use unicode flag "u".
- `false` - do not use flag "u".

### $comment

Log or pass the value of `$comment` keyword to a function.
6 changes: 4 additions & 2 deletions lib/compile/validate/keyword.ts
Original file line number Diff line number Diff line change
@@ -144,7 +144,7 @@ export function validSchemaType(
}

export function validateKeywordUsage(
{schema, opts, self}: SchemaObjCxt,
{schema, opts, self, errSchemaPath}: SchemaObjCxt,
def: AddedKeywordDefinition,
keyword: string
): void {
@@ -161,7 +161,9 @@ export function validateKeywordUsage(
if (def.validateSchema) {
const valid = def.validateSchema(schema[keyword])
if (!valid) {
const msg = "keyword value is invalid: " + self.errorsText(def.validateSchema.errors)
const msg =
`keyword "${keyword}" value is invalid at path "${errSchemaPath}": ` +
self.errorsText(def.validateSchema.errors)
if (opts.validateSchema === "log") self.logger.error(msg)
else throw new Error(msg)
}
5 changes: 4 additions & 1 deletion lib/core.ts
Original file line number Diff line number Diff line change
@@ -97,6 +97,7 @@ export interface CurrentOptions {
allErrors?: boolean
verbose?: boolean
discriminator?: boolean
unicodeRegExp?: boolean
$comment?:
| true
| ((comment: string, schemaPath?: string, rootSchema?: AnySchemaObject) => unknown)
@@ -212,7 +213,8 @@ type RequiredInstanceOptions = {
| "messages"
| "addUsedSchema"
| "validateSchema"
| "validateFormats"]: NonNullable<Options[K]>
| "validateFormats"
| "unicodeRegExp"]: NonNullable<Options[K]>
} & {code: InstanceCodeOptions}

export type InstanceOptions = Options & RequiredInstanceOptions
@@ -239,6 +241,7 @@ function requiredOptions(o: Options): RequiredInstanceOptions {
addUsedSchema: o.addUsedSchema ?? true,
validateSchema: o.validateSchema ?? true,
validateFormats: o.validateFormats ?? true,
unicodeRegExp: o.unicodeRegExp ?? true,
}
}

2 changes: 1 addition & 1 deletion lib/types/jtd-schema.ts
Original file line number Diff line number Diff line change
@@ -109,7 +109,7 @@ export type JTDSchemaType<T, D extends Record<string, unknown> = Record<string,
true extends IsValues<T>
? T extends Record<string, infer V>
? {
values: JTDSchemaType<V>
values: JTDSchemaType<V, D>
}
: never
: // properties
2 changes: 1 addition & 1 deletion lib/vocabularies/applicator/additionalProperties.ts
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ const def: CodeKeywordDefinition & AddedKeywordDefinition = {
definedProp = nil
}
if (patProps.length) {
definedProp = or(definedProp, ...patProps.map((p) => _`${usePattern(gen, p)}.test(${key})`))
definedProp = or(definedProp, ...patProps.map((p) => _`${usePattern(cxt, p)}.test(${key})`))
}
return not(definedProp)
}
2 changes: 1 addition & 1 deletion lib/vocabularies/applicator/patternProperties.ts
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ const def: CodeKeywordDefinition = {

function validateProperties(pat: string): void {
gen.forIn("key", data, (key) => {
gen.if(_`${usePattern(gen, pat)}.test(${key})`, () => {
gen.if(_`${usePattern(cxt, pat)}.test(${key})`, () => {
cxt.subschema(
{
keyword: "patternProperties",
7 changes: 4 additions & 3 deletions lib/vocabularies/code.ts
Original file line number Diff line number Diff line change
@@ -90,11 +90,12 @@ export function callValidateCode(
return context !== nil ? _`${func}.call(${context}, ${args})` : _`${func}(${args})`
}

export function usePattern(gen: CodeGen, pattern: string): Name {
export function usePattern({gen, it: {opts}}: KeywordCxt, pattern: string): Name {
const u = opts.unicodeRegExp ? "u" : ""
return gen.scopeValue("pattern", {
key: pattern,
ref: new RegExp(pattern, "u"),
code: _`new RegExp(${pattern}, "u")`,
ref: new RegExp(pattern, u),
code: _`new RegExp(${pattern}, ${u})`,
})
}

6 changes: 4 additions & 2 deletions lib/vocabularies/validation/pattern.ts
Original file line number Diff line number Diff line change
@@ -17,8 +17,10 @@ const def: CodeKeywordDefinition = {
$data: true,
error,
code(cxt: KeywordCxt) {
const {gen, data, $data, schema, schemaCode} = cxt
const regExp = $data ? _`(new RegExp(${schemaCode}, "u"))` : usePattern(gen, schema) // TODO regexp should be wrapped in try/catch
const {data, $data, schema, schemaCode, it} = cxt
// TODO regexp should be wrapped in try/catchs
const u = it.opts.unicodeRegExp ? "u" : ""
const regExp = $data ? _`(new RegExp(${schemaCode}, ${u}))` : usePattern(cxt, schema)
cxt.fail$data(_`!${regExp}.test(${data})`)
},
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "8.0.5",
"version": "8.1.0",
"description": "Another JSON Schema Validator",
"main": "dist/ajv.js",
"types": "dist/ajv.d.ts",
38 changes: 38 additions & 0 deletions spec/issues/1539_add_keyword_name_to_validation_error.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import _Ajv from "../ajv2019"
import chai from "../chai"
const should = chai.should()

describe("keyword usage validation error", () => {
it("should include the keyword name and schema path in the message", () => {
const ajv = new _Ajv({
keywords: [
{
keyword: "customKeyword",
metaSchema: {
type: "string",
},
macro() {
return {}
},
},
],
})

const schema = {
type: "object",
properties: {
foo: {
type: "object",
customKeyword: {
bar: true,
},
},
},
}

should.throw(
() => ajv.compile(schema),
'keyword "customKeyword" value is invalid at path "#/properties/foo": data must be string'
)
})
})
5 changes: 4 additions & 1 deletion spec/keyword.spec.ts
Original file line number Diff line number Diff line change
@@ -1055,7 +1055,10 @@ describe("User-defined keywords", () => {
validate.errors.should.have.length(numErrors)
}

function shouldBeInvalidSchema(schema, msg: string | RegExp = /keyword value is invalid/) {
function shouldBeInvalidSchema(
schema,
msg: string | RegExp = /keyword .+ value is invalid at path/
) {
instances.forEach((_ajv) => {
should.throw(() => {
_ajv.compile(schema)
67 changes: 67 additions & 0 deletions spec/options/unicodeRegExp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import _Ajv from "../ajv"
import chai from "../chai"
const should = chai.should()

describe("unicodeRegExp option", () => {
const unicodeChar = "\uD83D\uDC4D"
const unicodeSchema = {
type: "string",
pattern: `^[${unicodeChar}]$`,
}

const schemaWithEscape = {
type: "string",
pattern: "^[\\:]$",
}

const patternPropertiesSchema = {
type: "object",
patternProperties: {
"^\\:.*$": {type: "number"},
},
additionalProperties: false,
}

describe("= true (default)", () => {
const ajv = new _Ajv()
it("should fail schema compilation if used invalid (unnecessary) escape sequence for pattern", () => {
should.throw(() => {
ajv.compile(schemaWithEscape)
}, /Invalid escape/)
})

it("should fail schema compilation if used invalid (unnecessary) escape sequence for patternProperties", () => {
should.throw(() => {
ajv.compile(patternPropertiesSchema)
}, /Invalid escape/)
})

it("should validate unicode character", () => {
const validate = ajv.compile(unicodeSchema)
validate(unicodeChar).should.equal(true)
})
})

describe("= false", () => {
const ajv = new _Ajv({unicodeRegExp: false})
it("should pass schema compilation if used unnecessary escape sequence for pattern", () => {
should.not.throw(() => {
const validate = ajv.compile(schemaWithEscape)
validate(":").should.equal(true)
})
})

it("should pass schema compilation if used unnecessary escape sequence for patternProperties", () => {
should.not.throw(() => {
const validate = ajv.compile(patternPropertiesSchema)
validate({":test": 1}).should.equal(true)
validate({test: 1}).should.equal(false)
})
})

it("should not validate unicode character", () => {
const validate = ajv.compile(unicodeSchema)
validate(unicodeChar).should.equal(false)
})
})
})