-
-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pattern and path to regexp create errors #2477
- Loading branch information
1 parent
9050ba1
commit d33a36d
Showing
3 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import _Ajv from "../ajv2020" | ||
import * as assert from "assert" | ||
|
||
describe("Invalid regexp patterns should throw more informative errors (issue #2477)", () => { | ||
it("throws with pattern and schema path", () => { | ||
const ajv = new _Ajv() | ||
|
||
const rootSchema = { | ||
type: "string", | ||
pattern: "^[0-9]{2-4}", | ||
} | ||
|
||
assert.throws( | ||
() => ajv.compile(rootSchema), | ||
(thrown: unknown) => { | ||
assert.equal((thrown as Error).message, "Invalid regular expression: ^[0-9]{2-4} at #") | ||
return true | ||
} | ||
) | ||
|
||
const pathSchema = { | ||
type: "object", | ||
properties: { | ||
foo: rootSchema, | ||
}, | ||
} | ||
|
||
assert.throws( | ||
() => ajv.compile(pathSchema), | ||
(thrown: unknown) => { | ||
assert.equal( | ||
(thrown as Error).message, | ||
"Invalid regular expression: ^[0-9]{2-4} at #/properties/foo" | ||
) | ||
return true | ||
} | ||
) | ||
}) | ||
it("throws with pattern and schema path with $data", () => { | ||
const ajv = new _Ajv({$data: true}) | ||
|
||
const schema = { | ||
properties: { | ||
shouldMatch: {}, | ||
string: {pattern: {$data: "1/shouldMatch"}}, | ||
}, | ||
} | ||
const validate = ajv.compile(schema) | ||
|
||
assert.throws( | ||
() => ajv.compile(validate({shouldMatch: "^[0-9]{2-4}", string: "123"})), | ||
(thrown: unknown) => { | ||
assert.equal( | ||
(thrown as Error).message, | ||
"Invalid regular expression: ^[0-9]{2-4} at #/properties/string" | ||
) | ||
return true | ||
} | ||
) | ||
}) | ||
}) |