@kravc/schema 2.8.0-alpha.7 → 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 +1 -57
- 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 +1 -57
- 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
package/dist/Schema.js
CHANGED
|
@@ -10,93 +10,13 @@ const normalizeProperties_1 = __importDefault(require("./helpers/normalizeProper
|
|
|
10
10
|
const getLinkedDataContext_1 = __importDefault(require("./ld/getLinkedDataContext"));
|
|
11
11
|
const removeRequiredAndDefault_1 = __importDefault(require("./helpers/removeRequiredAndDefault"));
|
|
12
12
|
const UNDEFINED_SCHEMA_ID = 'UNDEFINED_SCHEMA_ID';
|
|
13
|
-
/**
|
|
14
|
-
* Schema class for defining and manipulating JSON schemas for object validation.
|
|
15
|
-
*
|
|
16
|
-
* This class provides a flexible API for creating, transforming, and composing JSON schemas.
|
|
17
|
-
* It supports both property-based schemas (for objects) and enum schemas (for value sets).
|
|
18
|
-
*
|
|
19
|
-
* **Use Cases:**
|
|
20
|
-
* - Define validation schemas for API request/response objects
|
|
21
|
-
* - Create reusable schema components that can be extended or composed
|
|
22
|
-
* - Generate JSON-LD linked data types for semantic web applications
|
|
23
|
-
* - Transform schemas for different use cases (e.g., create update schemas from create schemas)
|
|
24
|
-
*
|
|
25
|
-
* **Example - Basic Usage:**
|
|
26
|
-
* ```typescript
|
|
27
|
-
* const userSchema = new Schema({
|
|
28
|
-
* firstName: { type: 'string', required: true },
|
|
29
|
-
* lastName: { type: 'string', required: true },
|
|
30
|
-
* email: { type: 'string', format: 'email', required: true }
|
|
31
|
-
* }, 'User');
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* **Example - Schema Composition:**
|
|
35
|
-
* ```typescript
|
|
36
|
-
* const baseSchema = new Schema({ name: { required: true } }, 'Base');
|
|
37
|
-
* const extendedSchema = baseSchema.extend({ status: { enum: ['Active', 'Inactive'] } }, 'Extended');
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* **Example - Linked Data Types:**
|
|
41
|
-
* ```typescript
|
|
42
|
-
* const schema = new Schema(
|
|
43
|
-
* { id: { type: 'string' }, name: { type: 'string' } },
|
|
44
|
-
* 'Person',
|
|
45
|
-
* 'https://schema.org/'
|
|
46
|
-
* );
|
|
47
|
-
* // schema.linkedDataType will contain JSON-LD context
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
13
|
+
/** Schema class for defining and manipulating JSON schemas for object validation. */
|
|
50
14
|
class Schema {
|
|
51
15
|
_id;
|
|
52
16
|
_url;
|
|
53
17
|
_source;
|
|
54
18
|
_linkedDataType;
|
|
55
|
-
/**
|
|
56
|
-
* Creates a new Schema instance.
|
|
57
|
-
*
|
|
58
|
-
* **Intent:** Initialize a schema with properties, enum values, or clone from another schema.
|
|
59
|
-
* Automatically normalizes properties, infers types, and optionally creates JSON-LD linked data types.
|
|
60
|
-
*
|
|
61
|
-
* **Use Cases:**
|
|
62
|
-
* - Create a new schema from scratch with property definitions
|
|
63
|
-
* - Create an enum schema for value validation
|
|
64
|
-
* - Clone an existing schema with a new ID
|
|
65
|
-
* - Create a schema with JSON-LD context for semantic web applications
|
|
66
|
-
*
|
|
67
|
-
* @param propertiesOrSchema - Property definitions, enum schema, or existing Schema instance to clone
|
|
68
|
-
* @param id - Unique identifier for the schema (defaults to 'UNDEFINED_SCHEMA_ID' if not provided)
|
|
69
|
-
* @param url - Optional URL for generating JSON-LD linked data types (only for property schemas, not enums)
|
|
70
|
-
*
|
|
71
|
-
* **Example - Property Schema:**
|
|
72
|
-
* ```typescript
|
|
73
|
-
* const schema = new Schema({
|
|
74
|
-
* name: { type: 'string', required: true },
|
|
75
|
-
* age: { type: 'number', minimum: 0 }
|
|
76
|
-
* }, 'Person');
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
* **Example - Enum Schema:**
|
|
80
|
-
* ```typescript
|
|
81
|
-
* const sizeSchema = new Schema({ enum: ['S', 'M', 'L'], type: 'string' }, 'Size');
|
|
82
|
-
* ```
|
|
83
|
-
*
|
|
84
|
-
* **Example - Clone Schema:**
|
|
85
|
-
* ```typescript
|
|
86
|
-
* const original = new Schema({ name: { type: 'string' } }, 'Original');
|
|
87
|
-
* const cloned = new Schema(original, 'Cloned');
|
|
88
|
-
* ```
|
|
89
|
-
*
|
|
90
|
-
* **Example - With Linked Data URL:**
|
|
91
|
-
* ```typescript
|
|
92
|
-
* const schema = new Schema(
|
|
93
|
-
* { name: { type: 'string' } },
|
|
94
|
-
* 'Person',
|
|
95
|
-
* 'https://schema.org/'
|
|
96
|
-
* );
|
|
97
|
-
* // Automatically creates linkedDataType with @id and @context
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
19
|
+
/** Creates a new Schema instance. */
|
|
100
20
|
constructor(propertiesOrSchema, id, url) {
|
|
101
21
|
this._id = id || UNDEFINED_SCHEMA_ID;
|
|
102
22
|
this._url = url;
|
|
@@ -130,132 +50,23 @@ class Schema {
|
|
|
130
50
|
}
|
|
131
51
|
(0, normalizeProperties_1.default)(this._source);
|
|
132
52
|
}
|
|
133
|
-
/**
|
|
134
|
-
* Returns the unique identifier for this schema.
|
|
135
|
-
*
|
|
136
|
-
* **Intent:** Provide a way to reference and identify schemas in collections or validators.
|
|
137
|
-
*
|
|
138
|
-
* **Use Cases:**
|
|
139
|
-
* - Reference schemas in Validator instances
|
|
140
|
-
* - Track schema lineage when cloning/extending
|
|
141
|
-
* - Generate schema identifiers for JSON Schema output
|
|
142
|
-
*
|
|
143
|
-
* @returns The schema ID, or 'UNDEFINED_SCHEMA_ID' if not specified during construction
|
|
144
|
-
*
|
|
145
|
-
* **Example:**
|
|
146
|
-
* ```typescript
|
|
147
|
-
* const schema = new Schema({ name: { type: 'string' } }, 'User');
|
|
148
|
-
* console.log(schema.id); // 'User'
|
|
149
|
-
* ```
|
|
150
|
-
*/
|
|
53
|
+
/** Returns the unique identifier for this schema. */
|
|
151
54
|
get id() {
|
|
152
55
|
return this._id;
|
|
153
56
|
}
|
|
154
|
-
/**
|
|
155
|
-
* Returns the URL associated with this schema for JSON-LD linked data generation.
|
|
156
|
-
*
|
|
157
|
-
* **Intent:** Provide access to the base URL used for generating semantic web identifiers.
|
|
158
|
-
*
|
|
159
|
-
* **Use Cases:**
|
|
160
|
-
* - Check if a schema has linked data support
|
|
161
|
-
* - Access the base URL for constructing related URIs
|
|
162
|
-
* - Debug linked data type generation
|
|
163
|
-
*
|
|
164
|
-
* @returns The schema URL if provided during construction, undefined otherwise
|
|
165
|
-
*
|
|
166
|
-
* **Example:**
|
|
167
|
-
* ```typescript
|
|
168
|
-
* const schema = new Schema({ name: { type: 'string' } }, 'Person', 'https://schema.org/');
|
|
169
|
-
* console.log(schema.url); // 'https://schema.org/'
|
|
170
|
-
* ```
|
|
171
|
-
*/
|
|
57
|
+
/** Returns the URL associated with this schema for JSON-LD linked data generation. */
|
|
172
58
|
get url() {
|
|
173
59
|
return this._url;
|
|
174
60
|
}
|
|
175
|
-
/**
|
|
176
|
-
* Returns a deep clone of the schema's source properties or enum definition.
|
|
177
|
-
*
|
|
178
|
-
* **Intent:** Provide immutable access to schema definitions to prevent accidental mutations.
|
|
179
|
-
*
|
|
180
|
-
* **Use Cases:**
|
|
181
|
-
* - Inspect schema structure without modifying the original
|
|
182
|
-
* - Clone schemas manually for custom transformations
|
|
183
|
-
* - Debug schema definitions
|
|
184
|
-
* - Pass schema definitions to other functions safely
|
|
185
|
-
*
|
|
186
|
-
* @returns A deep copy of the schema source (PropertiesSchema or EnumSchema)
|
|
187
|
-
*
|
|
188
|
-
* **Example:**
|
|
189
|
-
* ```typescript
|
|
190
|
-
* const schema = new Schema({ name: { type: 'string', required: true } }, 'User');
|
|
191
|
-
* const source = schema.source;
|
|
192
|
-
* // source is a clone, modifying it won't affect the original schema
|
|
193
|
-
* ```
|
|
194
|
-
*/
|
|
61
|
+
/** Returns a deep clone of the schema's source properties or enum definition. */
|
|
195
62
|
get source() {
|
|
196
63
|
return (0, lodash_1.cloneDeep)(this._source);
|
|
197
64
|
}
|
|
198
|
-
/**
|
|
199
|
-
* Checks if this schema is an enum schema (defines a set of allowed values).
|
|
200
|
-
*
|
|
201
|
-
* **Intent:** Enable type-safe branching logic based on schema type.
|
|
202
|
-
*
|
|
203
|
-
* **Use Cases:**
|
|
204
|
-
* - Conditionally apply methods that only work on property schemas
|
|
205
|
-
* - Validate schema type before operations
|
|
206
|
-
* - Implement different serialization logic for enums vs objects
|
|
207
|
-
*
|
|
208
|
-
* @returns true if the schema is an enum schema, false if it's a properties schema
|
|
209
|
-
*
|
|
210
|
-
* **Example:**
|
|
211
|
-
* ```typescript
|
|
212
|
-
* const enumSchema = new Schema({ enum: ['A', 'B', 'C'] }, 'Letters');
|
|
213
|
-
* const propSchema = new Schema({ name: { type: 'string' } }, 'Person');
|
|
214
|
-
*
|
|
215
|
-
* console.log(enumSchema.isEnum); // true
|
|
216
|
-
* console.log(propSchema.isEnum); // false
|
|
217
|
-
* ```
|
|
218
|
-
*/
|
|
65
|
+
/** Checks if this schema is an enum schema (defines a set of allowed values). */
|
|
219
66
|
get isEnum() {
|
|
220
67
|
return !!this._source.enum;
|
|
221
68
|
}
|
|
222
|
-
/**
|
|
223
|
-
* Returns a normalized JSON Schema representation of this schema.
|
|
224
|
-
*
|
|
225
|
-
* **Intent:** Convert the internal schema representation to standard JSON Schema format
|
|
226
|
-
* with normalized required arrays and proper structure for validation libraries.
|
|
227
|
-
*
|
|
228
|
-
* **Use Cases:**
|
|
229
|
-
* - Export schemas for use with JSON Schema validators (e.g., z-schema)
|
|
230
|
-
* - Generate API documentation from schemas
|
|
231
|
-
* - Serialize schemas to JSON for storage or transmission
|
|
232
|
-
* - Integrate with tools that expect standard JSON Schema format
|
|
233
|
-
*
|
|
234
|
-
* @returns A JsonSchema object (ObjectSchema for property schemas, EnumSchema for enum schemas)
|
|
235
|
-
*
|
|
236
|
-
* **Example - Property Schema:**
|
|
237
|
-
* ```typescript
|
|
238
|
-
* const schema = new Schema({
|
|
239
|
-
* name: { type: 'string', required: true },
|
|
240
|
-
* age: { type: 'number' }
|
|
241
|
-
* }, 'Person');
|
|
242
|
-
*
|
|
243
|
-
* const jsonSchema = schema.jsonSchema;
|
|
244
|
-
* // {
|
|
245
|
-
* // id: 'Person',
|
|
246
|
-
* // type: 'object',
|
|
247
|
-
* // properties: { name: {...}, age: {...} },
|
|
248
|
-
* // required: ['name']
|
|
249
|
-
* // }
|
|
250
|
-
* ```
|
|
251
|
-
*
|
|
252
|
-
* **Example - Enum Schema:**
|
|
253
|
-
* ```typescript
|
|
254
|
-
* const enumSchema = new Schema({ enum: ['S', 'M', 'L'], type: 'string' }, 'Size');
|
|
255
|
-
* const jsonSchema = enumSchema.jsonSchema;
|
|
256
|
-
* // { id: 'Size', enum: ['S', 'M', 'L'], type: 'string' }
|
|
257
|
-
* ```
|
|
258
|
-
*/
|
|
69
|
+
/** Returns a normalized JSON Schema representation of this schema. */
|
|
259
70
|
get jsonSchema() {
|
|
260
71
|
if (this.isEnum) {
|
|
261
72
|
return {
|
|
@@ -271,93 +82,15 @@ class Schema {
|
|
|
271
82
|
(0, normalizeRequired_1.default)(jsonSchema);
|
|
272
83
|
return jsonSchema;
|
|
273
84
|
}
|
|
274
|
-
/**
|
|
275
|
-
* Returns the JSON-LD linked data type if a URL was provided during construction.
|
|
276
|
-
*
|
|
277
|
-
* **Intent:** Provide semantic web identifiers and context for schemas, enabling
|
|
278
|
-
* integration with Linked Data and Verifiable Credentials standards.
|
|
279
|
-
*
|
|
280
|
-
* **Use Cases:**
|
|
281
|
-
* - Generate Verifiable Credentials with proper semantic context
|
|
282
|
-
* - Create JSON-LD documents with schema.org or custom vocabularies
|
|
283
|
-
* - Enable semantic interoperability between systems
|
|
284
|
-
* - Support decentralized identity and credential systems
|
|
285
|
-
*
|
|
286
|
-
* @returns LinkedDataType object with @id and @context, or undefined if no URL was provided
|
|
287
|
-
*
|
|
288
|
-
* **Example:**
|
|
289
|
-
* ```typescript
|
|
290
|
-
* const schema = new Schema(
|
|
291
|
-
* { name: { type: 'string' }, email: { type: 'string', format: 'email' } },
|
|
292
|
-
* 'Person',
|
|
293
|
-
* 'https://schema.org/'
|
|
294
|
-
* );
|
|
295
|
-
*
|
|
296
|
-
* const linkedData = schema.linkedDataType;
|
|
297
|
-
* // {
|
|
298
|
-
* // '@id': 'https://schema.org/Person',
|
|
299
|
-
* // '@context': { ... } // Generated context mapping types to schema.org
|
|
300
|
-
* // }
|
|
301
|
-
* ```
|
|
302
|
-
*
|
|
303
|
-
* **Note:** Linked data types are only generated for property schemas, not enum schemas.
|
|
304
|
-
*/
|
|
85
|
+
/** Returns the JSON-LD linked data type if a URL was provided during construction. */
|
|
305
86
|
get linkedDataType() {
|
|
306
87
|
return this._linkedDataType;
|
|
307
88
|
}
|
|
308
|
-
/**
|
|
309
|
-
* Creates a complete clone of the schema with a new identifier.
|
|
310
|
-
*
|
|
311
|
-
* **Intent:** Duplicate a schema while maintaining all properties and structure,
|
|
312
|
-
* useful for creating variations or versioning schemas.
|
|
313
|
-
*
|
|
314
|
-
* **Use Cases:**
|
|
315
|
-
* - Create schema versions (e.g., 'UserV1', 'UserV2')
|
|
316
|
-
* - Duplicate schemas for different contexts
|
|
317
|
-
* - Create base schemas that can be independently extended
|
|
318
|
-
* - Maintain schema history or branching
|
|
319
|
-
*
|
|
320
|
-
* @param id - The new unique identifier for the cloned schema
|
|
321
|
-
* @returns A new Schema instance with identical structure but different ID
|
|
322
|
-
*
|
|
323
|
-
* **Example:**
|
|
324
|
-
* ```typescript
|
|
325
|
-
* const userSchema = new Schema({ name: { type: 'string' } }, 'User');
|
|
326
|
-
* const userV2Schema = userSchema.clone('UserV2');
|
|
327
|
-
* // Both schemas have identical properties but different IDs
|
|
328
|
-
* ```
|
|
329
|
-
*/
|
|
89
|
+
/** Creates a complete clone of the schema with a new identifier. */
|
|
330
90
|
clone(id) {
|
|
331
91
|
return new Schema(this.source, id);
|
|
332
92
|
}
|
|
333
|
-
/**
|
|
334
|
-
* Creates a schema clone without required constraints and default values.
|
|
335
|
-
*
|
|
336
|
-
* **Intent:** Generate update/patch schemas from create schemas by removing
|
|
337
|
-
* validation constraints that make sense for creation but not for updates.
|
|
338
|
-
*
|
|
339
|
-
* **Use Cases:**
|
|
340
|
-
* - Create update schemas where all fields are optional
|
|
341
|
-
* - Generate PATCH endpoint schemas from POST endpoint schemas
|
|
342
|
-
* - Allow partial updates without requiring all original fields
|
|
343
|
-
* - Create flexible input schemas for editing operations
|
|
344
|
-
*
|
|
345
|
-
* @param id - The new unique identifier for the pure schema
|
|
346
|
-
* @returns A new Schema instance with all required flags and defaults removed
|
|
347
|
-
* @throws Error if called on an enum schema
|
|
348
|
-
*
|
|
349
|
-
* **Example:**
|
|
350
|
-
* ```typescript
|
|
351
|
-
* const createSchema = new Schema({
|
|
352
|
-
* name: { type: 'string', required: true },
|
|
353
|
-
* email: { type: 'string', required: true, default: 'user@example.com' }
|
|
354
|
-
* }, 'CreateUser');
|
|
355
|
-
*
|
|
356
|
-
* const updateSchema = createSchema.pure('UpdateUser');
|
|
357
|
-
* // updateSchema has no required fields and no defaults
|
|
358
|
-
* // All fields are optional for updates
|
|
359
|
-
* ```
|
|
360
|
-
*/
|
|
93
|
+
/** Creates a schema clone without required constraints and default values. */
|
|
361
94
|
pure(id) {
|
|
362
95
|
if (this.isEnum) {
|
|
363
96
|
throw new Error('The "pure" method is not supported for enum schemas.');
|
|
@@ -368,37 +101,7 @@ class Schema {
|
|
|
368
101
|
});
|
|
369
102
|
return new Schema(source, id);
|
|
370
103
|
}
|
|
371
|
-
/**
|
|
372
|
-
* Creates a schema clone containing only the specified properties.
|
|
373
|
-
*
|
|
374
|
-
* **Intent:** Create focused schemas that expose only a subset of properties,
|
|
375
|
-
* useful for different API views or security contexts.
|
|
376
|
-
*
|
|
377
|
-
* **Use Cases:**
|
|
378
|
-
* - Create public-facing schemas that hide sensitive fields
|
|
379
|
-
* - Generate response schemas with only necessary fields
|
|
380
|
-
* - Create view-specific schemas (e.g., 'UserSummary', 'UserDetail')
|
|
381
|
-
* - Implement field-level access control in schemas
|
|
382
|
-
* - Create lightweight schemas for list views
|
|
383
|
-
*
|
|
384
|
-
* @param propertyNames - Array of property names to include in the new schema
|
|
385
|
-
* @param id - The new unique identifier for the filtered schema
|
|
386
|
-
* @returns A new Schema instance containing only the specified properties
|
|
387
|
-
* @throws Error if called on an enum schema
|
|
388
|
-
*
|
|
389
|
-
* **Example:**
|
|
390
|
-
* ```typescript
|
|
391
|
-
* const fullSchema = new Schema({
|
|
392
|
-
* name: { type: 'string' },
|
|
393
|
-
* email: { type: 'string' },
|
|
394
|
-
* password: { type: 'string' },
|
|
395
|
-
* ssn: { type: 'string' }
|
|
396
|
-
* }, 'User');
|
|
397
|
-
*
|
|
398
|
-
* const publicSchema = fullSchema.only(['name', 'email'], 'PublicUser');
|
|
399
|
-
* // publicSchema only contains name and email, hiding sensitive fields
|
|
400
|
-
* ```
|
|
401
|
-
*/
|
|
104
|
+
/** Creates a schema clone containing only the specified properties. */
|
|
402
105
|
only(propertyNames, id) {
|
|
403
106
|
if (this.isEnum) {
|
|
404
107
|
throw new Error('The "only" method is not supported for enum schemas.');
|
|
@@ -406,88 +109,14 @@ class Schema {
|
|
|
406
109
|
const source = (0, lodash_1.pick)(this.source, propertyNames);
|
|
407
110
|
return new Schema(source, id);
|
|
408
111
|
}
|
|
409
|
-
/**
|
|
410
|
-
* Creates a schema clone extended with additional properties.
|
|
411
|
-
*
|
|
412
|
-
* **Intent:** Build complex schemas incrementally by composing base schemas
|
|
413
|
-
* with additional fields, enabling schema inheritance and composition patterns.
|
|
414
|
-
*
|
|
415
|
-
* **Use Cases:**
|
|
416
|
-
* - Extend base schemas with domain-specific fields
|
|
417
|
-
* - Create schema hierarchies (e.g., BaseEntity -> User -> AdminUser)
|
|
418
|
-
* - Add computed or derived fields to existing schemas
|
|
419
|
-
* - Compose schemas from multiple sources
|
|
420
|
-
* - Build versioned schemas that add new fields
|
|
421
|
-
*
|
|
422
|
-
* @param properties - Additional properties to merge into the schema
|
|
423
|
-
* @param id - The new unique identifier for the extended schema
|
|
424
|
-
* @returns A new Schema instance with merged properties (new properties override existing ones)
|
|
425
|
-
* @throws Error if called on an enum schema
|
|
426
|
-
*
|
|
427
|
-
* **Example:**
|
|
428
|
-
* ```typescript
|
|
429
|
-
* const baseSchema = new Schema({
|
|
430
|
-
* id: { type: 'string', required: true },
|
|
431
|
-
* createdAt: { type: 'string', format: 'date-time', required: true }
|
|
432
|
-
* }, 'BaseEntity');
|
|
433
|
-
*
|
|
434
|
-
* const userSchema = baseSchema.extend({
|
|
435
|
-
* name: { type: 'string', required: true },
|
|
436
|
-
* email: { type: 'string', format: 'email', required: true }
|
|
437
|
-
* }, 'User');
|
|
438
|
-
*
|
|
439
|
-
* // userSchema contains id, createdAt, name, and email
|
|
440
|
-
* ```
|
|
441
|
-
*/
|
|
112
|
+
/** Creates a schema clone extended with additional properties. */
|
|
442
113
|
extend(properties, id) {
|
|
443
114
|
if (this.isEnum) {
|
|
444
115
|
throw new Error('The "extend" method is not supported for enum schemas.');
|
|
445
116
|
}
|
|
446
117
|
return new Schema({ ...this.source, ...properties }, id);
|
|
447
118
|
}
|
|
448
|
-
/**
|
|
449
|
-
* Creates a new schema that wraps the current schema as a nested property.
|
|
450
|
-
*
|
|
451
|
-
* **Intent:** Transform a flat schema into a nested structure, useful for
|
|
452
|
-
* API response formatting or when data needs to be wrapped in a container object.
|
|
453
|
-
*
|
|
454
|
-
* **Use Cases:**
|
|
455
|
-
* - Wrap schemas for API responses (e.g., { data: { ...schema } })
|
|
456
|
-
* - Create nested data structures from flat schemas
|
|
457
|
-
* - Format schemas for specific API conventions
|
|
458
|
-
* - Generate wrapper schemas for pagination or metadata containers
|
|
459
|
-
* - Transform schemas for different API versions or formats
|
|
460
|
-
*
|
|
461
|
-
* @param propertyName - The name of the property that will contain the wrapped schema
|
|
462
|
-
* @param attributes - Optional attributes for the wrapper property (default: { required: true })
|
|
463
|
-
* @param id - The new unique identifier for the wrapped schema
|
|
464
|
-
* @returns A new Schema instance where the original schema is nested under the specified property
|
|
465
|
-
* @throws Error if called on an enum schema
|
|
466
|
-
*
|
|
467
|
-
* **Example:**
|
|
468
|
-
* ```typescript
|
|
469
|
-
* const userSchema = new Schema({
|
|
470
|
-
* name: { type: 'string', required: true },
|
|
471
|
-
* email: { type: 'string', required: true }
|
|
472
|
-
* }, 'User');
|
|
473
|
-
*
|
|
474
|
-
* const wrappedSchema = userSchema.wrap('data', { required: true }, 'UserResponse');
|
|
475
|
-
* // Resulting schema structure:
|
|
476
|
-
* // {
|
|
477
|
-
* // data: {
|
|
478
|
-
* // type: 'object',
|
|
479
|
-
* // properties: { name: {...}, email: {...} },
|
|
480
|
-
* // required: true
|
|
481
|
-
* // }
|
|
482
|
-
* // }
|
|
483
|
-
* ```
|
|
484
|
-
*
|
|
485
|
-
* **Example - With Default Value:**
|
|
486
|
-
* ```typescript
|
|
487
|
-
* const wrappedSchema = userSchema.wrap('data', { default: '{}' }, 'OptionalUserResponse');
|
|
488
|
-
* // The 'data' property has a default value and is not required
|
|
489
|
-
* ```
|
|
490
|
-
*/
|
|
119
|
+
/** Creates a new schema that wraps the current schema as a nested property. */
|
|
491
120
|
wrap(propertyName, attributes, id) {
|
|
492
121
|
if (this.isEnum) {
|
|
493
122
|
throw new Error('The "wrap" method is not supported for enum schemas.');
|
package/dist/Schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.js","sourceRoot":"","sources":["../src/Schema.ts"],"names":[],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"Schema.js","sourceRoot":"","sources":["../src/Schema.ts"],"names":[],"mappings":";;;;;AACA,mCAAyC;AAEzC,sEAA8C;AAC9C,oFAA4D;AAC5D,wFAAgE;AAChE,qFAA6D;AAC7D,kGAA0E;AAG1E,MAAM,mBAAmB,GAAG,qBAAqB,CAAC;AAYlD,qFAAqF;AACrF,MAAM,MAAM;IACF,GAAG,CAAS;IACZ,IAAI,CAAU;IACd,OA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@ -1,64 +1,18 @@
|
|
|
1
1
|
import type { SchemaErrorDetail } from 'z-schema';
|
|
2
|
-
import { TargetObject } from './helpers/JsonSchema';
|
|
3
2
|
export type ValidationErrorOutput = {
|
|
4
3
|
path: string;
|
|
5
4
|
code: string;
|
|
6
5
|
params: string[];
|
|
7
6
|
message: string;
|
|
8
7
|
};
|
|
9
|
-
/**
|
|
10
|
-
* Normalized validation error thrown when object validation fails against a schema.
|
|
11
|
-
*
|
|
12
|
-
* This error is thrown by the Validator when an object does not conform to its schema.
|
|
13
|
-
* It extends the standard Error class and provides structured access to validation
|
|
14
|
-
* failure details, including the schema ID, the invalid object, and normalized error
|
|
15
|
-
* information for each validation failure.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* try {
|
|
20
|
-
* validator.validate(userData, 'UserSchema');
|
|
21
|
-
* } catch (error) {
|
|
22
|
-
* if (error instanceof ValidationError) {
|
|
23
|
-
* const errorDetails = error.toJSON();
|
|
24
|
-
* console.error(errorDetails.schemaId); // 'UserSchema'
|
|
25
|
-
* console.error(errorDetails.validationErrors); // Array of validation failures
|
|
26
|
-
* }
|
|
27
|
-
* }
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
8
|
+
/** Normalized validation error thrown when object validation fails against a schema. */
|
|
30
9
|
declare class ValidationError extends Error {
|
|
31
10
|
private _object;
|
|
32
11
|
private _schemaId;
|
|
33
12
|
private _validationErrors;
|
|
34
|
-
/**
|
|
35
|
-
* Creates a validation error instance.
|
|
36
|
-
*
|
|
37
|
-
* @param schemaId - The identifier of the schema that failed validation
|
|
38
|
-
* @param invalidObject - The object that failed validation (may be partially processed)
|
|
39
|
-
* @param validationErrors - Array of validation error details from z-schema, which will be
|
|
40
|
-
* normalized to extract only path, code, params, and message
|
|
41
|
-
*/
|
|
13
|
+
/** Creates a validation error instance. */
|
|
42
14
|
constructor(schemaId: string, invalidObject: TargetObject, validationErrors: SchemaErrorDetail[]);
|
|
43
|
-
/**
|
|
44
|
-
* Returns a JSON serializable representation of the validation error.
|
|
45
|
-
*
|
|
46
|
-
* This method is useful for:
|
|
47
|
-
* - API error responses (can be directly serialized and sent to clients)
|
|
48
|
-
* - Logging validation failures in a structured format
|
|
49
|
-
* - Error reporting and debugging
|
|
50
|
-
*
|
|
51
|
-
* @returns An object containing:
|
|
52
|
-
* - `code`: The error class name ('ValidationError')
|
|
53
|
-
* - `object`: The invalid object that failed validation
|
|
54
|
-
* - `message`: The error message (e.g., '"SchemaId" validation failed')
|
|
55
|
-
* - `schemaId`: The identifier of the schema that failed validation
|
|
56
|
-
* - `validationErrors`: Array of normalized validation errors, each containing:
|
|
57
|
-
* - `path`: JSON path to the invalid field (e.g., '#/user/email')
|
|
58
|
-
* - `code`: Error code from z-schema (e.g., 'INVALID_TYPE', 'REQUIRED')
|
|
59
|
-
* - `params`: Array of error-specific parameters
|
|
60
|
-
* - `message`: Human-readable error message
|
|
61
|
-
*/
|
|
15
|
+
/** Returns a JSON serializable representation of the validation error. */
|
|
62
16
|
toJSON(): {
|
|
63
17
|
code: string;
|
|
64
18
|
object: TargetObject;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ValidationError.d.ts","sourceRoot":"","sources":["../src/ValidationError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAElD,
|
|
1
|
+
{"version":3,"file":"ValidationError.d.ts","sourceRoot":"","sources":["../src/ValidationError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAElD,MAAM,MAAM,qBAAqB,GAAG;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAA;AAED,wFAAwF;AACxF,cAAM,eAAgB,SAAQ,KAAK;IACjC,OAAO,CAAC,OAAO,CAAe;IAC9B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,iBAAiB,CAA0B;IAEnD,2CAA2C;gBAC/B,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE;IAgBhG,0EAA0E;IAC1E,MAAM;;;;;;;CASP;AAED,eAAe,eAAe,CAAC"}
|
package/dist/ValidationError.js
CHANGED
|
@@ -1,38 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/**
|
|
4
|
-
* Normalized validation error thrown when object validation fails against a schema.
|
|
5
|
-
*
|
|
6
|
-
* This error is thrown by the Validator when an object does not conform to its schema.
|
|
7
|
-
* It extends the standard Error class and provides structured access to validation
|
|
8
|
-
* failure details, including the schema ID, the invalid object, and normalized error
|
|
9
|
-
* information for each validation failure.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* try {
|
|
14
|
-
* validator.validate(userData, 'UserSchema');
|
|
15
|
-
* } catch (error) {
|
|
16
|
-
* if (error instanceof ValidationError) {
|
|
17
|
-
* const errorDetails = error.toJSON();
|
|
18
|
-
* console.error(errorDetails.schemaId); // 'UserSchema'
|
|
19
|
-
* console.error(errorDetails.validationErrors); // Array of validation failures
|
|
20
|
-
* }
|
|
21
|
-
* }
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
3
|
+
/** Normalized validation error thrown when object validation fails against a schema. */
|
|
24
4
|
class ValidationError extends Error {
|
|
25
5
|
_object;
|
|
26
6
|
_schemaId;
|
|
27
7
|
_validationErrors;
|
|
28
|
-
/**
|
|
29
|
-
* Creates a validation error instance.
|
|
30
|
-
*
|
|
31
|
-
* @param schemaId - The identifier of the schema that failed validation
|
|
32
|
-
* @param invalidObject - The object that failed validation (may be partially processed)
|
|
33
|
-
* @param validationErrors - Array of validation error details from z-schema, which will be
|
|
34
|
-
* normalized to extract only path, code, params, and message
|
|
35
|
-
*/
|
|
8
|
+
/** Creates a validation error instance. */
|
|
36
9
|
constructor(schemaId, invalidObject, validationErrors) {
|
|
37
10
|
super(`"${schemaId}" validation failed`);
|
|
38
11
|
this._object = invalidObject;
|
|
@@ -45,25 +18,7 @@ class ValidationError extends Error {
|
|
|
45
18
|
message: error.message,
|
|
46
19
|
}));
|
|
47
20
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Returns a JSON serializable representation of the validation error.
|
|
50
|
-
*
|
|
51
|
-
* This method is useful for:
|
|
52
|
-
* - API error responses (can be directly serialized and sent to clients)
|
|
53
|
-
* - Logging validation failures in a structured format
|
|
54
|
-
* - Error reporting and debugging
|
|
55
|
-
*
|
|
56
|
-
* @returns An object containing:
|
|
57
|
-
* - `code`: The error class name ('ValidationError')
|
|
58
|
-
* - `object`: The invalid object that failed validation
|
|
59
|
-
* - `message`: The error message (e.g., '"SchemaId" validation failed')
|
|
60
|
-
* - `schemaId`: The identifier of the schema that failed validation
|
|
61
|
-
* - `validationErrors`: Array of normalized validation errors, each containing:
|
|
62
|
-
* - `path`: JSON path to the invalid field (e.g., '#/user/email')
|
|
63
|
-
* - `code`: Error code from z-schema (e.g., 'INVALID_TYPE', 'REQUIRED')
|
|
64
|
-
* - `params`: Array of error-specific parameters
|
|
65
|
-
* - `message`: Human-readable error message
|
|
66
|
-
*/
|
|
21
|
+
/** Returns a JSON serializable representation of the validation error. */
|
|
67
22
|
toJSON() {
|
|
68
23
|
return {
|
|
69
24
|
code: this.constructor.name,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ValidationError.js","sourceRoot":"","sources":["../src/ValidationError.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"ValidationError.js","sourceRoot":"","sources":["../src/ValidationError.ts"],"names":[],"mappings":";;AASA,wFAAwF;AACxF,MAAM,eAAgB,SAAQ,KAAK;IACzB,OAAO,CAAe;IACtB,SAAS,CAAS;IAClB,iBAAiB,CAA0B;IAEnD,2CAA2C;IAC3C,YAAY,QAAgB,EAAE,aAA2B,EAAE,gBAAqC;QAC9F,KAAK,CAAC,IAAI,QAAQ,qBAAqB,CAAC,CAAC;QAEzC,IAAI,CAAC,OAAO,GAAG,aAAa,CAAC;QAE7B,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAE1B,IAAI,CAAC,iBAAiB,GAAG,gBAAgB;aACtC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACb,IAAI,EAAE,KAAK,CAAC,IAAc;YAC1B,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,MAAM,EAAE,KAAK,CAAC,MAAkB;YAChC,OAAO,EAAE,KAAK,CAAC,OAAO;SACvB,CAAC,CAAC,CAAC;IACR,CAAC;IAED,0EAA0E;IAC1E,MAAM;QACJ,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI;YAC3B,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,QAAQ,EAAE,IAAI,CAAC,SAAS;YACxB,gBAAgB,EAAE,IAAI,CAAC,iBAAiB;SACzC,CAAC;IACJ,CAAC;CACF;AAED,kBAAe,eAAe,CAAC"}
|