Skip to content

Commit 4d3ab6f

Browse files
committed
refactor: replace ajv-formats-draft2019 with custom implementations for idn-email format (leveraging ajv-formats)
Signed-off-by: Pasi Eronen <pe@iki.fi>
1 parent 31ae08b commit 4d3ab6f

5 files changed

Lines changed: 11 additions & 12 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ Some features require optional peer dependencies — see `package.json` for vers
129129
* Validation of JSON on _Node.js_ requires all of:
130130
* [`ajv`](https://www.npmjs.com/package/ajv)
131131
* [`ajv-formats`](https://www.npmjs.com/package/ajv-formats)
132-
* [`ajv-formats-draft2019`](https://www.npmjs.com/package/ajv-formats-draft2019)
133132
* Validation of XML on _Node.js_ requires any of:
134133
* [`libxmljs2`](https://www.npmjs.com/package/libxmljs2)
135134
* the system might need to meet the requirements for [`node-gyp`](https://github.com/TooTallNate/node-gyp#installation), in certain cases.

docs/install.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ See the shipped ``package.json`` for version constraints.
3030
* Validation of JSON on _Node.js_ requires all of:
3131
* `ajv <https://www.npmjs.com/package/ajv>`_
3232
* `ajv-formats <https://www.npmjs.com/package/ajv-formats>`_
33-
* `ajv-formats-draft2019 <https://www.npmjs.com/package/ajv-formats-draft2019>`_
3433
* Validation of XML on _Node.js_ requires all of:
3534
* `libxmljs2 <https://www.npmjs.com/package/libxmljs2>`_
3635
* the system must meet the requirements for `node-gyp <https://github.com/TooTallNate/node-gyp#installation>`_

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
"peerDependencies": {
8484
"ajv": "^8.12.0",
8585
"ajv-formats": "^3.0.1",
86-
"ajv-formats-draft2019": "^1.6.1",
8786
"libxmljs2": "^0.35||^0.37",
8887
"xmlbuilder2": "^3.0.2||^4.0.0",
8988
"packageurl-js": "*",
@@ -96,9 +95,6 @@
9695
"ajv-formats": {
9796
"optional": true
9897
},
99-
"ajv-formats-draft2019": {
100-
"optional": true
101-
},
10298
"libxmljs2": {
10399
"optional": true
104100
},
@@ -127,7 +123,6 @@
127123
"devDependencies": {
128124
"ajv": "^8.12.0",
129125
"ajv-formats": "^3.0.1",
130-
"ajv-formats-draft2019": "^1.6.1",
131126
"libxmljs2": "^0.35||^0.37",
132127
"xmlbuilder2": "^3.0.2||^4.0.0",
133128
"spdx-expression-parse": "^3.0.1||^4",

src/_optPlug.node/__jsonValidators/ajv.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import { readFile } from 'node:fs/promises'
2121

2222
import Ajv, { type Options as AjvOptions } from 'ajv'
2323
import addFormats from 'ajv-formats'
24-
/* @ts-expect-error TS7016 */
25-
import addFormats2019 from 'ajv-formats-draft2019'
2624

2725
import type { ValidationError } from '../../validation/types'
2826
import type { Functionality, Validator } from '../jsonValidator'
@@ -50,11 +48,19 @@ export default (async function (schemaPath: string, schemaMap: Record<string, st
5048
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- intended */
5149
const ajv = new Ajv({ ...ajvOptions, schemas })
5250
addFormats(ajv)
53-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-call -- intended */
54-
addFormats2019(ajv, { formats: ['idn-email'] })
51+
5552
// there is just no working implementation for format "iri-reference": see https://github.com/luzlab/ajv-formats-draft2019/issues/22
5653
ajv.addFormat('iri-reference', true)
5754

55+
// add idn-email format (was previously provided by ajv-formats-draft2019)
56+
const emailValidator = ajv.compile({type: 'string', format: 'email'})
57+
ajv.addFormat('idn-email', {
58+
type: 'string',
59+
// syntax allows non-ASCII characters in places where 'x' would be allowed
60+
// (don't attempt to validate exactly which Unicode characters are OK - too complex)
61+
validate: x => emailValidator(x.replace(/[\u0080-\uffff]+/g, 'x'))
62+
})
63+
5864
const validator = ajv.compile(schema)
5965

6066
return function (data: string): null | ValidationError {

src/_optPlug.node/jsonValidator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default opWrapper<Functionality>('JsonValidator', [
2727
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-require-imports
2828
-- needed */
2929

30-
['( ajv && ajv-formats && ajv-formats-draft2019 )', () => require('./__jsonValidators/ajv').default]
30+
['( ajv && ajv-formats )', () => require('./__jsonValidators/ajv').default]
3131
// ... add others here, pull-requests welcome!
3232

3333
/* eslint-enable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-require-imports */

0 commit comments

Comments
 (0)