Skip to content

Commit 05e52bf

Browse files
committed
refactor(lint): extract createAjv() to utils
1 parent f4c6041 commit 05e52bf

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

lint/linter/test-schema.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
33

4-
import { Ajv } from 'ajv';
5-
import ajvErrors from 'ajv-errors';
6-
import ajvFormats from 'ajv-formats';
74
import betterAjvErrors from 'better-ajv-errors';
85

6+
import { createAjv } from '../../utils/index.js';
97
import { Linter, Logger, LinterData } from '../utils.js';
108

119
import compatDataSchema from './../../schemas/compat-data.schema.json' with { type: 'json' };
1210
import browserDataSchema from './../../schemas/browsers.schema.json' with { type: 'json' };
1311

14-
const ajv = new Ajv({ allErrors: true });
15-
// We use 'fast' because as a side effect that makes the "uri" format more lax.
16-
// By default the "uri" format rejects ① and similar in URLs.
17-
ajvFormats.default(ajv, { mode: 'fast' });
18-
// Allow for custom error messages to provide better directions for contributors
19-
ajvErrors.default(ajv);
20-
21-
// Define keywords for schema->TS converter
22-
ajv.addKeyword('tsEnumNames');
23-
ajv.addKeyword('tsName');
24-
ajv.addKeyword('tsType');
12+
const ajv = createAjv();
2513

2614
export default {
2715
name: 'JSON Schema',

utils/ajv.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Ajv } from 'ajv';
2+
import ajvErrors from 'ajv-errors';
3+
import ajvFormats from 'ajv-formats';
4+
5+
/**
6+
* Returns a new pre-configured Ajv instance.
7+
* @returns the Ajv instance.
8+
*/
9+
export const createAjv = (): Ajv => {
10+
const ajv = new Ajv({ allErrors: true });
11+
// We use 'fast' because as a side effect that makes the "uri" format more lax.
12+
// By default the "uri" format rejects ① and similar in URLs.
13+
ajvFormats.default(ajv, { mode: 'fast' });
14+
// Allow for custom error messages to provide better directions for contributors
15+
ajvErrors.default(ajv);
16+
17+
// Define keywords for schema->TS converter
18+
ajv.addKeyword('tsEnumNames');
19+
ajv.addKeyword('tsName');
20+
ajv.addKeyword('tsType');
21+
22+
return ajv;
23+
};

utils/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
33

4+
import { createAjv } from './ajv.js';
45
import iterSupport from './iter-support.js';
56
import query from './query.js';
67
import spawn from './spawn.js';
78
import spawnAsync from './spawn-async.js';
89
import walk from './walk.js';
910
import normalizePath from './normalize-path.js';
1011

11-
export { iterSupport, normalizePath, query, spawn, spawnAsync, walk };
12+
export {
13+
createAjv,
14+
iterSupport,
15+
normalizePath,
16+
query,
17+
spawn,
18+
spawnAsync,
19+
walk,
20+
};

0 commit comments

Comments
 (0)