@kravc/schema 2.8.0-alpha.6 → 2.8.0-alpha.8
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/dist/CredentialFactory.d.ts +5 -318
- package/dist/CredentialFactory.d.ts.map +1 -1
- package/dist/CredentialFactory.js +5 -317
- package/dist/CredentialFactory.js.map +1 -1
- package/dist/Schema.d.ts +15 -420
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +13 -384
- package/dist/Schema.js.map +1 -1
- package/dist/ValidationError.d.ts +3 -49
- package/dist/ValidationError.d.ts.map +1 -1
- package/dist/ValidationError.js +3 -48
- package/dist/ValidationError.js.map +1 -1
- package/dist/Validator.d.ts +6 -470
- package/dist/Validator.d.ts.map +1 -1
- package/dist/Validator.js +50 -478
- package/dist/Validator.js.map +1 -1
- package/dist/helpers/cleanupAttributes.d.ts +3 -32
- package/dist/helpers/cleanupAttributes.d.ts.map +1 -1
- package/dist/helpers/cleanupAttributes.js +1 -30
- package/dist/helpers/cleanupAttributes.js.map +1 -1
- package/dist/helpers/cleanupNulls.d.ts +1 -25
- package/dist/helpers/cleanupNulls.d.ts.map +1 -1
- package/dist/helpers/cleanupNulls.js +2 -61
- package/dist/helpers/cleanupNulls.js.map +1 -1
- package/dist/helpers/createSchemasMap.d.ts +2 -93
- package/dist/helpers/createSchemasMap.d.ts.map +1 -1
- package/dist/helpers/createSchemasMap.js +4 -162
- package/dist/helpers/createSchemasMap.js.map +1 -1
- package/dist/helpers/getReferenceIds.d.ts +1 -165
- package/dist/helpers/getReferenceIds.d.ts.map +1 -1
- package/dist/helpers/getReferenceIds.js +2 -166
- package/dist/helpers/getReferenceIds.js.map +1 -1
- package/dist/helpers/got.d.ts +2 -58
- package/dist/helpers/got.d.ts.map +1 -1
- package/dist/helpers/got.js +1 -57
- package/dist/helpers/got.js.map +1 -1
- package/dist/helpers/mapObjectProperties.d.ts +2 -144
- package/dist/helpers/mapObjectProperties.d.ts.map +1 -1
- package/dist/helpers/mapObjectProperties.js +1 -142
- package/dist/helpers/mapObjectProperties.js.map +1 -1
- package/dist/helpers/normalizeAttributes.d.ts +3 -211
- package/dist/helpers/normalizeAttributes.d.ts.map +1 -1
- package/dist/helpers/normalizeAttributes.js +1 -209
- package/dist/helpers/normalizeAttributes.js.map +1 -1
- package/dist/helpers/normalizeProperties.d.ts +1 -165
- package/dist/helpers/normalizeProperties.d.ts.map +1 -1
- package/dist/helpers/normalizeProperties.js +1 -164
- package/dist/helpers/normalizeProperties.js.map +1 -1
- package/dist/helpers/normalizeRequired.d.ts +1 -153
- package/dist/helpers/normalizeRequired.d.ts.map +1 -1
- package/dist/helpers/normalizeRequired.js +0 -151
- package/dist/helpers/normalizeRequired.js.map +1 -1
- package/dist/helpers/normalizeType.d.ts +1 -77
- package/dist/helpers/normalizeType.d.ts.map +1 -1
- package/dist/helpers/normalizeType.js +11 -139
- package/dist/helpers/normalizeType.js.map +1 -1
- package/dist/helpers/nullifyEmptyValues.d.ts +1 -135
- package/dist/helpers/nullifyEmptyValues.d.ts.map +1 -1
- package/dist/helpers/nullifyEmptyValues.js +13 -143
- package/dist/helpers/nullifyEmptyValues.js.map +1 -1
- package/dist/helpers/removeRequiredAndDefault.d.ts +2 -102
- package/dist/helpers/removeRequiredAndDefault.d.ts.map +1 -1
- package/dist/helpers/removeRequiredAndDefault.js +1 -100
- package/dist/helpers/removeRequiredAndDefault.js.map +1 -1
- package/dist/helpers/validateId.d.ts +1 -36
- package/dist/helpers/validateId.d.ts.map +1 -1
- package/dist/helpers/validateId.js +1 -36
- package/dist/helpers/validateId.js.map +1 -1
- package/dist/ld/documentLoader.d.ts +1 -1
- package/dist/ld/documentLoader.d.ts.map +1 -1
- package/dist/ld/documentLoader.js +1 -1
- package/dist/ld/documentLoader.js.map +1 -1
- package/dist/ld/getLinkedDataAttributeType.d.ts +1 -1
- package/dist/ld/getLinkedDataAttributeType.d.ts.map +1 -1
- package/dist/ld/getLinkedDataAttributeType.js +1 -1
- package/dist/ld/getLinkedDataAttributeType.js.map +1 -1
- package/dist/ld/getLinkedDataContext.d.ts +1 -1
- package/dist/ld/getLinkedDataContext.d.ts.map +1 -1
- package/dist/ld/getLinkedDataContext.js +1 -1
- package/dist/ld/getLinkedDataContext.js.map +1 -1
- package/package.json +2 -2
- package/src/CredentialFactory.ts +5 -318
- package/src/Schema.ts +17 -427
- package/src/ValidationError.ts +5 -52
- package/src/Validator.ts +19 -483
- package/src/__tests__/CredentialFactory.test.ts +1 -1
- package/src/__tests__/Schema.test.ts +3 -8
- package/src/__tests__/ValidationError.test.ts +4 -2
- package/src/__tests__/Validator.test.ts +21 -4
- package/src/helpers/__tests__/cleanupAttributes.test.ts +3 -1
- package/src/helpers/__tests__/createSchemasMap.test.ts +1 -1
- package/src/helpers/__tests__/mapObjectProperties.test.ts +2 -1
- package/src/helpers/__tests__/normalizeAttributes.test.ts +4 -2
- package/src/helpers/__tests__/normalizeProperties.test.ts +3 -1
- package/src/helpers/__tests__/normalizeRequired.test.ts +4 -9
- package/src/helpers/__tests__/nullifyEmptyValues.test.ts +43 -12
- package/src/helpers/__tests__/removeRequiredAndDefault.test.ts +3 -10
- package/src/helpers/cleanupAttributes.ts +6 -44
- package/src/helpers/cleanupNulls.ts +2 -63
- package/src/helpers/createSchemasMap.ts +4 -163
- package/src/helpers/getReferenceIds.ts +2 -173
- package/src/helpers/got.ts +3 -59
- package/src/helpers/mapObjectProperties.ts +4 -156
- package/src/helpers/normalizeAttributes.ts +7 -211
- package/src/helpers/normalizeProperties.ts +1 -172
- package/src/helpers/normalizeRequired.ts +1 -161
- package/src/helpers/normalizeType.ts +11 -139
- package/src/helpers/nullifyEmptyValues.ts +10 -145
- package/src/helpers/removeRequiredAndDefault.ts +1 -106
- package/src/helpers/validateId.ts +1 -36
- package/src/ld/documentLoader.ts +1 -1
- package/src/ld/getLinkedDataAttributeType.ts +1 -1
- package/src/ld/getLinkedDataContext.ts +1 -1
- package/src/{helpers/JsonSchema.ts → types.d.ts} +12 -16
- package/dist/helpers/JsonSchema.d.ts +0 -99
- package/dist/helpers/JsonSchema.d.ts.map +0 -1
- package/dist/helpers/JsonSchema.js +0 -3
- package/dist/helpers/JsonSchema.js.map +0 -1
|
@@ -1,170 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const lodash_1 = require("lodash");
|
|
4
|
-
/**
|
|
5
|
-
* Normalizes JSON schema properties by ensuring all properties have an explicit `type` attribute.
|
|
6
|
-
*
|
|
7
|
-
* **Intent:**
|
|
8
|
-
* This function transforms schemas that may have implicit or missing type information into
|
|
9
|
-
* fully normalized schemas with explicit types. It recursively processes nested structures
|
|
10
|
-
* (objects and arrays) to ensure type consistency throughout the schema hierarchy.
|
|
11
|
-
*
|
|
12
|
-
* **Use Cases:**
|
|
13
|
-
* - **Schema Validation Preparation**: Ensures schemas are ready for validation where type
|
|
14
|
-
* information is required by validators or processing tools
|
|
15
|
-
* - **External Schema Normalization**: Normalizes schemas imported from external sources
|
|
16
|
-
* (e.g., OpenAPI specs, YAML schemas) that may omit type information
|
|
17
|
-
* - **Type Inference**: Automatically infers types from structural hints (e.g., presence of
|
|
18
|
-
* `properties` implies `object`, presence of `items` implies `array`)
|
|
19
|
-
* - **Schema Consistency**: Guarantees consistent schema structure before further processing
|
|
20
|
-
* or transformation
|
|
21
|
-
* - **Default Type Assignment**: Provides sensible defaults (e.g., `string` for primitives)
|
|
22
|
-
* when type cannot be inferred
|
|
23
|
-
*
|
|
24
|
-
* **Behavior:**
|
|
25
|
-
* - **Enum Schemas**: Sets `type` to `'string'` if missing (preserves existing type if present)
|
|
26
|
-
* - **Reference Properties**: Skips `$ref` properties (they don't need type normalization)
|
|
27
|
-
* - **Type Inference Priority** (when type is missing):
|
|
28
|
-
* 1. If property has `properties` → sets `type: 'object'`
|
|
29
|
-
* 2. Else if property has `items` → sets `type: 'array'`
|
|
30
|
-
* 3. Else → sets `type: 'string'` (default)
|
|
31
|
-
* - **Object Properties**:
|
|
32
|
-
* - Infers `type: 'object'` from presence of `properties` (if type not already set)
|
|
33
|
-
* - Creates empty `properties: {}` if `type: 'object'` but no properties exist
|
|
34
|
-
* - Recursively normalizes nested object properties
|
|
35
|
-
* - Note: If both `properties` and `items` exist, `properties` takes precedence
|
|
36
|
-
* - **Array Properties**:
|
|
37
|
-
* - Infers `type: 'array'` from presence of `items` (if type not already set)
|
|
38
|
-
* - Sets default `items: { type: 'string' }` if array has no items
|
|
39
|
-
* - Normalizes item schemas: sets `type: 'object'` if items have `properties` (not undefined)
|
|
40
|
-
* - `properties: null` → treated as having properties (sets type to 'object')
|
|
41
|
-
* - `properties: undefined` → treated as not having properties (no type set)
|
|
42
|
-
* - Empty object `{}` → treated as not having properties (no type set)
|
|
43
|
-
* - Recursively normalizes nested properties within array items
|
|
44
|
-
* - **Primitive Properties**: Sets `type: 'string'` as default when no type, items, or properties exist
|
|
45
|
-
* - **Existing Types**: Never overrides existing type values (even if structure suggests different type)
|
|
46
|
-
* - **Edge Cases**:
|
|
47
|
-
* - Empty schemas (`{}`) are handled gracefully
|
|
48
|
-
* - Properties with conflicting structure (e.g., `type: 'object'` with `items`) are normalized
|
|
49
|
-
* according to the explicit type, ignoring conflicting structural hints
|
|
50
|
-
*
|
|
51
|
-
* **Examples:**
|
|
52
|
-
*
|
|
53
|
-
* @example Enum schema normalization
|
|
54
|
-
* ```typescript
|
|
55
|
-
* const schema: EnumSchema = { enum: ['red', 'green', 'blue'] };
|
|
56
|
-
* normalizeProperties(schema);
|
|
57
|
-
* // Result: { enum: ['red', 'green', 'blue'], type: 'string' }
|
|
58
|
-
* ```
|
|
59
|
-
*
|
|
60
|
-
* @example Object type inference
|
|
61
|
-
* ```typescript
|
|
62
|
-
* const schema: PropertiesSchema = {
|
|
63
|
-
* user: {
|
|
64
|
-
* properties: {
|
|
65
|
-
* name: {}
|
|
66
|
-
* }
|
|
67
|
-
* }
|
|
68
|
-
* };
|
|
69
|
-
* normalizeProperties(schema);
|
|
70
|
-
* // Result:
|
|
71
|
-
* // {
|
|
72
|
-
* // user: {
|
|
73
|
-
* // type: 'object',
|
|
74
|
-
* // properties: {
|
|
75
|
-
* // name: { type: 'string' }
|
|
76
|
-
* // }
|
|
77
|
-
* // }
|
|
78
|
-
* // }
|
|
79
|
-
* ```
|
|
80
|
-
*
|
|
81
|
-
* @example Array type inference
|
|
82
|
-
* ```typescript
|
|
83
|
-
* const schema: PropertiesSchema = {
|
|
84
|
-
* tags: {
|
|
85
|
-
* items: { type: 'string' }
|
|
86
|
-
* }
|
|
87
|
-
* };
|
|
88
|
-
* normalizeProperties(schema);
|
|
89
|
-
* // Result:
|
|
90
|
-
* // {
|
|
91
|
-
* // tags: {
|
|
92
|
-
* // type: 'array',
|
|
93
|
-
* // items: { type: 'string' }
|
|
94
|
-
* // }
|
|
95
|
-
* // }
|
|
96
|
-
* ```
|
|
97
|
-
*
|
|
98
|
-
* @example Complex nested structure
|
|
99
|
-
* ```typescript
|
|
100
|
-
* const schema: PropertiesSchema = {
|
|
101
|
-
* profile: {
|
|
102
|
-
* properties: {
|
|
103
|
-
* name: {},
|
|
104
|
-
* addresses: {
|
|
105
|
-
* items: {
|
|
106
|
-
* properties: {
|
|
107
|
-
* street: {},
|
|
108
|
-
* city: {}
|
|
109
|
-
* }
|
|
110
|
-
* }
|
|
111
|
-
* }
|
|
112
|
-
* }
|
|
113
|
-
* }
|
|
114
|
-
* };
|
|
115
|
-
* normalizeProperties(schema);
|
|
116
|
-
* // Result:
|
|
117
|
-
* // {
|
|
118
|
-
* // profile: {
|
|
119
|
-
* // type: 'object',
|
|
120
|
-
* // properties: {
|
|
121
|
-
* // name: { type: 'string' },
|
|
122
|
-
* // addresses: {
|
|
123
|
-
* // type: 'array',
|
|
124
|
-
* // items: {
|
|
125
|
-
* // type: 'object',
|
|
126
|
-
* // properties: {
|
|
127
|
-
* // street: { type: 'string' },
|
|
128
|
-
* // city: { type: 'string' }
|
|
129
|
-
* // }
|
|
130
|
-
* // }
|
|
131
|
-
* // }
|
|
132
|
-
* // }
|
|
133
|
-
* // }
|
|
134
|
-
* // }
|
|
135
|
-
* ```
|
|
136
|
-
*
|
|
137
|
-
* @example Reference properties are skipped
|
|
138
|
-
* ```typescript
|
|
139
|
-
* const schema: PropertiesSchema = {
|
|
140
|
-
* refField: { $ref: '#/definitions/User' },
|
|
141
|
-
* normalField: {}
|
|
142
|
-
* };
|
|
143
|
-
* normalizeProperties(schema);
|
|
144
|
-
* // Result:
|
|
145
|
-
* // {
|
|
146
|
-
* // refField: { $ref: '#/definitions/User' }, // Unchanged
|
|
147
|
-
* // normalField: { type: 'string' }
|
|
148
|
-
* // }
|
|
149
|
-
* ```
|
|
150
|
-
*
|
|
151
|
-
* @example Default type assignment
|
|
152
|
-
* ```typescript
|
|
153
|
-
* const schema: PropertiesSchema = {
|
|
154
|
-
* title: {},
|
|
155
|
-
* count: { type: 'number' }
|
|
156
|
-
* };
|
|
157
|
-
* normalizeProperties(schema);
|
|
158
|
-
* // Result:
|
|
159
|
-
* // {
|
|
160
|
-
* // title: { type: 'string' }, // Default assigned
|
|
161
|
-
* // count: { type: 'number' } // Existing preserved
|
|
162
|
-
* // }
|
|
163
|
-
* ```
|
|
164
|
-
*
|
|
165
|
-
* @param schema - The schema to normalize (either an EnumSchema or PropertiesSchema)
|
|
166
|
-
* @modifies The schema object is mutated in place with normalized types
|
|
167
|
-
*/
|
|
4
|
+
/** Normalizes JSON schema properties by ensuring all properties have an explicit `type` attribute. */
|
|
168
5
|
const normalizeProperties = (schema) => {
|
|
169
6
|
const { enum: isEnum } = schema;
|
|
170
7
|
if (isEnum) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeProperties.js","sourceRoot":"","sources":["../../src/helpers/normalizeProperties.ts"],"names":[],"mappings":";;AAAA,mCAA+C;
|
|
1
|
+
{"version":3,"file":"normalizeProperties.js","sourceRoot":"","sources":["../../src/helpers/normalizeProperties.ts"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G,EAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAEnC,MAAM,QAAQ,GAAG,IAAI,KAAK,QAAQ,CAAC;QAEnC,IAAI,QAAQ,EAAE,CAAC;YACb,IAAI,CAAC,aAAa,EAAE,CAAC;gBAClB,QAAiC,CAAC,UAAU,GAAG,EAAE,CAAC;YACrD,CAAC;YAED,qGAAqG;YACrG,mBAAmB,CAAE,QAAiC,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC;QAC3E,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,KAAK,OAAO,CAAC;QAEjC,IAAI,OAAO,EAAE,CAAC;YACZ,IAAI,QAAQ,EAAE,CAAC;gBACb,oGAAoG;gBACpG,MAAM,EAAE,KAAK,GAAG,EAAE,EAAE,GAAI,QAAgC,CAAC;gBAEzD,MAAM,YAAY,GAAG,CAAC,IAAA,oBAAW,EAAE,KAA8B,CAAC,UAAU,CAAC,CAAC;gBAE9E,IAAI,YAAY,EAAE,CAAC;oBACjB,IAAA,YAAG,EAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC;oBAE7B,mBAAmB,CAAE,KAA8B,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC;gBACxE,CAAC;YAEH,CAAC;iBAAM,CAAC;gBACL,QAAgC,CAAC,KAAK,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;YAE/D,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAEF,kBAAe,mBAAmB,CAAC"}
|
|
@@ -1,159 +1,7 @@
|
|
|
1
|
-
import type { JsonSchema, ObjectPropertySchema, ReferencePropertySchema } from './JsonSchema';
|
|
2
1
|
/**
|
|
3
2
|
* Normalizes required field declarations in JSON schemas by converting property-level
|
|
4
3
|
* `required` flags to schema-level `required` arrays and `x-required` metadata flags.
|
|
5
|
-
*
|
|
6
|
-
* **Intent:**
|
|
7
|
-
* This function transforms JSON schemas from a property-centric required field model
|
|
8
|
-
* (where each property has its own `required: true/false` flag) to the standard JSON Schema
|
|
9
|
-
* format (where required fields are listed in a top-level `required` array). This normalization
|
|
10
|
-
* ensures compatibility with JSON Schema validators while preserving the original required
|
|
11
|
-
* field information through the `x-required` extension attribute.
|
|
12
|
-
*
|
|
13
|
-
* **Use Cases:**
|
|
14
|
-
* 1. **Schema Standardization**: Convert custom schema formats to standard JSON Schema format
|
|
15
|
-
* for validator compatibility
|
|
16
|
-
* 2. **Schema Transformation**: Prepare schemas for validation libraries that expect
|
|
17
|
-
* required fields in array format
|
|
18
|
-
* 3. **Metadata Preservation**: Maintain required field information in both standard format
|
|
19
|
-
* (`required` array) and extension format (`x-required` flag) for different use cases
|
|
20
|
-
* 4. **Schema Processing Pipeline**: Normalize schemas before validation, credential generation,
|
|
21
|
-
* or API documentation generation
|
|
22
|
-
*
|
|
23
|
-
* **Behavior:**
|
|
24
|
-
* - Mutates the input schema in-place
|
|
25
|
-
* - Moves `required: true` from property level to schema-level `required` array
|
|
26
|
-
* - Sets `x-required: true` on properties that were marked as required
|
|
27
|
-
* - Deletes the `required` property from individual property schemas
|
|
28
|
-
* - Recursively processes nested object properties
|
|
29
|
-
* - Recursively processes array items (including nested objects within arrays)
|
|
30
|
-
* - Skips reference properties (`$ref`) as they are resolved elsewhere
|
|
31
|
-
* - Skips EnumSchema (returns early)
|
|
32
|
-
* - Only sets `required` array if at least one field is required
|
|
33
|
-
*
|
|
34
|
-
* **Examples:**
|
|
35
|
-
*
|
|
36
|
-
* ```typescript
|
|
37
|
-
* // Example 1: Simple object schema
|
|
38
|
-
* const schema = {
|
|
39
|
-
* id: 'User',
|
|
40
|
-
* properties: {
|
|
41
|
-
* name: { type: 'string', required: true },
|
|
42
|
-
* email: { type: 'string', required: true },
|
|
43
|
-
* age: { type: 'number', required: false }
|
|
44
|
-
* }
|
|
45
|
-
* };
|
|
46
|
-
*
|
|
47
|
-
* normalizeRequired(schema);
|
|
48
|
-
* // Result:
|
|
49
|
-
* // schema.required = ['name', 'email']
|
|
50
|
-
* // schema.properties.name['x-required'] = true
|
|
51
|
-
* // schema.properties.email['x-required'] = true
|
|
52
|
-
* // schema.properties.name.required = undefined (deleted)
|
|
53
|
-
* // schema.properties.email.required = undefined (deleted)
|
|
54
|
-
* ```
|
|
55
|
-
*
|
|
56
|
-
* ```typescript
|
|
57
|
-
* // Example 2: Nested objects
|
|
58
|
-
* const schema = {
|
|
59
|
-
* id: 'Order',
|
|
60
|
-
* properties: {
|
|
61
|
-
* user: {
|
|
62
|
-
* type: 'object',
|
|
63
|
-
* required: true,
|
|
64
|
-
* properties: {
|
|
65
|
-
* name: { type: 'string', required: true },
|
|
66
|
-
* address: {
|
|
67
|
-
* type: 'object',
|
|
68
|
-
* properties: {
|
|
69
|
-
* street: { type: 'string', required: true }
|
|
70
|
-
* }
|
|
71
|
-
* }
|
|
72
|
-
* }
|
|
73
|
-
* }
|
|
74
|
-
* }
|
|
75
|
-
* };
|
|
76
|
-
*
|
|
77
|
-
* normalizeRequired(schema);
|
|
78
|
-
* // Result:
|
|
79
|
-
* // schema.required = ['user']
|
|
80
|
-
* // schema.properties.user['x-required'] = true
|
|
81
|
-
* // schema.properties.user.required = ['name']
|
|
82
|
-
* // schema.properties.user.properties.name['x-required'] = true
|
|
83
|
-
* // schema.properties.user.properties.address.required = ['street']
|
|
84
|
-
* // schema.properties.user.properties.address.properties.street['x-required'] = true
|
|
85
|
-
* ```
|
|
86
|
-
*
|
|
87
|
-
* ```typescript
|
|
88
|
-
* // Example 3: Arrays with object items
|
|
89
|
-
* const schema = {
|
|
90
|
-
* id: 'Order',
|
|
91
|
-
* properties: {
|
|
92
|
-
* items: {
|
|
93
|
-
* type: 'array',
|
|
94
|
-
* required: true,
|
|
95
|
-
* items: {
|
|
96
|
-
* type: 'object',
|
|
97
|
-
* properties: {
|
|
98
|
-
* productId: { type: 'string', required: true },
|
|
99
|
-
* quantity: { type: 'number', required: true }
|
|
100
|
-
* }
|
|
101
|
-
* }
|
|
102
|
-
* }
|
|
103
|
-
* }
|
|
104
|
-
* };
|
|
105
|
-
*
|
|
106
|
-
* normalizeRequired(schema);
|
|
107
|
-
* // Result:
|
|
108
|
-
* // schema.required = ['items']
|
|
109
|
-
* // schema.properties.items['x-required'] = true
|
|
110
|
-
* // schema.properties.items.items.required = ['productId', 'quantity']
|
|
111
|
-
* // schema.properties.items.items.properties.productId['x-required'] = true
|
|
112
|
-
* // schema.properties.items.items.properties.quantity['x-required'] = true
|
|
113
|
-
* ```
|
|
114
|
-
*
|
|
115
|
-
* ```typescript
|
|
116
|
-
* // Example 4: Mixed structure
|
|
117
|
-
* const schema = {
|
|
118
|
-
* id: 'Profile',
|
|
119
|
-
* properties: {
|
|
120
|
-
* name: { type: 'string', required: true },
|
|
121
|
-
* address: {
|
|
122
|
-
* type: 'object',
|
|
123
|
-
* required: true,
|
|
124
|
-
* properties: {
|
|
125
|
-
* street: { type: 'string', required: true },
|
|
126
|
-
* city: { type: 'string' }
|
|
127
|
-
* }
|
|
128
|
-
* },
|
|
129
|
-
* tags: {
|
|
130
|
-
* type: 'array',
|
|
131
|
-
* items: {
|
|
132
|
-
* type: 'object',
|
|
133
|
-
* properties: {
|
|
134
|
-
* label: { type: 'string', required: true }
|
|
135
|
-
* }
|
|
136
|
-
* }
|
|
137
|
-
* }
|
|
138
|
-
* }
|
|
139
|
-
* };
|
|
140
|
-
*
|
|
141
|
-
* normalizeRequired(schema);
|
|
142
|
-
* // Result:
|
|
143
|
-
* // schema.required = ['name', 'address']
|
|
144
|
-
* // All nested required fields are normalized recursively
|
|
145
|
-
* ```
|
|
146
|
-
*
|
|
147
|
-
* **Limitations:**
|
|
148
|
-
* - Only processes schemas with a `properties` field (ObjectSchema or ObjectPropertySchema)
|
|
149
|
-
* - EnumSchema is accepted but returns early without processing
|
|
150
|
-
* - Reference properties (`$ref`) are skipped and not processed
|
|
151
|
-
* - The function mutates the input schema object
|
|
152
|
-
* - Does not resolve `$ref` references (they must be resolved separately)
|
|
153
|
-
*
|
|
154
|
-
* @param jsonSchema - The JSON schema to normalize (ObjectSchema, ObjectPropertySchema, or ReferencePropertySchema)
|
|
155
|
-
* @returns void (mutates the input schema in-place)
|
|
156
4
|
*/
|
|
157
|
-
declare const normalizeRequired: (jsonSchema:
|
|
5
|
+
declare const normalizeRequired: (jsonSchema: EnumSchema | ObjectSchema | ObjectPropertySchema | ReferencePropertySchema) => void;
|
|
158
6
|
export default normalizeRequired;
|
|
159
7
|
//# sourceMappingURL=normalizeRequired.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeRequired.d.ts","sourceRoot":"","sources":["../../src/helpers/normalizeRequired.ts"],"names":[],"mappings":"AAEA
|
|
1
|
+
{"version":3,"file":"normalizeRequired.d.ts","sourceRoot":"","sources":["../../src/helpers/normalizeRequired.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,QAAA,MAAM,iBAAiB,GAAI,YAAY,UAAU,GAAG,YAAY,GAAG,oBAAoB,GAAG,uBAAuB,SAgEhH,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
|
@@ -4,157 +4,6 @@ const lodash_1 = require("lodash");
|
|
|
4
4
|
/**
|
|
5
5
|
* Normalizes required field declarations in JSON schemas by converting property-level
|
|
6
6
|
* `required` flags to schema-level `required` arrays and `x-required` metadata flags.
|
|
7
|
-
*
|
|
8
|
-
* **Intent:**
|
|
9
|
-
* This function transforms JSON schemas from a property-centric required field model
|
|
10
|
-
* (where each property has its own `required: true/false` flag) to the standard JSON Schema
|
|
11
|
-
* format (where required fields are listed in a top-level `required` array). This normalization
|
|
12
|
-
* ensures compatibility with JSON Schema validators while preserving the original required
|
|
13
|
-
* field information through the `x-required` extension attribute.
|
|
14
|
-
*
|
|
15
|
-
* **Use Cases:**
|
|
16
|
-
* 1. **Schema Standardization**: Convert custom schema formats to standard JSON Schema format
|
|
17
|
-
* for validator compatibility
|
|
18
|
-
* 2. **Schema Transformation**: Prepare schemas for validation libraries that expect
|
|
19
|
-
* required fields in array format
|
|
20
|
-
* 3. **Metadata Preservation**: Maintain required field information in both standard format
|
|
21
|
-
* (`required` array) and extension format (`x-required` flag) for different use cases
|
|
22
|
-
* 4. **Schema Processing Pipeline**: Normalize schemas before validation, credential generation,
|
|
23
|
-
* or API documentation generation
|
|
24
|
-
*
|
|
25
|
-
* **Behavior:**
|
|
26
|
-
* - Mutates the input schema in-place
|
|
27
|
-
* - Moves `required: true` from property level to schema-level `required` array
|
|
28
|
-
* - Sets `x-required: true` on properties that were marked as required
|
|
29
|
-
* - Deletes the `required` property from individual property schemas
|
|
30
|
-
* - Recursively processes nested object properties
|
|
31
|
-
* - Recursively processes array items (including nested objects within arrays)
|
|
32
|
-
* - Skips reference properties (`$ref`) as they are resolved elsewhere
|
|
33
|
-
* - Skips EnumSchema (returns early)
|
|
34
|
-
* - Only sets `required` array if at least one field is required
|
|
35
|
-
*
|
|
36
|
-
* **Examples:**
|
|
37
|
-
*
|
|
38
|
-
* ```typescript
|
|
39
|
-
* // Example 1: Simple object schema
|
|
40
|
-
* const schema = {
|
|
41
|
-
* id: 'User',
|
|
42
|
-
* properties: {
|
|
43
|
-
* name: { type: 'string', required: true },
|
|
44
|
-
* email: { type: 'string', required: true },
|
|
45
|
-
* age: { type: 'number', required: false }
|
|
46
|
-
* }
|
|
47
|
-
* };
|
|
48
|
-
*
|
|
49
|
-
* normalizeRequired(schema);
|
|
50
|
-
* // Result:
|
|
51
|
-
* // schema.required = ['name', 'email']
|
|
52
|
-
* // schema.properties.name['x-required'] = true
|
|
53
|
-
* // schema.properties.email['x-required'] = true
|
|
54
|
-
* // schema.properties.name.required = undefined (deleted)
|
|
55
|
-
* // schema.properties.email.required = undefined (deleted)
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
|
-
* ```typescript
|
|
59
|
-
* // Example 2: Nested objects
|
|
60
|
-
* const schema = {
|
|
61
|
-
* id: 'Order',
|
|
62
|
-
* properties: {
|
|
63
|
-
* user: {
|
|
64
|
-
* type: 'object',
|
|
65
|
-
* required: true,
|
|
66
|
-
* properties: {
|
|
67
|
-
* name: { type: 'string', required: true },
|
|
68
|
-
* address: {
|
|
69
|
-
* type: 'object',
|
|
70
|
-
* properties: {
|
|
71
|
-
* street: { type: 'string', required: true }
|
|
72
|
-
* }
|
|
73
|
-
* }
|
|
74
|
-
* }
|
|
75
|
-
* }
|
|
76
|
-
* }
|
|
77
|
-
* };
|
|
78
|
-
*
|
|
79
|
-
* normalizeRequired(schema);
|
|
80
|
-
* // Result:
|
|
81
|
-
* // schema.required = ['user']
|
|
82
|
-
* // schema.properties.user['x-required'] = true
|
|
83
|
-
* // schema.properties.user.required = ['name']
|
|
84
|
-
* // schema.properties.user.properties.name['x-required'] = true
|
|
85
|
-
* // schema.properties.user.properties.address.required = ['street']
|
|
86
|
-
* // schema.properties.user.properties.address.properties.street['x-required'] = true
|
|
87
|
-
* ```
|
|
88
|
-
*
|
|
89
|
-
* ```typescript
|
|
90
|
-
* // Example 3: Arrays with object items
|
|
91
|
-
* const schema = {
|
|
92
|
-
* id: 'Order',
|
|
93
|
-
* properties: {
|
|
94
|
-
* items: {
|
|
95
|
-
* type: 'array',
|
|
96
|
-
* required: true,
|
|
97
|
-
* items: {
|
|
98
|
-
* type: 'object',
|
|
99
|
-
* properties: {
|
|
100
|
-
* productId: { type: 'string', required: true },
|
|
101
|
-
* quantity: { type: 'number', required: true }
|
|
102
|
-
* }
|
|
103
|
-
* }
|
|
104
|
-
* }
|
|
105
|
-
* }
|
|
106
|
-
* };
|
|
107
|
-
*
|
|
108
|
-
* normalizeRequired(schema);
|
|
109
|
-
* // Result:
|
|
110
|
-
* // schema.required = ['items']
|
|
111
|
-
* // schema.properties.items['x-required'] = true
|
|
112
|
-
* // schema.properties.items.items.required = ['productId', 'quantity']
|
|
113
|
-
* // schema.properties.items.items.properties.productId['x-required'] = true
|
|
114
|
-
* // schema.properties.items.items.properties.quantity['x-required'] = true
|
|
115
|
-
* ```
|
|
116
|
-
*
|
|
117
|
-
* ```typescript
|
|
118
|
-
* // Example 4: Mixed structure
|
|
119
|
-
* const schema = {
|
|
120
|
-
* id: 'Profile',
|
|
121
|
-
* properties: {
|
|
122
|
-
* name: { type: 'string', required: true },
|
|
123
|
-
* address: {
|
|
124
|
-
* type: 'object',
|
|
125
|
-
* required: true,
|
|
126
|
-
* properties: {
|
|
127
|
-
* street: { type: 'string', required: true },
|
|
128
|
-
* city: { type: 'string' }
|
|
129
|
-
* }
|
|
130
|
-
* },
|
|
131
|
-
* tags: {
|
|
132
|
-
* type: 'array',
|
|
133
|
-
* items: {
|
|
134
|
-
* type: 'object',
|
|
135
|
-
* properties: {
|
|
136
|
-
* label: { type: 'string', required: true }
|
|
137
|
-
* }
|
|
138
|
-
* }
|
|
139
|
-
* }
|
|
140
|
-
* }
|
|
141
|
-
* };
|
|
142
|
-
*
|
|
143
|
-
* normalizeRequired(schema);
|
|
144
|
-
* // Result:
|
|
145
|
-
* // schema.required = ['name', 'address']
|
|
146
|
-
* // All nested required fields are normalized recursively
|
|
147
|
-
* ```
|
|
148
|
-
*
|
|
149
|
-
* **Limitations:**
|
|
150
|
-
* - Only processes schemas with a `properties` field (ObjectSchema or ObjectPropertySchema)
|
|
151
|
-
* - EnumSchema is accepted but returns early without processing
|
|
152
|
-
* - Reference properties (`$ref`) are skipped and not processed
|
|
153
|
-
* - The function mutates the input schema object
|
|
154
|
-
* - Does not resolve `$ref` references (they must be resolved separately)
|
|
155
|
-
*
|
|
156
|
-
* @param jsonSchema - The JSON schema to normalize (ObjectSchema, ObjectPropertySchema, or ReferencePropertySchema)
|
|
157
|
-
* @returns void (mutates the input schema in-place)
|
|
158
7
|
*/
|
|
159
8
|
const normalizeRequired = (jsonSchema) => {
|
|
160
9
|
const { enum: enumItems } = jsonSchema;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeRequired.js","sourceRoot":"","sources":["../../src/helpers/normalizeRequired.ts"],"names":[],"mappings":";;AAAA,mCAAqC;
|
|
1
|
+
{"version":3,"file":"normalizeRequired.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KAAK,OAAO,CAAC;QAEjC,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,EAAE,KAAK,EAAE,GAAI,QAAgC,CAAC;YAEpD,IAAI,KAAK,EAAE,CAAC;gBACV,iBAAiB,CAAC,KAAuD,CAAC,CAAC;YAC7E,CAAC;YAED,SAAS;QACX,CAAC;IACH,CAAC;IAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,YAAY,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACnC,CAAC;AACH,CAAC,CAAC;AAEF,kBAAe,iBAAiB,CAAC"}
|
|
@@ -1,81 +1,5 @@
|
|
|
1
1
|
type ValueType = 'number' | 'integer' | 'boolean' | 'string' | 'object' | 'array';
|
|
2
|
-
/**
|
|
3
|
-
* Normalizes a value to match a specified JSON Schema type.
|
|
4
|
-
*
|
|
5
|
-
* ## Intent
|
|
6
|
-
*
|
|
7
|
-
* This function is designed to coerce values into their expected types based on JSON Schema
|
|
8
|
-
* type definitions. It's particularly useful when processing data from external sources (like
|
|
9
|
-
* form inputs, query parameters, or API responses) where values may arrive as strings but
|
|
10
|
-
* need to be converted to their proper types according to a schema definition.
|
|
11
|
-
*
|
|
12
|
-
* The function performs type coercion where appropriate, but preserves the original value
|
|
13
|
-
* when conversion is not possible or when the value is already of the correct type. This
|
|
14
|
-
* makes it safe to use in data normalization pipelines without losing information.
|
|
15
|
-
*
|
|
16
|
-
* ## Use Cases
|
|
17
|
-
*
|
|
18
|
-
* 1. **Schema-based data normalization**: When processing objects against JSON Schema
|
|
19
|
-
* definitions, ensuring property values match their declared types.
|
|
20
|
-
*
|
|
21
|
-
* 2. **Form data processing**: Converting string values from HTML forms (which are always
|
|
22
|
-
* strings) to their expected types (numbers, booleans) based on schema definitions.
|
|
23
|
-
*
|
|
24
|
-
* 3. **API response normalization**: Normalizing API responses where types may be ambiguous
|
|
25
|
-
* or incorrectly serialized (e.g., numbers as strings, booleans as strings).
|
|
26
|
-
*
|
|
27
|
-
* 4. **Configuration parsing**: Parsing configuration values from environment variables or
|
|
28
|
-
* config files where everything is initially a string but needs type coercion.
|
|
29
|
-
*
|
|
30
|
-
* ## Behavior by Type
|
|
31
|
-
*
|
|
32
|
-
* - **number/integer**: Attempts to convert strings and booleans to numbers. Preserves
|
|
33
|
-
* original value if conversion fails or value is already a number.
|
|
34
|
-
*
|
|
35
|
-
* - **boolean**: Converts numbers (0 → false, non-zero → true) and recognized string
|
|
36
|
-
* values ('yes', 'true', '1' → true; 'no', 'false', '0' → false). Preserves original
|
|
37
|
-
* value for unrecognized strings or non-convertible types.
|
|
38
|
-
*
|
|
39
|
-
* - **string/object/array**: Returns the value as-is (no conversion performed).
|
|
40
|
-
*
|
|
41
|
-
* - **null/undefined**: Always preserved regardless of target type.
|
|
42
|
-
*
|
|
43
|
-
* ## Examples
|
|
44
|
-
*
|
|
45
|
-
* ### Number Conversion
|
|
46
|
-
* ```typescript
|
|
47
|
-
* normalizeType('number', '123') // → 123
|
|
48
|
-
* normalizeType('number', '45.67') // → 45.67
|
|
49
|
-
* normalizeType('number', '0') // → 0
|
|
50
|
-
* normalizeType('number', true) // → 1
|
|
51
|
-
* normalizeType('number', 'abc') // → 'abc' (conversion failed, original preserved)
|
|
52
|
-
* ```
|
|
53
|
-
*
|
|
54
|
-
* ### Boolean Conversion
|
|
55
|
-
* ```typescript
|
|
56
|
-
* normalizeType('boolean', 0) // → false
|
|
57
|
-
* normalizeType('boolean', 1) // → true
|
|
58
|
-
* normalizeType('boolean', 'yes') // → true
|
|
59
|
-
* normalizeType('boolean', 'true') // → true
|
|
60
|
-
* normalizeType('boolean', '1') // → true
|
|
61
|
-
* normalizeType('boolean', 'no') // → false
|
|
62
|
-
* normalizeType('boolean', 'false') // → false
|
|
63
|
-
* normalizeType('boolean', 'maybe') // → 'maybe' (unrecognized, original preserved)
|
|
64
|
-
* ```
|
|
65
|
-
*
|
|
66
|
-
* ### Type Preservation
|
|
67
|
-
* ```typescript
|
|
68
|
-
* normalizeType('string', 'hello') // → 'hello'
|
|
69
|
-
* normalizeType('string', 123) // → 123 (no conversion for string type)
|
|
70
|
-
* normalizeType('object', { a: 1 }) // → { a: 1 }
|
|
71
|
-
* normalizeType('array', [1, 2, 3]) // → [1, 2, 3]
|
|
72
|
-
* normalizeType('number', null) // → null (null always preserved)
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* @param type - The target JSON Schema type ('number', 'integer', 'boolean', 'string', 'object', 'array')
|
|
76
|
-
* @param value - The value to normalize (can be any type)
|
|
77
|
-
* @returns The normalized value, or the original value if normalization is not applicable
|
|
78
|
-
*/
|
|
2
|
+
/** Normalizes a value to match a specified JSON Schema type. */
|
|
79
3
|
declare const normalizeType: (type: ValueType, value: unknown) => string | number | boolean | unknown;
|
|
80
4
|
export default normalizeType;
|
|
81
5
|
//# sourceMappingURL=normalizeType.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeType.d.ts","sourceRoot":"","sources":["../../src/helpers/normalizeType.ts"],"names":[],"mappings":"AAAA,KAAK,SAAS,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,GAAG,QAAQ,GAAG,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"normalizeType.d.ts","sourceRoot":"","sources":["../../src/helpers/normalizeType.ts"],"names":[],"mappings":"AAAA,KAAK,SAAS,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,GAAG,QAAQ,GAAG,OAAO,CAAC;AAuClF,gEAAgE;AAChE,QAAA,MAAM,aAAa,GAAI,MAAM,SAAS,EAAE,OAAO,OAAO,KAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,OAgEpF,CAAC;AAEF,eAAe,aAAa,CAAC"}
|