@amritk/generate-parsers 0.23.0 → 0.25.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/README.md +37 -463
- package/dist/index.d.ts +22 -2
- package/dist/index.js +18 -1
- package/package.json +15 -40
- package/AI.md +0 -52
- package/dist/generators/assert-supported-keywords.d.ts +0 -30
- package/dist/generators/assert-supported-keywords.js +0 -87
- package/dist/generators/build-schema.d.ts +0 -111
- package/dist/generators/build-schema.js +0 -74
- package/dist/generators/generate-deep-equal-check.d.ts +0 -24
- package/dist/generators/generate-deep-equal-check.js +0 -35
- package/dist/generators/generate-default-from-pattern.d.ts +0 -26
- package/dist/generators/generate-default-from-pattern.js +0 -223
- package/dist/generators/generate-discriminated-union-validation.d.ts +0 -15
- package/dist/generators/generate-discriminated-union-validation.js +0 -28
- package/dist/generators/generate-enum-check.d.ts +0 -35
- package/dist/generators/generate-enum-check.js +0 -31
- package/dist/generators/generate-files.d.ts +0 -138
- package/dist/generators/generate-files.js +0 -79
- package/dist/generators/generate-non-discriminated-union-validation.d.ts +0 -6
- package/dist/generators/generate-non-discriminated-union-validation.js +0 -26
- package/dist/generators/generate-parser-function.d.ts +0 -120
- package/dist/generators/generate-parser-function.js +0 -1955
- package/dist/generators/generate-schema-checks.d.ts +0 -5
- package/dist/generators/generate-schema-checks.js +0 -260
- package/dist/generators/generate-strict-assertion.d.ts +0 -155
- package/dist/generators/generate-strict-assertion.js +0 -759
- package/dist/generators/generate-type-checks.d.ts +0 -148
- package/dist/generators/generate-type-checks.js +0 -454
- package/dist/generators/generate-unique-items-check.d.ts +0 -16
- package/dist/generators/generate-unique-items-check.js +0 -32
- package/dist/generators/generate-validation-expression.d.ts +0 -79
- package/dist/generators/generate-validation-expression.js +0 -416
- package/dist/generators/subschema-match.d.ts +0 -48
- package/dist/generators/subschema-match.js +0 -485
- package/dist/generators/unevaluated-match.d.ts +0 -19
- package/dist/generators/unevaluated-match.js +0 -206
- package/dist/helpers/apply-schema-extensions.d.ts +0 -35
- package/dist/helpers/apply-schema-extensions.js +0 -20
- package/dist/helpers/collect-helpers.d.ts +0 -15
- package/dist/helpers/collect-helpers.js +0 -64
- package/dist/helpers/collect-imports.d.ts +0 -93
- package/dist/helpers/collect-imports.js +0 -197
- package/dist/helpers/find-discriminator.d.ts +0 -6
- package/dist/helpers/find-discriminator.js +0 -27
- package/dist/helpers/get-default-value.d.ts +0 -7
- package/dist/helpers/get-default-value.js +0 -155
- package/dist/helpers/get-discriminator-value.d.ts +0 -6
- package/dist/helpers/get-discriminator-value.js +0 -18
- package/dist/types/schema-extensions.d.ts +0 -34
- package/dist/types/schema-extensions.js +0 -0
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { getMjstPrimitive } from "@amritk/helpers/mjst-extension";
|
|
2
|
-
import { hasAllOf, hasAnyOf, hasConst, hasDefault, hasEnum, hasExamples, hasOneOf, hasPattern, hasProperties, hasRequired, hasType, isSchemaObject } from "@amritk/helpers/schema-guards";
|
|
3
|
-
import { generateDefaultFromPattern } from "../generators/generate-default-from-pattern.js";
|
|
4
|
-
const defaultMatchesType = (value, schema) => {
|
|
5
|
-
if (!hasType(schema))
|
|
6
|
-
return true;
|
|
7
|
-
switch (schema.type) {
|
|
8
|
-
case "string":
|
|
9
|
-
return typeof value === "string";
|
|
10
|
-
case "number":
|
|
11
|
-
return typeof value === "number";
|
|
12
|
-
case "integer":
|
|
13
|
-
return typeof value === "number" && Number.isInteger(value);
|
|
14
|
-
case "boolean":
|
|
15
|
-
return typeof value === "boolean";
|
|
16
|
-
case "null":
|
|
17
|
-
return value === null;
|
|
18
|
-
case "array":
|
|
19
|
-
return Array.isArray(value);
|
|
20
|
-
case "object":
|
|
21
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
22
|
-
default:
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
const MAX_FALLBACK_ITEMS = 32;
|
|
27
|
-
const arrayFallback = (schema) => {
|
|
28
|
-
const record = schema;
|
|
29
|
-
const positions = Array.isArray(Object.hasOwn(record, "prefixItems") ? record["prefixItems"] : void 0) ? Object.hasOwn(record, "prefixItems") ? record["prefixItems"] : void 0 : Array.isArray(Object.hasOwn(record, "items") ? record["items"] : void 0) ? Object.hasOwn(record, "items") ? record["items"] : void 0 : [];
|
|
30
|
-
const tail = Array.isArray(Object.hasOwn(record, "items") ? record["items"] : void 0) ? Object.hasOwn(record, "additionalItems") ? record["additionalItems"] : void 0 : Object.hasOwn(record, "items") ? record["items"] : void 0;
|
|
31
|
-
const minItems = typeof record["minItems"] === "number" ? record["minItems"] : 0;
|
|
32
|
-
const wanted = minItems > MAX_FALLBACK_ITEMS ? Math.min(minItems, positions.length) : minItems;
|
|
33
|
-
if (wanted === 0)
|
|
34
|
-
return "[]";
|
|
35
|
-
const elements = [];
|
|
36
|
-
for (let i = 0; i < wanted; i++) {
|
|
37
|
-
const elementSchema = i < positions.length ? positions[i] : tail;
|
|
38
|
-
if (elementSchema === void 0 || typeof elementSchema === "boolean")
|
|
39
|
-
return "[]";
|
|
40
|
-
const element = getDefaultValue(elementSchema);
|
|
41
|
-
if (element === "undefined")
|
|
42
|
-
return "[]";
|
|
43
|
-
elements.push(element);
|
|
44
|
-
}
|
|
45
|
-
return `[${elements.join(", ")}]`;
|
|
46
|
-
};
|
|
47
|
-
const MAX_FALLBACK_LENGTH = 256;
|
|
48
|
-
const stringFallback = (schema) => {
|
|
49
|
-
const bounds = schema;
|
|
50
|
-
if (hasPattern(schema)) {
|
|
51
|
-
const patternDefault = generateDefaultFromPattern(schema.pattern, typeof bounds["minLength"] === "number" ? bounds["minLength"] : void 0, typeof bounds["maxLength"] === "number" ? bounds["maxLength"] : void 0);
|
|
52
|
-
if (patternDefault)
|
|
53
|
-
return patternDefault;
|
|
54
|
-
return '""';
|
|
55
|
-
}
|
|
56
|
-
const minLength = schema["minLength"];
|
|
57
|
-
if (typeof minLength !== "number" || !(minLength > 0))
|
|
58
|
-
return '""';
|
|
59
|
-
return JSON.stringify("x".repeat(Math.min(minLength, MAX_FALLBACK_LENGTH)));
|
|
60
|
-
};
|
|
61
|
-
const finiteKeyword = (schema, keyword) => {
|
|
62
|
-
const value = schema[keyword];
|
|
63
|
-
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
64
|
-
};
|
|
65
|
-
const numberFallback = (schema, integer) => {
|
|
66
|
-
const minimum = finiteKeyword(schema, "minimum");
|
|
67
|
-
const maximum = finiteKeyword(schema, "maximum");
|
|
68
|
-
const exclusiveMinimum = finiteKeyword(schema, "exclusiveMinimum");
|
|
69
|
-
const exclusiveMaximum = finiteKeyword(schema, "exclusiveMaximum");
|
|
70
|
-
const multipleOf = finiteKeyword(schema, "multipleOf");
|
|
71
|
-
const low = Math.max(minimum ?? -Infinity, exclusiveMinimum !== void 0 ? exclusiveMinimum + 1 : -Infinity);
|
|
72
|
-
const high = Math.min(maximum ?? Infinity, exclusiveMaximum !== void 0 ? exclusiveMaximum - 1 : Infinity);
|
|
73
|
-
let candidate = Math.min(Math.max(0, low), high);
|
|
74
|
-
if (integer)
|
|
75
|
-
candidate = Math.ceil(candidate);
|
|
76
|
-
if (multipleOf !== void 0 && multipleOf > 0 && candidate !== 0) {
|
|
77
|
-
candidate = Math.ceil(candidate / multipleOf) * multipleOf;
|
|
78
|
-
}
|
|
79
|
-
const satisfies = Number.isFinite(candidate) && (minimum === void 0 || candidate >= minimum) && (maximum === void 0 || candidate <= maximum) && (exclusiveMinimum === void 0 || candidate > exclusiveMinimum) && (exclusiveMaximum === void 0 || candidate < exclusiveMaximum) && (!integer || Number.isInteger(candidate)) && (multipleOf === void 0 || multipleOf <= 0 || Number.isInteger(candidate / multipleOf));
|
|
80
|
-
return satisfies ? String(candidate) : "0";
|
|
81
|
-
};
|
|
82
|
-
const getDefaultValue = (schema) => {
|
|
83
|
-
if (!isSchemaObject(schema)) {
|
|
84
|
-
return "undefined";
|
|
85
|
-
}
|
|
86
|
-
if (hasDefault(schema) && defaultMatchesType(schema.default, schema)) {
|
|
87
|
-
return JSON.stringify(schema.default);
|
|
88
|
-
}
|
|
89
|
-
if (hasConst(schema)) {
|
|
90
|
-
return JSON.stringify(schema.const);
|
|
91
|
-
}
|
|
92
|
-
if (hasEnum(schema) && schema.enum.length > 0) {
|
|
93
|
-
return JSON.stringify(schema.enum[0]);
|
|
94
|
-
}
|
|
95
|
-
if (hasExamples(schema) && schema.examples.length > 0) {
|
|
96
|
-
return JSON.stringify(schema.examples[0]);
|
|
97
|
-
}
|
|
98
|
-
if (hasOneOf(schema) && schema.oneOf.length > 0) {
|
|
99
|
-
const firstSchema = schema.oneOf[0];
|
|
100
|
-
if (firstSchema !== void 0) {
|
|
101
|
-
return getDefaultValue(firstSchema);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
if (hasAnyOf(schema) && schema.anyOf.length > 0) {
|
|
105
|
-
const firstSchema = schema.anyOf[0];
|
|
106
|
-
if (firstSchema !== void 0) {
|
|
107
|
-
return getDefaultValue(firstSchema);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
if (hasAllOf(schema) && schema.allOf.length > 0) {
|
|
111
|
-
const firstSchema = schema.allOf[0];
|
|
112
|
-
if (firstSchema !== void 0) {
|
|
113
|
-
return getDefaultValue(firstSchema);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (getMjstPrimitive(schema) === "bigint") {
|
|
117
|
-
return "0n";
|
|
118
|
-
}
|
|
119
|
-
const rawType = schema["type"];
|
|
120
|
-
if (Array.isArray(rawType) && rawType.length > 0 && typeof rawType[0] === "string") {
|
|
121
|
-
return getDefaultValue({ ...schema, type: rawType[0] });
|
|
122
|
-
}
|
|
123
|
-
if (!hasType(schema)) {
|
|
124
|
-
return "undefined";
|
|
125
|
-
}
|
|
126
|
-
switch (schema.type) {
|
|
127
|
-
case "string":
|
|
128
|
-
return stringFallback(schema);
|
|
129
|
-
case "number":
|
|
130
|
-
case "integer":
|
|
131
|
-
return numberFallback(schema, schema.type === "integer");
|
|
132
|
-
case "boolean":
|
|
133
|
-
return "false";
|
|
134
|
-
case "null":
|
|
135
|
-
return "null";
|
|
136
|
-
case "array":
|
|
137
|
-
return arrayFallback(schema);
|
|
138
|
-
case "object": {
|
|
139
|
-
if (hasProperties(schema) && hasRequired(schema)) {
|
|
140
|
-
const props = schema.properties;
|
|
141
|
-
const required = schema.required.filter((key) => Object.hasOwn(props, key));
|
|
142
|
-
const defaults = required.map((key) => getDefaultValue(props[key]));
|
|
143
|
-
if (defaults.length > 0 && !defaults.includes("undefined")) {
|
|
144
|
-
return `{ ${required.map((key, i) => `${JSON.stringify(key)}: ${defaults[i]}`).join(", ")} }`;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return "{}";
|
|
148
|
-
}
|
|
149
|
-
default:
|
|
150
|
-
return "undefined";
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
export {
|
|
154
|
-
getDefaultValue
|
|
155
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
2
|
-
/**
|
|
3
|
-
* Gets the discriminator value for a specific schema.
|
|
4
|
-
* Returns the const value or single enum value if available.
|
|
5
|
-
*/
|
|
6
|
-
export declare const getDiscriminatorValue: (schema: JSONSchema, discriminatorKey: string) => unknown | null;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { hasConst, hasEnum, hasProperties, isObjectSchema, isSchemaObject } from "@amritk/helpers/schema-guards";
|
|
2
|
-
const getDiscriminatorValue = (schema, discriminatorKey) => {
|
|
3
|
-
if (isObjectSchema(schema) && hasProperties(schema)) {
|
|
4
|
-
const propSchema = schema.properties[discriminatorKey];
|
|
5
|
-
if (propSchema && isSchemaObject(propSchema)) {
|
|
6
|
-
if (hasConst(propSchema)) {
|
|
7
|
-
return propSchema.const;
|
|
8
|
-
}
|
|
9
|
-
if (hasEnum(propSchema) && propSchema.enum.length === 1) {
|
|
10
|
-
return propSchema.enum[0];
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return null;
|
|
15
|
-
};
|
|
16
|
-
export {
|
|
17
|
-
getDiscriminatorValue
|
|
18
|
-
};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
2
|
-
/**
|
|
3
|
-
* Defines custom extension properties that can be added to specific schema
|
|
4
|
-
* definitions during the build process.
|
|
5
|
-
*
|
|
6
|
-
* The outer key is the definition name (matching the $defs key in the root
|
|
7
|
-
* schema, e.g., "parameter", "operation"). The inner key is the extension
|
|
8
|
-
* property name (e.g., "x-enabled", "x-internal"). The value is a JSON
|
|
9
|
-
* Schema that validates the extension property and generates its TypeScript type.
|
|
10
|
-
*
|
|
11
|
-
* Extensions are merged into the schema properties before type and parser
|
|
12
|
-
* generation, so each extension gets full type safety and runtime validation.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* const extensions: SchemaExtensions = {
|
|
17
|
-
* parameter: {
|
|
18
|
-
* 'x-enabled': { type: 'boolean' },
|
|
19
|
-
* 'x-internal': { type: 'boolean', default: false },
|
|
20
|
-
* },
|
|
21
|
-
* operation: {
|
|
22
|
-
* 'x-codegen': {
|
|
23
|
-
* type: 'object',
|
|
24
|
-
* properties: {
|
|
25
|
-
* methodName: { type: 'string' },
|
|
26
|
-
* },
|
|
27
|
-
* },
|
|
28
|
-
* },
|
|
29
|
-
* }
|
|
30
|
-
*
|
|
31
|
-
* const files = buildSchema(schema, 'Document', markdownDocumentation, extensions)
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export type SchemaExtensions = Record<string, Record<string, JSONSchema>>;
|
|
File without changes
|