@amritk/lint 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AI.md +55 -0
- package/dist/core/document.js +12 -10
- package/dist/core/formats.js +10 -13
- package/dist/core/glob.js +96 -118
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.js +30 -11
- package/dist/core/jsonpath.js +487 -561
- package/dist/core/lint.js +78 -85
- package/dist/core/plugin.js +21 -29
- package/dist/core/pointers.js +107 -151
- package/dist/core/ruleset.js +0 -0
- package/dist/core/runner.js +214 -272
- package/dist/core/types.js +4 -1
- package/dist/core/validate-ruleset.js +98 -112
- package/dist/fix/apply.d.ts +1 -1
- package/dist/fix/apply.js +58 -96
- package/dist/fix/index.js +7 -2
- package/dist/fix/plugin.js +15 -20
- package/dist/fix/types.d.ts +1 -1
- package/dist/functions/alphabetical.d.ts +1 -1
- package/dist/functions/alphabetical.js +39 -50
- package/dist/functions/casing.d.ts +1 -1
- package/dist/functions/casing.js +39 -45
- package/dist/functions/defined.d.ts +1 -1
- package/dist/functions/defined.js +7 -5
- package/dist/functions/enumeration.d.ts +1 -1
- package/dist/functions/enumeration.js +15 -25
- package/dist/functions/falsy.d.ts +1 -1
- package/dist/functions/falsy.js +7 -5
- package/dist/functions/index.d.ts +1 -1
- package/dist/functions/index.js +60 -44
- package/dist/functions/length.d.ts +1 -1
- package/dist/functions/length.js +22 -32
- package/dist/functions/or.d.ts +1 -1
- package/dist/functions/or.js +18 -24
- package/dist/functions/pattern.d.ts +1 -1
- package/dist/functions/pattern.js +39 -49
- package/dist/functions/schema.d.ts +1 -1
- package/dist/functions/schema.js +93 -119
- package/dist/functions/truthy.d.ts +1 -1
- package/dist/functions/truthy.js +7 -5
- package/dist/functions/typed-enum.d.ts +1 -1
- package/dist/functions/typed-enum.js +33 -36
- package/dist/functions/undefined.d.ts +1 -1
- package/dist/functions/undefined.js +7 -8
- package/dist/functions/unreferenced-reusable-object.d.ts +1 -1
- package/dist/functions/unreferenced-reusable-object.js +35 -47
- package/dist/functions/xor.d.ts +1 -1
- package/dist/functions/xor.js +13 -16
- package/dist/index.d.ts +17 -4
- package/dist/index.js +149 -164
- package/dist/parsers/edit-model.js +316 -444
- package/dist/parsers/index.d.ts +1 -1
- package/dist/parsers/index.js +23 -19
- package/dist/parsers/json.js +39 -37
- package/dist/parsers/lines.js +23 -26
- package/dist/parsers/types.js +9 -7
- package/dist/parsers/yaml.js +137 -204
- package/dist/rules/openapi/fixers.js +166 -221
- package/dist/rules/openapi/formats.js +26 -27
- package/dist/rules/openapi/functions/example-validation.d.ts +1 -1
- package/dist/rules/openapi/functions/example-validation.js +98 -138
- package/dist/rules/openapi/functions/helpers.js +8 -10
- package/dist/rules/openapi/functions/index.d.ts +1 -1
- package/dist/rules/openapi/functions/index.js +98 -72
- package/dist/rules/openapi/functions/oas-additional-operations.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-additional-operations.js +16 -22
- package/dist/rules/openapi/functions/oas-discriminator.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-discriminator.js +24 -22
- package/dist/rules/openapi/functions/oas-example-external-value.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-example-external-value.js +13 -21
- package/dist/rules/openapi/functions/oas-example-value.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-example-value.js +24 -27
- package/dist/rules/openapi/functions/oas-mutually-exclusive.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-mutually-exclusive.js +15 -19
- package/dist/rules/openapi/functions/oas-no-nullable.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-no-nullable.js +13 -21
- package/dist/rules/openapi/functions/oas-op-form-data-consume-check.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-op-form-data-consume-check.js +21 -19
- package/dist/rules/openapi/functions/oas-op-id-unique.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-op-id-unique.js +27 -27
- package/dist/rules/openapi/functions/oas-op-params.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-op-params.js +36 -42
- package/dist/rules/openapi/functions/oas-op-security-defined.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-op-security-defined.js +40 -39
- package/dist/rules/openapi/functions/oas-op-success-response.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-op-success-response.js +11 -13
- package/dist/rules/openapi/functions/oas-path-param.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-path-param.js +75 -96
- package/dist/rules/openapi/functions/oas-schema-example-deprecated.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-schema-example-deprecated.js +33 -40
- package/dist/rules/openapi/functions/oas-schema.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-schema.js +9 -14
- package/dist/rules/openapi/functions/oas-server-name-unique.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-server-name-unique.js +21 -19
- package/dist/rules/openapi/functions/oas-server-variables.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-server-variables.js +45 -49
- package/dist/rules/openapi/functions/oas-tag-defined.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-tag-defined.js +20 -20
- package/dist/rules/openapi/functions/oas-tag-kind.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-tag-kind.js +16 -16
- package/dist/rules/openapi/functions/oas-tag-parent-defined.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-tag-parent-defined.js +40 -43
- package/dist/rules/openapi/functions/oas-tags-unique.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-tags-unique.js +18 -16
- package/dist/rules/openapi/functions/oas-unused-component.d.ts +1 -1
- package/dist/rules/openapi/functions/oas-unused-component.js +48 -58
- package/dist/rules/openapi/functions/ref-siblings.d.ts +1 -1
- package/dist/rules/openapi/functions/ref-siblings.js +13 -11
- package/dist/rules/openapi/index.d.ts +2 -1
- package/dist/rules/openapi/index.js +99 -117
- package/dist/rules/openapi/oas.d.ts +1 -1
- package/dist/rules/openapi/oas.js +524 -536
- package/dist/rules/openapi/schemas/index.js +17 -33
- package/dist/rules/openapi/schemas/oas20.json +1 -1592
- package/dist/rules/openapi/schemas/oas30.json +1 -1651
- package/dist/rules/openapi/schemas/oas31.json +1 -1412
- package/dist/rules/openapi/schemas/oas32.json +1 -1684
- package/package.json +11 -6
package/dist/functions/casing.js
CHANGED
|
@@ -1,52 +1,46 @@
|
|
|
1
1
|
const PATTERNS = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
flat: "[a-z][a-z{d}]*",
|
|
3
|
+
camel: "[a-z][a-z{d}]*(?:[A-Z{d}](?:[a-z{d}]+|$))*",
|
|
4
|
+
pascal: "[A-Z][a-z{d}]*(?:[A-Z{d}](?:[a-z{d}]+|$))*",
|
|
5
|
+
// Segments after a separator may start with a digit, matching Spectral (so
|
|
6
|
+
// "foo-2fa" is valid kebab case). The sub-pattern is `[a-z{d}]+`, not the
|
|
7
|
+
// stricter `[a-z][a-z{d}]*` which would require a letter right after the sep.
|
|
8
|
+
kebab: "[a-z][a-z{d}]*(?:-[a-z{d}]+)*",
|
|
9
|
+
cobol: "[A-Z][A-Z{d}]*(?:-[A-Z{d}]+)*",
|
|
10
|
+
snake: "[a-z][a-z{d}]*(?:_[a-z{d}]+)*",
|
|
11
|
+
macro: "[A-Z][A-Z{d}]*(?:_[A-Z{d}]+)*"
|
|
12
12
|
};
|
|
13
13
|
const VALID_TYPES = Object.keys(PATTERNS);
|
|
14
|
-
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g,
|
|
14
|
+
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
15
15
|
const buildRegExp = (options) => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
16
|
+
const digits = options.disallowDigits ? "" : "0-9";
|
|
17
|
+
const base = PATTERNS[options.type].replace(/\{d\}/g, digits);
|
|
18
|
+
if (!options.separator)
|
|
19
|
+
return new RegExp(`^${base}$`);
|
|
20
|
+
const sep = escapeRegExp(options.separator.char);
|
|
21
|
+
const leading = options.separator.allowLeading ? `${sep}?` : "";
|
|
22
|
+
return new RegExp(`^${leading}${base}(?:${sep}${base})*$`);
|
|
23
23
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (!options?.type)
|
|
27
|
-
return [];
|
|
28
|
-
// Guard an unknown `type` before it reaches `PATTERNS[type]`, which would be
|
|
29
|
-
// `undefined` and crash on `.replace`. Mirror Spectral's option schema by
|
|
30
|
-
// naming every accepted value in a single, clear error finding.
|
|
31
|
-
if (!VALID_TYPES.includes(options.type)) {
|
|
32
|
-
return [
|
|
33
|
-
{
|
|
34
|
-
message: `"casing" function and its "type" option accept the following values: ${VALID_TYPES.join(', ')}`,
|
|
35
|
-
},
|
|
36
|
-
];
|
|
37
|
-
}
|
|
38
|
-
if (typeof input !== 'string' || input.length === 0)
|
|
39
|
-
return [];
|
|
40
|
-
// Spectral special-cases a lone separator char with `allowLeading` as valid.
|
|
41
|
-
// This is what keeps the OpenAPI root path "/" from being flagged.
|
|
42
|
-
if (input.length === 1 &&
|
|
43
|
-
options.separator !== undefined &&
|
|
44
|
-
options.separator.allowLeading === true &&
|
|
45
|
-
input === options.separator.char) {
|
|
46
|
-
return [];
|
|
47
|
-
}
|
|
48
|
-
if (!buildRegExp(options).test(input)) {
|
|
49
|
-
return [{ message: `The value must be in ${options.type} case` }];
|
|
50
|
-
}
|
|
24
|
+
const casing = (input, options) => {
|
|
25
|
+
if (!options?.type)
|
|
51
26
|
return [];
|
|
27
|
+
if (!VALID_TYPES.includes(options.type)) {
|
|
28
|
+
return [
|
|
29
|
+
{
|
|
30
|
+
message: `"casing" function and its "type" option accept the following values: ${VALID_TYPES.join(", ")}`
|
|
31
|
+
}
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
if (typeof input !== "string" || input.length === 0)
|
|
35
|
+
return [];
|
|
36
|
+
if (input.length === 1 && options.separator !== void 0 && options.separator.allowLeading === true && input === options.separator.char) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
if (!buildRegExp(options).test(input)) {
|
|
40
|
+
return [{ message: `The value must be in ${options.type} case` }];
|
|
41
|
+
}
|
|
42
|
+
return [];
|
|
43
|
+
};
|
|
44
|
+
export {
|
|
45
|
+
casing
|
|
52
46
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
const defined = (input) => {
|
|
2
|
+
if (input === void 0)
|
|
3
|
+
return [{ message: "The value must be defined" }];
|
|
4
|
+
return [];
|
|
5
|
+
};
|
|
6
|
+
export {
|
|
7
|
+
defined
|
|
6
8
|
};
|
|
@@ -1,27 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* flagged against a list they could never be `===` to.
|
|
6
|
-
*/
|
|
7
|
-
const isPrimitive = (value) => value === null || (typeof value !== 'object' && typeof value !== 'function');
|
|
8
|
-
/** Flags a value that is not one of the allowed `values`. */
|
|
9
|
-
export const enumeration = (input, options) => {
|
|
10
|
-
const values = options?.values;
|
|
11
|
-
// Without a valid list of allowed values there is nothing to compare against.
|
|
12
|
-
// Spectral requires `values` in its option schema and no-ops when it is
|
|
13
|
-
// missing, so skip rather than flag everything against an empty allow-list.
|
|
14
|
-
if (!Array.isArray(values))
|
|
15
|
-
return [];
|
|
16
|
-
// Spectral's input schema only lets primitives reach this function. `includes`
|
|
17
|
-
// uses reference equality, so a non-primitive would always be reported as
|
|
18
|
-
// "not allowed"; match Spectral by skipping objects and arrays entirely.
|
|
19
|
-
if (!isPrimitive(input))
|
|
20
|
-
return [];
|
|
21
|
-
if (!values.includes(input)) {
|
|
22
|
-
return [
|
|
23
|
-
{ message: `The value must be one of the allowed values: ${values.map((v) => JSON.stringify(v)).join(', ')}` },
|
|
24
|
-
];
|
|
25
|
-
}
|
|
1
|
+
const isPrimitive = (value) => value === null || typeof value !== "object" && typeof value !== "function";
|
|
2
|
+
const enumeration = (input, options) => {
|
|
3
|
+
const values = options?.values;
|
|
4
|
+
if (!Array.isArray(values))
|
|
26
5
|
return [];
|
|
6
|
+
if (!isPrimitive(input))
|
|
7
|
+
return [];
|
|
8
|
+
if (!values.includes(input)) {
|
|
9
|
+
return [
|
|
10
|
+
{ message: `The value must be one of the allowed values: ${values.map((v) => JSON.stringify(v)).join(", ")}` }
|
|
11
|
+
];
|
|
12
|
+
}
|
|
13
|
+
return [];
|
|
14
|
+
};
|
|
15
|
+
export {
|
|
16
|
+
enumeration
|
|
27
17
|
};
|
package/dist/functions/falsy.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
const falsy = (input) => {
|
|
2
|
+
if (input)
|
|
3
|
+
return [{ message: "The value must be falsy" }];
|
|
4
|
+
return [];
|
|
5
|
+
};
|
|
6
|
+
export {
|
|
7
|
+
falsy
|
|
6
8
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FunctionRegistry } from '../core/
|
|
1
|
+
import type { FunctionRegistry } from '../core/types.js';
|
|
2
2
|
export { alphabetical, type IAlphabeticalOptions } from './alphabetical.js';
|
|
3
3
|
export { type CasingType, casing, type ICasingOptions } from './casing.js';
|
|
4
4
|
export { defined } from './defined.js';
|
package/dist/functions/index.js
CHANGED
|
@@ -1,45 +1,61 @@
|
|
|
1
|
-
import { alphabetical } from
|
|
2
|
-
import { casing } from
|
|
3
|
-
import { defined } from
|
|
4
|
-
import { enumeration } from
|
|
5
|
-
import { falsy } from
|
|
6
|
-
import { length } from
|
|
7
|
-
import { or } from
|
|
8
|
-
import { pattern } from
|
|
9
|
-
import { schema } from
|
|
10
|
-
import { truthy } from
|
|
11
|
-
import { typedEnum } from
|
|
12
|
-
import { undefinedFn } from
|
|
13
|
-
import { unreferencedReusableObject } from
|
|
14
|
-
import { xor } from
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
1
|
+
import { alphabetical } from "./alphabetical.js";
|
|
2
|
+
import { casing } from "./casing.js";
|
|
3
|
+
import { defined } from "./defined.js";
|
|
4
|
+
import { enumeration } from "./enumeration.js";
|
|
5
|
+
import { falsy } from "./falsy.js";
|
|
6
|
+
import { length } from "./length.js";
|
|
7
|
+
import { or } from "./or.js";
|
|
8
|
+
import { pattern } from "./pattern.js";
|
|
9
|
+
import { schema } from "./schema.js";
|
|
10
|
+
import { truthy } from "./truthy.js";
|
|
11
|
+
import { typedEnum } from "./typed-enum.js";
|
|
12
|
+
import { undefinedFn } from "./undefined.js";
|
|
13
|
+
import { unreferencedReusableObject } from "./unreferenced-reusable-object.js";
|
|
14
|
+
import { xor } from "./xor.js";
|
|
15
|
+
import { alphabetical as alphabetical2 } from "./alphabetical.js";
|
|
16
|
+
import { casing as casing2 } from "./casing.js";
|
|
17
|
+
import { defined as defined2 } from "./defined.js";
|
|
18
|
+
import { enumeration as enumeration2 } from "./enumeration.js";
|
|
19
|
+
import { falsy as falsy2 } from "./falsy.js";
|
|
20
|
+
import { length as length2 } from "./length.js";
|
|
21
|
+
import { or as or2 } from "./or.js";
|
|
22
|
+
import { pattern as pattern2 } from "./pattern.js";
|
|
23
|
+
import { schema as schema2 } from "./schema.js";
|
|
24
|
+
import { truthy as truthy2 } from "./truthy.js";
|
|
25
|
+
import { typedEnum as typedEnum2 } from "./typed-enum.js";
|
|
26
|
+
import { undefinedFn as undefinedFn2 } from "./undefined.js";
|
|
27
|
+
import { unreferencedReusableObject as unreferencedReusableObject2 } from "./unreferenced-reusable-object.js";
|
|
28
|
+
import { xor as xor2 } from "./xor.js";
|
|
29
|
+
const builtinFunctions = {
|
|
30
|
+
alphabetical,
|
|
31
|
+
casing,
|
|
32
|
+
defined,
|
|
33
|
+
enumeration,
|
|
34
|
+
falsy,
|
|
35
|
+
length,
|
|
36
|
+
or,
|
|
37
|
+
pattern,
|
|
38
|
+
schema,
|
|
39
|
+
truthy,
|
|
40
|
+
undefined: undefinedFn,
|
|
41
|
+
unreferencedReusableObject,
|
|
42
|
+
xor,
|
|
43
|
+
typedEnum
|
|
44
|
+
};
|
|
45
|
+
export {
|
|
46
|
+
alphabetical2 as alphabetical,
|
|
47
|
+
builtinFunctions,
|
|
48
|
+
casing2 as casing,
|
|
49
|
+
defined2 as defined,
|
|
50
|
+
enumeration2 as enumeration,
|
|
51
|
+
falsy2 as falsy,
|
|
52
|
+
length2 as length,
|
|
53
|
+
or2 as or,
|
|
54
|
+
pattern2 as pattern,
|
|
55
|
+
schema2 as schema,
|
|
56
|
+
truthy2 as truthy,
|
|
57
|
+
typedEnum2 as typedEnum,
|
|
58
|
+
undefinedFn2 as undefinedFn,
|
|
59
|
+
unreferencedReusableObject2 as unreferencedReusableObject,
|
|
60
|
+
xor2 as xor
|
|
45
61
|
};
|
package/dist/functions/length.js
CHANGED
|
@@ -1,35 +1,25 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the comparable size of a value: string/array length, object key count,
|
|
3
|
-
* or the number itself. `undefined` for anything else (so the rule is skipped).
|
|
4
|
-
*/
|
|
5
1
|
const measure = (input) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
if (typeof input === "string" || Array.isArray(input))
|
|
3
|
+
return input.length;
|
|
4
|
+
if (typeof input === "object" && input !== null)
|
|
5
|
+
return Object.keys(input).length;
|
|
6
|
+
if (typeof input === "number")
|
|
7
|
+
return input;
|
|
8
|
+
return void 0;
|
|
13
9
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
results.push({ message: `The value must not be shorter than ${options.min}` });
|
|
30
|
-
}
|
|
31
|
-
if (typeof options?.max === 'number' && size > options.max) {
|
|
32
|
-
results.push({ message: `The value must not be longer than ${options.max}` });
|
|
33
|
-
}
|
|
34
|
-
return results;
|
|
10
|
+
const length = (input, options) => {
|
|
11
|
+
const size = measure(input);
|
|
12
|
+
if (size === void 0)
|
|
13
|
+
return [];
|
|
14
|
+
const results = [];
|
|
15
|
+
if (typeof options?.min === "number" && size < options.min) {
|
|
16
|
+
results.push({ message: `The value must not be shorter than ${options.min}` });
|
|
17
|
+
}
|
|
18
|
+
if (typeof options?.max === "number" && size > options.max) {
|
|
19
|
+
results.push({ message: `The value must not be longer than ${options.max}` });
|
|
20
|
+
}
|
|
21
|
+
return results;
|
|
22
|
+
};
|
|
23
|
+
export {
|
|
24
|
+
length
|
|
35
25
|
};
|
package/dist/functions/or.d.ts
CHANGED
package/dist/functions/or.js
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (properties.length > 4) {
|
|
20
|
-
const shortProps = properties.slice(0, 3);
|
|
21
|
-
const count = `${properties.length - 3} other properties must be defined`;
|
|
22
|
-
return [{ message: `At least one of "${shortProps.join('" or "')}" or ${count}` }];
|
|
23
|
-
}
|
|
24
|
-
return [{ message: `At least one of "${properties.join('" or "')}" must be defined` }];
|
|
1
|
+
const or = (input, options) => {
|
|
2
|
+
if (typeof input !== "object" || input === null)
|
|
3
|
+
return [];
|
|
4
|
+
const properties = options?.properties;
|
|
5
|
+
if (!Array.isArray(properties) || properties.length < 2)
|
|
6
|
+
return [];
|
|
7
|
+
const present = properties.filter((property) => property in input);
|
|
8
|
+
if (present.length > 0)
|
|
9
|
+
return [];
|
|
10
|
+
if (properties.length > 4) {
|
|
11
|
+
const shortProps = properties.slice(0, 3);
|
|
12
|
+
const count = `${properties.length - 3} other properties must be defined`;
|
|
13
|
+
return [{ message: `At least one of "${shortProps.join('" or "')}" or ${count}` }];
|
|
14
|
+
}
|
|
15
|
+
return [{ message: `At least one of "${properties.join('" or "')}" must be defined` }];
|
|
16
|
+
};
|
|
17
|
+
export {
|
|
18
|
+
or
|
|
25
19
|
};
|
|
@@ -1,53 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
let compiled;
|
|
19
|
-
try {
|
|
20
|
-
const match = /^\/(.+)\/([a-z]*)$/s.exec(pattern);
|
|
21
|
-
compiled = match ? new RegExp(match[1], match[2]) : new RegExp(pattern);
|
|
22
|
-
}
|
|
23
|
-
catch (error) {
|
|
24
|
-
compiled = error instanceof Error ? error : new Error(String(error));
|
|
25
|
-
}
|
|
26
|
-
cache.set(pattern, compiled);
|
|
27
|
-
return compiled;
|
|
1
|
+
const cache = /* @__PURE__ */ new Map();
|
|
2
|
+
const toRegExp = (pattern2) => {
|
|
3
|
+
const cached = cache.get(pattern2);
|
|
4
|
+
if (cached !== void 0) {
|
|
5
|
+
if (cached instanceof RegExp)
|
|
6
|
+
cached.lastIndex = 0;
|
|
7
|
+
return cached;
|
|
8
|
+
}
|
|
9
|
+
let compiled;
|
|
10
|
+
try {
|
|
11
|
+
const match = /^\/(.+)\/([a-z]*)$/s.exec(pattern2);
|
|
12
|
+
compiled = match ? new RegExp(match[1], match[2]) : new RegExp(pattern2);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
compiled = error instanceof Error ? error : new Error(String(error));
|
|
15
|
+
}
|
|
16
|
+
cache.set(pattern2, compiled);
|
|
17
|
+
return compiled;
|
|
28
18
|
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
else if (!re.test(input)) {
|
|
40
|
-
results.push({ message: `The value must match the pattern "${options.match}"` });
|
|
41
|
-
}
|
|
19
|
+
const pattern = (input, options) => {
|
|
20
|
+
if (typeof input !== "string")
|
|
21
|
+
return [];
|
|
22
|
+
const results = [];
|
|
23
|
+
if (options?.match !== void 0) {
|
|
24
|
+
const re = toRegExp(options.match);
|
|
25
|
+
if (re instanceof Error) {
|
|
26
|
+
results.push({ message: `The "match" option is not a valid regular expression: ${re.message}` });
|
|
27
|
+
} else if (!re.test(input)) {
|
|
28
|
+
results.push({ message: `The value must match the pattern "${options.match}"` });
|
|
42
29
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
30
|
+
}
|
|
31
|
+
if (options?.notMatch !== void 0) {
|
|
32
|
+
const re = toRegExp(options.notMatch);
|
|
33
|
+
if (re instanceof Error) {
|
|
34
|
+
results.push({ message: `The "notMatch" option is not a valid regular expression: ${re.message}` });
|
|
35
|
+
} else if (re.test(input)) {
|
|
36
|
+
results.push({ message: `The value must not match the pattern "${options.notMatch}"` });
|
|
51
37
|
}
|
|
52
|
-
|
|
38
|
+
}
|
|
39
|
+
return results;
|
|
40
|
+
};
|
|
41
|
+
export {
|
|
42
|
+
pattern
|
|
53
43
|
};
|