1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getAjv = void 0;
|
4 | const ajv_1 = require("ajv");
|
5 | const AJV_OPTIONS = {
|
6 | removeAdditional: true,
|
7 | allErrors: true,
|
8 |
|
9 | useDefaults: 'empty',
|
10 |
|
11 |
|
12 | coerceTypes: false,
|
13 | };
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function getAjv(opt) {
|
20 | const ajv = new ajv_1.default({
|
21 | ...AJV_OPTIONS,
|
22 | ...opt,
|
23 | });
|
24 |
|
25 | addCustomAjvFormats(ajv);
|
26 |
|
27 |
|
28 | require('ajv-formats')(ajv);
|
29 |
|
30 | require('ajv-keywords')(ajv, [
|
31 | 'transform',
|
32 | 'uniqueItemProperties',
|
33 | 'instanceof',
|
34 | ]);
|
35 |
|
36 |
|
37 | require('ajv-merge-patch')(ajv);
|
38 | return ajv;
|
39 | }
|
40 | exports.getAjv = getAjv;
|
41 | function addCustomAjvFormats(ajv) {
|
42 | return (ajv
|
43 | .addFormat('id', /^[a-z0-9_]{6,64}$/)
|
44 | .addFormat('slug', /^[a-z0-9-]+$/)
|
45 | .addFormat('semVer', /^[0-9]+\.[0-9]+\.[0-9]+$/)
|
46 |
|
47 | .addFormat('languageTag', /^[a-z]{2}(-[A-Z]{2})?$/)
|
48 | .addFormat('countryCode', /^[A-Z]{2}$/)
|
49 | .addFormat('currency', /^[A-Z]{3}$/)
|
50 | .addFormat('unixTimestamp', {
|
51 | type: 'number',
|
52 | validate: (n) => {
|
53 |
|
54 | return n >= 0 && n < 16725225600;
|
55 | },
|
56 | })
|
57 | .addFormat('unixTimestampMillis', {
|
58 | type: 'number',
|
59 | validate: (n) => {
|
60 |
|
61 | return n >= 0 && n < 16725225600000;
|
62 | },
|
63 | })
|
64 | .addFormat('utcOffset', {
|
65 | type: 'number',
|
66 | validate: (n) => {
|
67 |
|
68 |
|
69 |
|
70 | return n >= -14 * 60 && n <= 14 * 60 && Number.isInteger(n);
|
71 | },
|
72 | })
|
73 | .addFormat('utcOffsetHours', {
|
74 | type: 'number',
|
75 | validate: (n) => {
|
76 |
|
77 |
|
78 |
|
79 | return n >= -14 && n <= 14 && Number.isInteger(n);
|
80 | },
|
81 | }));
|
82 | }
|