@kravc/schema 2.7.6 → 2.8.0-alpha.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.
- package/README.md +19 -14
- package/dist/CredentialFactory.d.ts +345 -0
- package/dist/CredentialFactory.d.ts.map +1 -0
- package/dist/CredentialFactory.js +381 -0
- package/dist/CredentialFactory.js.map +1 -0
- package/dist/Schema.d.ts +448 -0
- package/dist/Schema.d.ts.map +1 -0
- package/dist/Schema.js +506 -0
- package/dist/Schema.js.map +1 -0
- package/dist/ValidationError.d.ts +70 -0
- package/dist/ValidationError.d.ts.map +1 -0
- package/dist/ValidationError.js +78 -0
- package/dist/ValidationError.js.map +1 -0
- package/dist/Validator.d.ts +483 -0
- package/dist/Validator.d.ts.map +1 -0
- package/dist/Validator.js +570 -0
- package/dist/Validator.js.map +1 -0
- package/dist/helpers/JsonSchema.d.ts +99 -0
- package/dist/helpers/JsonSchema.d.ts.map +1 -0
- package/dist/helpers/JsonSchema.js +3 -0
- package/dist/helpers/JsonSchema.js.map +1 -0
- package/dist/helpers/cleanupAttributes.d.ts +34 -0
- package/dist/helpers/cleanupAttributes.d.ts.map +1 -0
- package/dist/helpers/cleanupAttributes.js +113 -0
- package/dist/helpers/cleanupAttributes.js.map +1 -0
- package/dist/helpers/cleanupNulls.d.ts +27 -0
- package/dist/helpers/cleanupNulls.d.ts.map +1 -0
- package/dist/helpers/cleanupNulls.js +96 -0
- package/dist/helpers/cleanupNulls.js.map +1 -0
- package/dist/helpers/createSchemasMap.d.ts +67 -0
- package/dist/helpers/createSchemasMap.d.ts.map +1 -0
- package/dist/helpers/createSchemasMap.js +200 -0
- package/dist/helpers/createSchemasMap.js.map +1 -0
- package/dist/helpers/getReferenceIds.d.ts +169 -0
- package/dist/helpers/getReferenceIds.d.ts.map +1 -0
- package/dist/helpers/getReferenceIds.js +241 -0
- package/dist/helpers/getReferenceIds.js.map +1 -0
- package/dist/helpers/got.d.ts +60 -0
- package/dist/helpers/got.d.ts.map +1 -0
- package/dist/helpers/got.js +72 -0
- package/dist/helpers/got.js.map +1 -0
- package/dist/helpers/mapObjectProperties.d.ts +150 -0
- package/dist/helpers/mapObjectProperties.d.ts.map +1 -0
- package/dist/helpers/mapObjectProperties.js +229 -0
- package/dist/helpers/mapObjectProperties.js.map +1 -0
- package/dist/helpers/normalizeAttributes.d.ts +213 -0
- package/dist/helpers/normalizeAttributes.d.ts.map +1 -0
- package/dist/helpers/normalizeAttributes.js +243 -0
- package/dist/helpers/normalizeAttributes.js.map +1 -0
- package/dist/helpers/normalizeProperties.d.ts +168 -0
- package/dist/helpers/normalizeProperties.d.ts.map +1 -0
- package/dist/helpers/normalizeProperties.js +223 -0
- package/dist/helpers/normalizeProperties.js.map +1 -0
- package/dist/helpers/normalizeRequired.d.ts +159 -0
- package/dist/helpers/normalizeRequired.d.ts.map +1 -0
- package/dist/helpers/normalizeRequired.js +206 -0
- package/dist/helpers/normalizeRequired.js.map +1 -0
- package/dist/helpers/normalizeType.d.ts +81 -0
- package/dist/helpers/normalizeType.d.ts.map +1 -0
- package/dist/helpers/normalizeType.js +210 -0
- package/dist/helpers/normalizeType.js.map +1 -0
- package/dist/helpers/nullifyEmptyValues.d.ts +139 -0
- package/dist/helpers/nullifyEmptyValues.d.ts.map +1 -0
- package/dist/helpers/nullifyEmptyValues.js +191 -0
- package/dist/helpers/nullifyEmptyValues.js.map +1 -0
- package/dist/helpers/removeRequiredAndDefault.d.ts +106 -0
- package/dist/helpers/removeRequiredAndDefault.d.ts.map +1 -0
- package/dist/helpers/removeRequiredAndDefault.js +138 -0
- package/dist/helpers/removeRequiredAndDefault.js.map +1 -0
- package/dist/helpers/validateId.d.ts +39 -0
- package/dist/helpers/validateId.d.ts.map +1 -0
- package/dist/helpers/validateId.js +51 -0
- package/dist/helpers/validateId.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/ld/documentLoader.d.ts +8 -0
- package/dist/ld/documentLoader.d.ts.map +1 -0
- package/dist/ld/documentLoader.js +24 -0
- package/dist/ld/documentLoader.js.map +1 -0
- package/dist/ld/getLinkedDataAttributeType.d.ts +10 -0
- package/dist/ld/getLinkedDataAttributeType.d.ts.map +1 -0
- package/dist/ld/getLinkedDataAttributeType.js +32 -0
- package/dist/ld/getLinkedDataAttributeType.js.map +1 -0
- package/dist/ld/getLinkedDataContext.d.ts +19 -0
- package/dist/ld/getLinkedDataContext.d.ts.map +1 -0
- package/dist/ld/getLinkedDataContext.js +50 -0
- package/dist/ld/getLinkedDataContext.js.map +1 -0
- package/eslint.config.mjs +32 -52
- package/examples/credentials/createAccountCredential.ts +27 -0
- package/examples/credentials/createMineSweeperScoreCredential.ts +115 -0
- package/examples/index.ts +7 -0
- package/examples/schemas/FavoriteItemSchema.ts +27 -0
- package/examples/{Preferences.yaml → schemas/Preferences.yaml} +2 -0
- package/examples/schemas/PreferencesSchema.ts +29 -0
- package/examples/schemas/ProfileSchema.ts +91 -0
- package/examples/schemas/Status.yaml +3 -0
- package/examples/schemas/StatusSchema.ts +12 -0
- package/jest.config.mjs +5 -0
- package/package.json +27 -20
- package/src/CredentialFactory.ts +392 -0
- package/src/Schema.ts +583 -0
- package/src/ValidationError.ts +90 -0
- package/src/Validator.ts +603 -0
- package/src/__tests__/CredentialFactory.test.ts +588 -0
- package/src/__tests__/Schema.test.ts +371 -0
- package/src/__tests__/ValidationError.test.ts +235 -0
- package/src/__tests__/Validator.test.ts +787 -0
- package/src/helpers/JsonSchema.ts +119 -0
- package/src/helpers/__tests__/cleanupAttributes.test.ts +943 -0
- package/src/helpers/__tests__/cleanupNulls.test.ts +772 -0
- package/src/helpers/__tests__/createSchemasMap.test.ts +238 -0
- package/src/helpers/__tests__/getReferenceIds.test.ts +975 -0
- package/src/helpers/__tests__/got.test.ts +193 -0
- package/src/helpers/__tests__/mapObjectProperties.test.ts +1126 -0
- package/src/helpers/__tests__/normalizeAttributes.test.ts +1435 -0
- package/src/helpers/__tests__/normalizeProperties.test.ts +727 -0
- package/src/helpers/__tests__/normalizeRequired.test.ts +669 -0
- package/src/helpers/__tests__/normalizeType.test.ts +772 -0
- package/src/helpers/__tests__/nullifyEmptyValues.test.ts +735 -0
- package/src/helpers/__tests__/removeRequiredAndDefault.test.ts +734 -0
- package/src/helpers/__tests__/validateId.test.ts +118 -0
- package/src/helpers/cleanupAttributes.ts +151 -0
- package/src/helpers/cleanupNulls.ts +106 -0
- package/src/helpers/createSchemasMap.ts +212 -0
- package/src/helpers/getReferenceIds.ts +273 -0
- package/src/helpers/got.ts +73 -0
- package/src/helpers/mapObjectProperties.ts +272 -0
- package/src/helpers/normalizeAttributes.ts +247 -0
- package/src/helpers/normalizeProperties.ts +249 -0
- package/src/helpers/normalizeRequired.ts +233 -0
- package/src/helpers/normalizeType.ts +235 -0
- package/src/helpers/nullifyEmptyValues.ts +207 -0
- package/src/helpers/removeRequiredAndDefault.ts +151 -0
- package/src/helpers/validateId.ts +53 -0
- package/src/index.ts +17 -0
- package/src/ld/__tests__/documentLoader.test.ts +57 -0
- package/src/ld/__tests__/getLinkedDataAttributeType.test.ts +212 -0
- package/src/ld/__tests__/getLinkedDataContext.test.ts +378 -0
- package/src/ld/documentLoader.ts +28 -0
- package/src/ld/getLinkedDataAttributeType.ts +46 -0
- package/src/ld/getLinkedDataContext.ts +80 -0
- package/tsconfig.json +27 -0
- package/types/credentials-context.d.ts +14 -0
- package/types/security-context.d.ts +6 -0
- package/examples/Status.yaml +0 -3
- package/examples/createAccountCredential.js +0 -27
- package/examples/createMineSweeperScoreCredential.js +0 -63
- package/examples/index.js +0 -9
- package/src/CredentialFactory.js +0 -67
- package/src/CredentialFactory.spec.js +0 -131
- package/src/Schema.js +0 -104
- package/src/Schema.spec.js +0 -172
- package/src/ValidationError.js +0 -31
- package/src/Validator.js +0 -128
- package/src/Validator.spec.js +0 -355
- package/src/helpers/cleanupAttributes.js +0 -71
- package/src/helpers/cleanupNulls.js +0 -42
- package/src/helpers/getReferenceIds.js +0 -71
- package/src/helpers/mapObject.js +0 -65
- package/src/helpers/normalizeAttributes.js +0 -28
- package/src/helpers/normalizeProperties.js +0 -61
- package/src/helpers/normalizeRequired.js +0 -37
- package/src/helpers/normalizeType.js +0 -41
- package/src/helpers/nullifyEmptyValues.js +0 -57
- package/src/helpers/removeRequiredAndDefault.js +0 -30
- package/src/helpers/validateId.js +0 -19
- package/src/index.d.ts +0 -25
- package/src/index.js +0 -8
- package/src/ld/documentLoader.js +0 -25
- package/src/ld/documentLoader.spec.js +0 -12
- package/src/ld/getLinkedDataContext.js +0 -63
- package/src/ld/getLinkedDataType.js +0 -38
- /package/examples/{FavoriteItem.yaml → schemas/FavoriteItem.yaml} +0 -0
- /package/examples/{Profile.yaml → schemas/Profile.yaml} +0 -0
package/dist/Schema.d.ts
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import type { LinkedDataContext } from './ld/getLinkedDataContext';
|
|
2
|
+
import type { JsonSchema, EnumSchema, PropertiesSchema } from './helpers/JsonSchema';
|
|
3
|
+
/**
|
|
4
|
+
* JSON-LD linked data type structure for semantic web integration.
|
|
5
|
+
*
|
|
6
|
+
* **Intent:** Provide a standardized format for representing schemas in the
|
|
7
|
+
* semantic web, enabling integration with Linked Data and Verifiable Credentials.
|
|
8
|
+
*
|
|
9
|
+
* **Use Cases:**
|
|
10
|
+
* - Generate Verifiable Credentials with proper semantic context
|
|
11
|
+
* - Create JSON-LD documents compatible with schema.org and other vocabularies
|
|
12
|
+
* - Enable semantic interoperability between different systems
|
|
13
|
+
* - Support decentralized identity and credential systems
|
|
14
|
+
*
|
|
15
|
+
* **Structure:**
|
|
16
|
+
* - `@id`: The unique identifier (URI) for the schema type
|
|
17
|
+
* - `@context`: The JSON-LD context mapping properties to semantic types
|
|
18
|
+
*
|
|
19
|
+
* **Example:**
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const schema = new Schema(
|
|
22
|
+
* { name: { type: 'string' }, email: { type: 'string', format: 'email' } },
|
|
23
|
+
* 'Person',
|
|
24
|
+
* 'https://schema.org/'
|
|
25
|
+
* );
|
|
26
|
+
*
|
|
27
|
+
* const linkedData = schema.linkedDataType;
|
|
28
|
+
* // {
|
|
29
|
+
* // '@id': 'https://schema.org/Person',
|
|
30
|
+
* // '@context': {
|
|
31
|
+
* // '@vocab': 'https://schema.org/',
|
|
32
|
+
* // 'name': 'https://schema.org/name',
|
|
33
|
+
* // 'email': 'https://schema.org/email'
|
|
34
|
+
* // }
|
|
35
|
+
* // }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export type LinkedDataType = {
|
|
39
|
+
'@id': string;
|
|
40
|
+
'@context': LinkedDataContext;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Schema class for defining and manipulating JSON schemas for object validation.
|
|
44
|
+
*
|
|
45
|
+
* This class provides a flexible API for creating, transforming, and composing JSON schemas.
|
|
46
|
+
* It supports both property-based schemas (for objects) and enum schemas (for value sets).
|
|
47
|
+
*
|
|
48
|
+
* **Use Cases:**
|
|
49
|
+
* - Define validation schemas for API request/response objects
|
|
50
|
+
* - Create reusable schema components that can be extended or composed
|
|
51
|
+
* - Generate JSON-LD linked data types for semantic web applications
|
|
52
|
+
* - Transform schemas for different use cases (e.g., create update schemas from create schemas)
|
|
53
|
+
*
|
|
54
|
+
* **Example - Basic Usage:**
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const userSchema = new Schema({
|
|
57
|
+
* firstName: { type: 'string', required: true },
|
|
58
|
+
* lastName: { type: 'string', required: true },
|
|
59
|
+
* email: { type: 'string', format: 'email', required: true }
|
|
60
|
+
* }, 'User');
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* **Example - Schema Composition:**
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const baseSchema = new Schema({ name: { required: true } }, 'Base');
|
|
66
|
+
* const extendedSchema = baseSchema.extend({ status: { enum: ['Active', 'Inactive'] } }, 'Extended');
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* **Example - Linked Data Types:**
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const schema = new Schema(
|
|
72
|
+
* { id: { type: 'string' }, name: { type: 'string' } },
|
|
73
|
+
* 'Person',
|
|
74
|
+
* 'https://schema.org/'
|
|
75
|
+
* );
|
|
76
|
+
* // schema.linkedDataType will contain JSON-LD context
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
declare class Schema {
|
|
80
|
+
private _id;
|
|
81
|
+
private _url?;
|
|
82
|
+
private _source;
|
|
83
|
+
private _linkedDataType?;
|
|
84
|
+
/**
|
|
85
|
+
* Creates a new Schema instance.
|
|
86
|
+
*
|
|
87
|
+
* **Intent:** Initialize a schema with properties, enum values, or clone from another schema.
|
|
88
|
+
* Automatically normalizes properties, infers types, and optionally creates JSON-LD linked data types.
|
|
89
|
+
*
|
|
90
|
+
* **Use Cases:**
|
|
91
|
+
* - Create a new schema from scratch with property definitions
|
|
92
|
+
* - Create an enum schema for value validation
|
|
93
|
+
* - Clone an existing schema with a new ID
|
|
94
|
+
* - Create a schema with JSON-LD context for semantic web applications
|
|
95
|
+
*
|
|
96
|
+
* @param propertiesOrSchema - Property definitions, enum schema, or existing Schema instance to clone
|
|
97
|
+
* @param id - Unique identifier for the schema (defaults to 'UNDEFINED_SCHEMA_ID' if not provided)
|
|
98
|
+
* @param url - Optional URL for generating JSON-LD linked data types (only for property schemas, not enums)
|
|
99
|
+
*
|
|
100
|
+
* **Example - Property Schema:**
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const schema = new Schema({
|
|
103
|
+
* name: { type: 'string', required: true },
|
|
104
|
+
* age: { type: 'number', minimum: 0 }
|
|
105
|
+
* }, 'Person');
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* **Example - Enum Schema:**
|
|
109
|
+
* ```typescript
|
|
110
|
+
* const sizeSchema = new Schema({ enum: ['S', 'M', 'L'], type: 'string' }, 'Size');
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* **Example - Clone Schema:**
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const original = new Schema({ name: { type: 'string' } }, 'Original');
|
|
116
|
+
* const cloned = new Schema(original, 'Cloned');
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* **Example - With Linked Data URL:**
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const schema = new Schema(
|
|
122
|
+
* { name: { type: 'string' } },
|
|
123
|
+
* 'Person',
|
|
124
|
+
* 'https://schema.org/'
|
|
125
|
+
* );
|
|
126
|
+
* // Automatically creates linkedDataType with @id and @context
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
constructor(propertiesOrSchema: EnumSchema | PropertiesSchema | Schema, id?: string, url?: string);
|
|
130
|
+
/**
|
|
131
|
+
* Returns the unique identifier for this schema.
|
|
132
|
+
*
|
|
133
|
+
* **Intent:** Provide a way to reference and identify schemas in collections or validators.
|
|
134
|
+
*
|
|
135
|
+
* **Use Cases:**
|
|
136
|
+
* - Reference schemas in Validator instances
|
|
137
|
+
* - Track schema lineage when cloning/extending
|
|
138
|
+
* - Generate schema identifiers for JSON Schema output
|
|
139
|
+
*
|
|
140
|
+
* @returns The schema ID, or 'UNDEFINED_SCHEMA_ID' if not specified during construction
|
|
141
|
+
*
|
|
142
|
+
* **Example:**
|
|
143
|
+
* ```typescript
|
|
144
|
+
* const schema = new Schema({ name: { type: 'string' } }, 'User');
|
|
145
|
+
* console.log(schema.id); // 'User'
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
get id(): string;
|
|
149
|
+
/**
|
|
150
|
+
* Returns the URL associated with this schema for JSON-LD linked data generation.
|
|
151
|
+
*
|
|
152
|
+
* **Intent:** Provide access to the base URL used for generating semantic web identifiers.
|
|
153
|
+
*
|
|
154
|
+
* **Use Cases:**
|
|
155
|
+
* - Check if a schema has linked data support
|
|
156
|
+
* - Access the base URL for constructing related URIs
|
|
157
|
+
* - Debug linked data type generation
|
|
158
|
+
*
|
|
159
|
+
* @returns The schema URL if provided during construction, undefined otherwise
|
|
160
|
+
*
|
|
161
|
+
* **Example:**
|
|
162
|
+
* ```typescript
|
|
163
|
+
* const schema = new Schema({ name: { type: 'string' } }, 'Person', 'https://schema.org/');
|
|
164
|
+
* console.log(schema.url); // 'https://schema.org/'
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
get url(): string | undefined;
|
|
168
|
+
/**
|
|
169
|
+
* Returns a deep clone of the schema's source properties or enum definition.
|
|
170
|
+
*
|
|
171
|
+
* **Intent:** Provide immutable access to schema definitions to prevent accidental mutations.
|
|
172
|
+
*
|
|
173
|
+
* **Use Cases:**
|
|
174
|
+
* - Inspect schema structure without modifying the original
|
|
175
|
+
* - Clone schemas manually for custom transformations
|
|
176
|
+
* - Debug schema definitions
|
|
177
|
+
* - Pass schema definitions to other functions safely
|
|
178
|
+
*
|
|
179
|
+
* @returns A deep copy of the schema source (PropertiesSchema or EnumSchema)
|
|
180
|
+
*
|
|
181
|
+
* **Example:**
|
|
182
|
+
* ```typescript
|
|
183
|
+
* const schema = new Schema({ name: { type: 'string', required: true } }, 'User');
|
|
184
|
+
* const source = schema.source;
|
|
185
|
+
* // source is a clone, modifying it won't affect the original schema
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
get source(): EnumSchema | PropertiesSchema;
|
|
189
|
+
/**
|
|
190
|
+
* Checks if this schema is an enum schema (defines a set of allowed values).
|
|
191
|
+
*
|
|
192
|
+
* **Intent:** Enable type-safe branching logic based on schema type.
|
|
193
|
+
*
|
|
194
|
+
* **Use Cases:**
|
|
195
|
+
* - Conditionally apply methods that only work on property schemas
|
|
196
|
+
* - Validate schema type before operations
|
|
197
|
+
* - Implement different serialization logic for enums vs objects
|
|
198
|
+
*
|
|
199
|
+
* @returns true if the schema is an enum schema, false if it's a properties schema
|
|
200
|
+
*
|
|
201
|
+
* **Example:**
|
|
202
|
+
* ```typescript
|
|
203
|
+
* const enumSchema = new Schema({ enum: ['A', 'B', 'C'] }, 'Letters');
|
|
204
|
+
* const propSchema = new Schema({ name: { type: 'string' } }, 'Person');
|
|
205
|
+
*
|
|
206
|
+
* console.log(enumSchema.isEnum); // true
|
|
207
|
+
* console.log(propSchema.isEnum); // false
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
get isEnum(): boolean;
|
|
211
|
+
/**
|
|
212
|
+
* Returns a normalized JSON Schema representation of this schema.
|
|
213
|
+
*
|
|
214
|
+
* **Intent:** Convert the internal schema representation to standard JSON Schema format
|
|
215
|
+
* with normalized required arrays and proper structure for validation libraries.
|
|
216
|
+
*
|
|
217
|
+
* **Use Cases:**
|
|
218
|
+
* - Export schemas for use with JSON Schema validators (e.g., z-schema)
|
|
219
|
+
* - Generate API documentation from schemas
|
|
220
|
+
* - Serialize schemas to JSON for storage or transmission
|
|
221
|
+
* - Integrate with tools that expect standard JSON Schema format
|
|
222
|
+
*
|
|
223
|
+
* @returns A JsonSchema object (ObjectSchema for property schemas, EnumSchema for enum schemas)
|
|
224
|
+
*
|
|
225
|
+
* **Example - Property Schema:**
|
|
226
|
+
* ```typescript
|
|
227
|
+
* const schema = new Schema({
|
|
228
|
+
* name: { type: 'string', required: true },
|
|
229
|
+
* age: { type: 'number' }
|
|
230
|
+
* }, 'Person');
|
|
231
|
+
*
|
|
232
|
+
* const jsonSchema = schema.jsonSchema;
|
|
233
|
+
* // {
|
|
234
|
+
* // id: 'Person',
|
|
235
|
+
* // type: 'object',
|
|
236
|
+
* // properties: { name: {...}, age: {...} },
|
|
237
|
+
* // required: ['name']
|
|
238
|
+
* // }
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* **Example - Enum Schema:**
|
|
242
|
+
* ```typescript
|
|
243
|
+
* const enumSchema = new Schema({ enum: ['S', 'M', 'L'], type: 'string' }, 'Size');
|
|
244
|
+
* const jsonSchema = enumSchema.jsonSchema;
|
|
245
|
+
* // { id: 'Size', enum: ['S', 'M', 'L'], type: 'string' }
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
get jsonSchema(): JsonSchema;
|
|
249
|
+
/**
|
|
250
|
+
* Returns the JSON-LD linked data type if a URL was provided during construction.
|
|
251
|
+
*
|
|
252
|
+
* **Intent:** Provide semantic web identifiers and context for schemas, enabling
|
|
253
|
+
* integration with Linked Data and Verifiable Credentials standards.
|
|
254
|
+
*
|
|
255
|
+
* **Use Cases:**
|
|
256
|
+
* - Generate Verifiable Credentials with proper semantic context
|
|
257
|
+
* - Create JSON-LD documents with schema.org or custom vocabularies
|
|
258
|
+
* - Enable semantic interoperability between systems
|
|
259
|
+
* - Support decentralized identity and credential systems
|
|
260
|
+
*
|
|
261
|
+
* @returns LinkedDataType object with @id and @context, or undefined if no URL was provided
|
|
262
|
+
*
|
|
263
|
+
* **Example:**
|
|
264
|
+
* ```typescript
|
|
265
|
+
* const schema = new Schema(
|
|
266
|
+
* { name: { type: 'string' }, email: { type: 'string', format: 'email' } },
|
|
267
|
+
* 'Person',
|
|
268
|
+
* 'https://schema.org/'
|
|
269
|
+
* );
|
|
270
|
+
*
|
|
271
|
+
* const linkedData = schema.linkedDataType;
|
|
272
|
+
* // {
|
|
273
|
+
* // '@id': 'https://schema.org/Person',
|
|
274
|
+
* // '@context': { ... } // Generated context mapping types to schema.org
|
|
275
|
+
* // }
|
|
276
|
+
* ```
|
|
277
|
+
*
|
|
278
|
+
* **Note:** Linked data types are only generated for property schemas, not enum schemas.
|
|
279
|
+
*/
|
|
280
|
+
get linkedDataType(): LinkedDataType | undefined;
|
|
281
|
+
/**
|
|
282
|
+
* Creates a complete clone of the schema with a new identifier.
|
|
283
|
+
*
|
|
284
|
+
* **Intent:** Duplicate a schema while maintaining all properties and structure,
|
|
285
|
+
* useful for creating variations or versioning schemas.
|
|
286
|
+
*
|
|
287
|
+
* **Use Cases:**
|
|
288
|
+
* - Create schema versions (e.g., 'UserV1', 'UserV2')
|
|
289
|
+
* - Duplicate schemas for different contexts
|
|
290
|
+
* - Create base schemas that can be independently extended
|
|
291
|
+
* - Maintain schema history or branching
|
|
292
|
+
*
|
|
293
|
+
* @param id - The new unique identifier for the cloned schema
|
|
294
|
+
* @returns A new Schema instance with identical structure but different ID
|
|
295
|
+
*
|
|
296
|
+
* **Example:**
|
|
297
|
+
* ```typescript
|
|
298
|
+
* const userSchema = new Schema({ name: { type: 'string' } }, 'User');
|
|
299
|
+
* const userV2Schema = userSchema.clone('UserV2');
|
|
300
|
+
* // Both schemas have identical properties but different IDs
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
clone(id: string): Schema;
|
|
304
|
+
/**
|
|
305
|
+
* Creates a schema clone without required constraints and default values.
|
|
306
|
+
*
|
|
307
|
+
* **Intent:** Generate update/patch schemas from create schemas by removing
|
|
308
|
+
* validation constraints that make sense for creation but not for updates.
|
|
309
|
+
*
|
|
310
|
+
* **Use Cases:**
|
|
311
|
+
* - Create update schemas where all fields are optional
|
|
312
|
+
* - Generate PATCH endpoint schemas from POST endpoint schemas
|
|
313
|
+
* - Allow partial updates without requiring all original fields
|
|
314
|
+
* - Create flexible input schemas for editing operations
|
|
315
|
+
*
|
|
316
|
+
* @param id - The new unique identifier for the pure schema
|
|
317
|
+
* @returns A new Schema instance with all required flags and defaults removed
|
|
318
|
+
* @throws Error if called on an enum schema
|
|
319
|
+
*
|
|
320
|
+
* **Example:**
|
|
321
|
+
* ```typescript
|
|
322
|
+
* const createSchema = new Schema({
|
|
323
|
+
* name: { type: 'string', required: true },
|
|
324
|
+
* email: { type: 'string', required: true, default: 'user@example.com' }
|
|
325
|
+
* }, 'CreateUser');
|
|
326
|
+
*
|
|
327
|
+
* const updateSchema = createSchema.pure('UpdateUser');
|
|
328
|
+
* // updateSchema has no required fields and no defaults
|
|
329
|
+
* // All fields are optional for updates
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
pure(id: string): Schema;
|
|
333
|
+
/**
|
|
334
|
+
* Creates a schema clone containing only the specified properties.
|
|
335
|
+
*
|
|
336
|
+
* **Intent:** Create focused schemas that expose only a subset of properties,
|
|
337
|
+
* useful for different API views or security contexts.
|
|
338
|
+
*
|
|
339
|
+
* **Use Cases:**
|
|
340
|
+
* - Create public-facing schemas that hide sensitive fields
|
|
341
|
+
* - Generate response schemas with only necessary fields
|
|
342
|
+
* - Create view-specific schemas (e.g., 'UserSummary', 'UserDetail')
|
|
343
|
+
* - Implement field-level access control in schemas
|
|
344
|
+
* - Create lightweight schemas for list views
|
|
345
|
+
*
|
|
346
|
+
* @param propertyNames - Array of property names to include in the new schema
|
|
347
|
+
* @param id - The new unique identifier for the filtered schema
|
|
348
|
+
* @returns A new Schema instance containing only the specified properties
|
|
349
|
+
* @throws Error if called on an enum schema
|
|
350
|
+
*
|
|
351
|
+
* **Example:**
|
|
352
|
+
* ```typescript
|
|
353
|
+
* const fullSchema = new Schema({
|
|
354
|
+
* name: { type: 'string' },
|
|
355
|
+
* email: { type: 'string' },
|
|
356
|
+
* password: { type: 'string' },
|
|
357
|
+
* ssn: { type: 'string' }
|
|
358
|
+
* }, 'User');
|
|
359
|
+
*
|
|
360
|
+
* const publicSchema = fullSchema.only(['name', 'email'], 'PublicUser');
|
|
361
|
+
* // publicSchema only contains name and email, hiding sensitive fields
|
|
362
|
+
* ```
|
|
363
|
+
*/
|
|
364
|
+
only(propertyNames: string[], id: string): Schema;
|
|
365
|
+
/**
|
|
366
|
+
* Creates a schema clone extended with additional properties.
|
|
367
|
+
*
|
|
368
|
+
* **Intent:** Build complex schemas incrementally by composing base schemas
|
|
369
|
+
* with additional fields, enabling schema inheritance and composition patterns.
|
|
370
|
+
*
|
|
371
|
+
* **Use Cases:**
|
|
372
|
+
* - Extend base schemas with domain-specific fields
|
|
373
|
+
* - Create schema hierarchies (e.g., BaseEntity -> User -> AdminUser)
|
|
374
|
+
* - Add computed or derived fields to existing schemas
|
|
375
|
+
* - Compose schemas from multiple sources
|
|
376
|
+
* - Build versioned schemas that add new fields
|
|
377
|
+
*
|
|
378
|
+
* @param properties - Additional properties to merge into the schema
|
|
379
|
+
* @param id - The new unique identifier for the extended schema
|
|
380
|
+
* @returns A new Schema instance with merged properties (new properties override existing ones)
|
|
381
|
+
* @throws Error if called on an enum schema
|
|
382
|
+
*
|
|
383
|
+
* **Example:**
|
|
384
|
+
* ```typescript
|
|
385
|
+
* const baseSchema = new Schema({
|
|
386
|
+
* id: { type: 'string', required: true },
|
|
387
|
+
* createdAt: { type: 'string', format: 'date-time', required: true }
|
|
388
|
+
* }, 'BaseEntity');
|
|
389
|
+
*
|
|
390
|
+
* const userSchema = baseSchema.extend({
|
|
391
|
+
* name: { type: 'string', required: true },
|
|
392
|
+
* email: { type: 'string', format: 'email', required: true }
|
|
393
|
+
* }, 'User');
|
|
394
|
+
*
|
|
395
|
+
* // userSchema contains id, createdAt, name, and email
|
|
396
|
+
* ```
|
|
397
|
+
*/
|
|
398
|
+
extend(properties: PropertiesSchema, id: string): Schema;
|
|
399
|
+
/**
|
|
400
|
+
* Creates a new schema that wraps the current schema as a nested property.
|
|
401
|
+
*
|
|
402
|
+
* **Intent:** Transform a flat schema into a nested structure, useful for
|
|
403
|
+
* API response formatting or when data needs to be wrapped in a container object.
|
|
404
|
+
*
|
|
405
|
+
* **Use Cases:**
|
|
406
|
+
* - Wrap schemas for API responses (e.g., { data: { ...schema } })
|
|
407
|
+
* - Create nested data structures from flat schemas
|
|
408
|
+
* - Format schemas for specific API conventions
|
|
409
|
+
* - Generate wrapper schemas for pagination or metadata containers
|
|
410
|
+
* - Transform schemas for different API versions or formats
|
|
411
|
+
*
|
|
412
|
+
* @param propertyName - The name of the property that will contain the wrapped schema
|
|
413
|
+
* @param attributes - Optional attributes for the wrapper property (default: { required: true })
|
|
414
|
+
* @param id - The new unique identifier for the wrapped schema
|
|
415
|
+
* @returns A new Schema instance where the original schema is nested under the specified property
|
|
416
|
+
* @throws Error if called on an enum schema
|
|
417
|
+
*
|
|
418
|
+
* **Example:**
|
|
419
|
+
* ```typescript
|
|
420
|
+
* const userSchema = new Schema({
|
|
421
|
+
* name: { type: 'string', required: true },
|
|
422
|
+
* email: { type: 'string', required: true }
|
|
423
|
+
* }, 'User');
|
|
424
|
+
*
|
|
425
|
+
* const wrappedSchema = userSchema.wrap('data', { required: true }, 'UserResponse');
|
|
426
|
+
* // Resulting schema structure:
|
|
427
|
+
* // {
|
|
428
|
+
* // data: {
|
|
429
|
+
* // type: 'object',
|
|
430
|
+
* // properties: { name: {...}, email: {...} },
|
|
431
|
+
* // required: true
|
|
432
|
+
* // }
|
|
433
|
+
* // }
|
|
434
|
+
* ```
|
|
435
|
+
*
|
|
436
|
+
* **Example - With Default Value:**
|
|
437
|
+
* ```typescript
|
|
438
|
+
* const wrappedSchema = userSchema.wrap('data', { default: '{}' }, 'OptionalUserResponse');
|
|
439
|
+
* // The 'data' property has a default value and is not required
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
wrap(propertyName: string, attributes: {
|
|
443
|
+
default?: string;
|
|
444
|
+
required?: boolean;
|
|
445
|
+
}, id: string): Schema;
|
|
446
|
+
}
|
|
447
|
+
export default Schema;
|
|
448
|
+
//# sourceMappingURL=Schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,KAAK,EACV,UAAU,EACV,UAAU,EACV,gBAAgB,EAEjB,MAAM,sBAAsB,CAAC;AAI9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,iBAAiB,CAAC;CAC/B,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,cAAM,MAAM;IACV,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,IAAI,CAAC,CAAS;IACtB,OAAO,CAAC,OAAO,CAAgC;IAC/C,OAAO,CAAC,eAAe,CAAC,CAAiB;IAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4CG;gBACS,kBAAkB,EAAE,UAAU,GAAG,gBAAgB,GAAG,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM;IA6CjG;;;;;;;;;;;;;;;;;OAiBG;IACH,IAAI,EAAE,WAEL;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,IAAI,GAAG,uBAEN;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,IAAI,MAAM,kCAET;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,IAAI,MAAM,YAET;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACH,IAAI,UAAU,IAAI,UAAU,CAiB3B;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,IAAI,cAAc,+BAEjB;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,KAAK,CAAC,EAAE,EAAE,MAAM;IAIhB;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,IAAI,CAAC,EAAE,EAAE,MAAM;IAaf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,IAAI,CAAC,aAAa,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,MAAM;IASxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAgCG;IACH,MAAM,CAAC,UAAU,EAAE,gBAAgB,EAAE,EAAE,EAAE,MAAM;IAQ/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA0CG;IACH,IAAI,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;KAAE,EAAE,EAAE,EAAE,MAAM;CAe7F;AAED,eAAe,MAAM,CAAC"}
|