@amritk/generate-validators 0.15.0 → 0.15.1
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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { regexFlagsFor, regexLiteral } from "@amritk/helpers/escape-regex-pattern";
|
|
2
2
|
import { getMjstInstanceOf, getMjstPrimitive, MJST_EXTENSION_KEY } from "@amritk/helpers/mjst-extension";
|
|
3
3
|
import { multipleOfFailExpr, multipleOfPassExpr } from "@amritk/helpers/multiple-of-check";
|
|
4
|
+
import { boundFailExpr, boundOperator, boundPassExpr } from "@amritk/helpers/numeric-bound-check";
|
|
4
5
|
import { declaresKey, readKey } from "@amritk/helpers/read-key";
|
|
5
6
|
import { refToName } from "@amritk/helpers/ref-to-name";
|
|
6
7
|
import { safeAccessor } from "@amritk/helpers/safe-accessor";
|
|
@@ -338,25 +339,23 @@ const generateConstraintChecks = (key, raw, path, propSchema, suffix, ctx) => {
|
|
|
338
339
|
if (hasMinimum(propSchema) || hasMaximum(propSchema) || hasExclusiveMinimum(propSchema) || hasExclusiveMaximum(propSchema) || hasMultipleOf(propSchema)) {
|
|
339
340
|
if (hasMinimum(propSchema)) {
|
|
340
341
|
const strict = hasStrictExclusiveMinimum(propSchema);
|
|
341
|
-
|
|
342
|
-
lines.push(`
|
|
343
|
-
lines.push(` ${ctx.sink}.push({ message: 'must be ${rel} ${propSchema.minimum}', path: ${path} })`);
|
|
342
|
+
lines.push(` if (typeof ${raw} === 'number' && ${boundFailExpr(raw, "minimum", propSchema.minimum, strict)}) {`);
|
|
343
|
+
lines.push(` ${ctx.sink}.push({ message: 'must be ${boundOperator("minimum", strict)} ${propSchema.minimum}', path: ${path} })`);
|
|
344
344
|
lines.push(` }`);
|
|
345
345
|
}
|
|
346
346
|
if (hasMaximum(propSchema)) {
|
|
347
347
|
const strict = hasStrictExclusiveMaximum(propSchema);
|
|
348
|
-
|
|
349
|
-
lines.push(`
|
|
350
|
-
lines.push(` ${ctx.sink}.push({ message: 'must be ${rel} ${propSchema.maximum}', path: ${path} })`);
|
|
348
|
+
lines.push(` if (typeof ${raw} === 'number' && ${boundFailExpr(raw, "maximum", propSchema.maximum, strict)}) {`);
|
|
349
|
+
lines.push(` ${ctx.sink}.push({ message: 'must be ${boundOperator("maximum", strict)} ${propSchema.maximum}', path: ${path} })`);
|
|
351
350
|
lines.push(` }`);
|
|
352
351
|
}
|
|
353
352
|
if (hasExclusiveMinimum(propSchema)) {
|
|
354
|
-
lines.push(` if (typeof ${raw} === 'number' &&
|
|
353
|
+
lines.push(` if (typeof ${raw} === 'number' && ${boundFailExpr(raw, "minimum", propSchema.exclusiveMinimum, true)}) {`);
|
|
355
354
|
lines.push(` ${ctx.sink}.push({ message: 'must be > ${propSchema.exclusiveMinimum}', path: ${path} })`);
|
|
356
355
|
lines.push(` }`);
|
|
357
356
|
}
|
|
358
357
|
if (hasExclusiveMaximum(propSchema)) {
|
|
359
|
-
lines.push(` if (typeof ${raw} === 'number' &&
|
|
358
|
+
lines.push(` if (typeof ${raw} === 'number' && ${boundFailExpr(raw, "maximum", propSchema.exclusiveMaximum, true)}) {`);
|
|
360
359
|
lines.push(` ${ctx.sink}.push({ message: 'must be < ${propSchema.exclusiveMaximum}', path: ${path} })`);
|
|
361
360
|
lines.push(` }`);
|
|
362
361
|
}
|
|
@@ -1070,13 +1069,13 @@ const booleanLeafExpr = (schema, acc, narrowable = true) => {
|
|
|
1070
1069
|
if (t === "integer")
|
|
1071
1070
|
parts.push(`Number.isInteger(${acc})`);
|
|
1072
1071
|
if (hasMinimum(schema))
|
|
1073
|
-
parts.push(
|
|
1072
|
+
parts.push(boundPassExpr(num, "minimum", schema.minimum, hasStrictExclusiveMinimum(schema)));
|
|
1074
1073
|
if (hasMaximum(schema))
|
|
1075
|
-
parts.push(
|
|
1074
|
+
parts.push(boundPassExpr(num, "maximum", schema.maximum, hasStrictExclusiveMaximum(schema)));
|
|
1076
1075
|
if (hasExclusiveMinimum(schema))
|
|
1077
|
-
parts.push(
|
|
1076
|
+
parts.push(boundPassExpr(num, "minimum", schema.exclusiveMinimum, true));
|
|
1078
1077
|
if (hasExclusiveMaximum(schema))
|
|
1079
|
-
parts.push(
|
|
1078
|
+
parts.push(boundPassExpr(num, "maximum", schema.exclusiveMaximum, true));
|
|
1080
1079
|
if (hasMultipleOf(schema))
|
|
1081
1080
|
parts.push(multipleOfPassExpr(num, schema.multipleOf));
|
|
1082
1081
|
return withMembership(parts.join(" && "));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amritk/generate-validators",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.1",
|
|
4
4
|
"description": "Generate TypeScript validation functions from JSON Schemas.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -55,10 +55,10 @@
|
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"json-schema-typed": "^8.0.1",
|
|
58
|
-
"@amritk/helpers": "^0.
|
|
58
|
+
"@amritk/helpers": "^0.18.0"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@amritk/runtime-validators": "^0.
|
|
61
|
+
"@amritk/runtime-validators": "^0.13.0",
|
|
62
62
|
"@ryoppippi/unplugin-typia": "^2.6.5",
|
|
63
63
|
"@scalar/openapi-parser": "^0.26.1",
|
|
64
64
|
"@sinclair/typebox": "^0.34.49",
|