@amritk/generate-validators 0.13.1 → 0.15.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 +3 -2
- package/README.md +138 -15
- package/dist/generators/assert-unevaluated-generatable.js +9 -8
- package/dist/generators/build-schema.d.ts +1 -1
- package/dist/generators/build-schema.js +135 -9
- package/dist/generators/collect-emitted-refs.js +12 -10
- package/dist/generators/collect-validator-imports.d.ts +22 -0
- package/dist/generators/collect-validator-imports.js +13 -3
- package/dist/generators/generate-files.js +12 -5
- package/dist/generators/generate-validator-function.js +194 -136
- package/dist/generators/tuple-shape.js +7 -5
- package/dist/generators/unevaluated-match.js +33 -28
- package/package.json +6 -4
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import { generateTypeDefinition } from "@amritk/helpers/generate-type-definition";
|
|
2
2
|
import { collectValidatorImports } from "./collect-validator-imports.js";
|
|
3
3
|
import { generateBooleanGuard, generateValidatorFunction } from "./generate-validator-function.js";
|
|
4
|
+
const escapeForWordMatch = (name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4
5
|
const generateValidatorFile = (schema, typeName, options) => {
|
|
5
6
|
const typeSuffix = options?.typeSuffix ?? "";
|
|
6
|
-
const refImports = collectValidatorImports(schema, {
|
|
7
|
-
selfRef: options?.selfRef,
|
|
8
|
-
rootSchema: options?.rootSchema,
|
|
9
|
-
typeSuffix
|
|
10
|
-
});
|
|
11
7
|
const typeDefinition = generateTypeDefinition(schema, typeName, {
|
|
12
8
|
typeSuffix,
|
|
13
9
|
...options?.rootSchema !== void 0 ? { rootSchema: options.rootSchema } : {}
|
|
@@ -15,6 +11,17 @@ const generateValidatorFile = (schema, typeName, options) => {
|
|
|
15
11
|
const validatorFunction = generateValidatorFunction(schema, typeName, typeSuffix, options?.rootSchema);
|
|
16
12
|
const booleanGuard = generateBooleanGuard(schema, typeName, typeSuffix);
|
|
17
13
|
const body = validatorFunction + booleanGuard;
|
|
14
|
+
const emitted = typeDefinition + body;
|
|
15
|
+
const mentions = (name) => new RegExp(`\\b${escapeForWordMatch(name)}\\b`).test(emitted);
|
|
16
|
+
const refImports = collectValidatorImports(schema, {
|
|
17
|
+
selfRef: options?.selfRef,
|
|
18
|
+
rootSchema: options?.rootSchema,
|
|
19
|
+
typeSuffix,
|
|
20
|
+
reads: ({ typeName: name, validatorName }) => ({
|
|
21
|
+
type: mentions(name),
|
|
22
|
+
validator: mentions(validatorName)
|
|
23
|
+
})
|
|
24
|
+
});
|
|
18
25
|
const resultTypes = ["ValidationResult", .../\bValidationError\b/.test(body) ? ["ValidationError"] : []];
|
|
19
26
|
let result = `import type { ${resultTypes.join(", ")} } from './validation-result.js'
|
|
20
27
|
`;
|