@kravc/schema 2.7.6 → 2.8.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +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/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 +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -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__/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/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 +13 -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/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @kravc/schema
|
|
2
2
|
|
|
3
|
-
Advanced JSON schema manipulation and validation library
|
|
4
|
-
[z-schema](https://github.com/zaggino/z-schema).
|
|
3
|
+
Advanced JSON schema manipulation and validation library written in TypeScript,
|
|
4
|
+
based on [z-schema](https://github.com/zaggino/z-schema).
|
|
5
5
|
|
|
6
6
|
## Get Started
|
|
7
7
|
|
|
@@ -11,8 +11,8 @@ Install npm dependency:
|
|
|
11
11
|
npm i --save @kravc/schema
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
```
|
|
15
|
-
|
|
14
|
+
```ts
|
|
15
|
+
import { Schema, Validator } from '@kravc/schema'
|
|
16
16
|
|
|
17
17
|
const userSchema = new Schema({
|
|
18
18
|
firstName: { required: true },
|
|
@@ -41,8 +41,8 @@ Expected output:
|
|
|
41
41
|
|
|
42
42
|
Other `Schema` and `Validator` usage examples:
|
|
43
43
|
|
|
44
|
-
- [Schema](./src/Schema.
|
|
45
|
-
- [Validator](./src/Validator.
|
|
44
|
+
- [Schema](./src/__tests__/Schema.test.ts)
|
|
45
|
+
- [Validator](./src/__tests__/Validator.test.ts)
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
## Verifiable Credentials
|
|
@@ -53,8 +53,8 @@ linked data context. Common json schema types and formats (`integer`,
|
|
|
53
53
|
|
|
54
54
|
Define schema for a credential subject:
|
|
55
55
|
|
|
56
|
-
```
|
|
57
|
-
|
|
56
|
+
```ts
|
|
57
|
+
import { Schema } from '@kravc/schema'
|
|
58
58
|
|
|
59
59
|
const accountSchema = new Schema({
|
|
60
60
|
id: { required: true },
|
|
@@ -67,8 +67,8 @@ const accountSchema = new Schema({
|
|
|
67
67
|
Initialize credential factory by providing credential URI and credential subject
|
|
68
68
|
schemas:
|
|
69
69
|
|
|
70
|
-
```
|
|
71
|
-
|
|
70
|
+
```ts
|
|
71
|
+
import { CredentialFactory } from '@kravc/schema'
|
|
72
72
|
|
|
73
73
|
const factory = new CredentialFactory('https://example.com/schema/AccountV1', [ accountSchema ])
|
|
74
74
|
```
|
|
@@ -76,7 +76,7 @@ const factory = new CredentialFactory('https://example.com/schema/AccountV1', [
|
|
|
76
76
|
Create a credential for a specific subject, `createCredential` method validates
|
|
77
77
|
the input and populates any defaults defined by schema:
|
|
78
78
|
|
|
79
|
-
```
|
|
79
|
+
```ts
|
|
80
80
|
const holder = 'did:HOLDER_ID'
|
|
81
81
|
const username = 'USER'
|
|
82
82
|
const createdAt = new Date().toISOString()
|
|
@@ -94,7 +94,7 @@ console.log(JSON.stringify(credential, null, 2))
|
|
|
94
94
|
|
|
95
95
|
Expected JSON-LD output (could be verified using [JSON-LD Playground](https://json-ld.org/playground/)):
|
|
96
96
|
|
|
97
|
-
```
|
|
97
|
+
```json
|
|
98
98
|
{
|
|
99
99
|
"@context": [
|
|
100
100
|
"https://www.w3.org/2018/credentials/v1",
|
|
@@ -144,5 +144,10 @@ to be set by issuing function (e.g [@kravc/identity](http://github.com/alexkrave
|
|
|
144
144
|
|
|
145
145
|
Other `CredentialFactory` examples:
|
|
146
146
|
|
|
147
|
-
- [createAccountCredential](./examples/createAccountCredential.
|
|
148
|
-
- [createMineSweeperScoreCredential](./examples/createMineSweeperScoreCredential.
|
|
147
|
+
- [createAccountCredential](./examples/credentials/createAccountCredential.ts)
|
|
148
|
+
- [createMineSweeperScoreCredential](./examples/credentials/createMineSweeperScoreCredential.ts)
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
Revision: January 27, 2026<br/>
|
|
153
|
+
By: Alex Kravets (@alexkravets)
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import type { TargetObject } from './helpers/JsonSchema';
|
|
2
|
+
import Schema, { type LinkedDataType } from './Schema';
|
|
3
|
+
/**
|
|
4
|
+
* Factory class for creating Verifiable Credentials with embedded JSON-LD linked data context.
|
|
5
|
+
*
|
|
6
|
+
* **Intent:** Provide a standardized way to generate W3C Verifiable Credentials that are
|
|
7
|
+
* compatible with JSON-LD and semantic web standards. The factory automatically validates
|
|
8
|
+
* credential subjects against provided schemas, generates appropriate JSON-LD contexts, and
|
|
9
|
+
* structures credentials according to the Verifiable Credentials data model.
|
|
10
|
+
*
|
|
11
|
+
* **Use Cases:**
|
|
12
|
+
* - Generate Verifiable Credentials for decentralized identity systems
|
|
13
|
+
* - Create credentials with automatic schema validation and type checking
|
|
14
|
+
* - Build credentials with embedded semantic context (schema.org, custom vocabularies)
|
|
15
|
+
* - Support multi-schema credentials with nested object references
|
|
16
|
+
* - Enable credential interoperability through standardized JSON-LD contexts
|
|
17
|
+
* - Integrate with credential issuance systems (e.g., DID-based identity providers)
|
|
18
|
+
*
|
|
19
|
+
* **Key Features:**
|
|
20
|
+
* - Automatic validation of credential subjects against schema definitions
|
|
21
|
+
* - JSON-LD context generation for semantic web compatibility
|
|
22
|
+
* - Support for multiple schemas with cross-references (`$ref`)
|
|
23
|
+
* - Type-safe credential generation with TypeScript support
|
|
24
|
+
* - Schema.org type mapping for common data formats (date-time, date, etc.)
|
|
25
|
+
*
|
|
26
|
+
* **Example - Simple Credential:**
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { Schema, CredentialFactory } from '@kravc/schema';
|
|
29
|
+
*
|
|
30
|
+
* // Define credential subject schema
|
|
31
|
+
* const accountSchema = new Schema({
|
|
32
|
+
* id: {},
|
|
33
|
+
* username: { required: true },
|
|
34
|
+
* createdAt: { format: 'date-time', required: true },
|
|
35
|
+
* dateOfBirth: { format: 'date' }
|
|
36
|
+
* }, 'Account');
|
|
37
|
+
*
|
|
38
|
+
* // Create factory with credential URI and schemas
|
|
39
|
+
* const factory = new CredentialFactory(
|
|
40
|
+
* 'https://example.com/schema/AccountV1',
|
|
41
|
+
* [accountSchema]
|
|
42
|
+
* );
|
|
43
|
+
*
|
|
44
|
+
* // Generate credential
|
|
45
|
+
* const credential = factory.createCredential(
|
|
46
|
+
* 'https://example.com/credentials/123',
|
|
47
|
+
* 'did:holder:123',
|
|
48
|
+
* {
|
|
49
|
+
* id: 'did:holder:123',
|
|
50
|
+
* username: 'alice',
|
|
51
|
+
* createdAt: new Date().toISOString()
|
|
52
|
+
* }
|
|
53
|
+
* );
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* **Example - Multi-Schema Credential with References:**
|
|
57
|
+
* ```typescript
|
|
58
|
+
* import { Schema, CredentialFactory } from '@kravc/schema';
|
|
59
|
+
*
|
|
60
|
+
* // Define multiple related schemas
|
|
61
|
+
* const playerSchema = new Schema({
|
|
62
|
+
* id: {},
|
|
63
|
+
* hasVideoGameScore: { $ref: 'VideoGameScore', required: true }
|
|
64
|
+
* }, 'Player');
|
|
65
|
+
*
|
|
66
|
+
* const videoGameSchema = new Schema({
|
|
67
|
+
* id: {},
|
|
68
|
+
* name: { type: 'string', required: true },
|
|
69
|
+
* version: { type: 'string', required: true }
|
|
70
|
+
* }, 'VideoGame', 'https://schema.org/');
|
|
71
|
+
*
|
|
72
|
+
* const scoreSchema = new Schema({
|
|
73
|
+
* game: { $ref: 'VideoGame', required: true },
|
|
74
|
+
* wins: { type: 'integer', required: true },
|
|
75
|
+
* bestScore: { type: 'integer', required: true }
|
|
76
|
+
* }, 'VideoGameScore');
|
|
77
|
+
*
|
|
78
|
+
* // Create factory with multiple schemas
|
|
79
|
+
* const factory = new CredentialFactory(
|
|
80
|
+
* 'https://example.com/schema/GameScoreV1',
|
|
81
|
+
* [playerSchema, videoGameSchema, scoreSchema]
|
|
82
|
+
* );
|
|
83
|
+
*
|
|
84
|
+
* // Generate credential with nested data
|
|
85
|
+
* const credential = factory.createCredential(
|
|
86
|
+
* 'https://example.com/credentials/score-456',
|
|
87
|
+
* 'did:player:789',
|
|
88
|
+
* {
|
|
89
|
+
* id: 'did:player:789',
|
|
90
|
+
* hasVideoGameScore: {
|
|
91
|
+
* game: { id: 'did:game:001', name: 'MineSweeper', version: '1.0' },
|
|
92
|
+
* wins: 10,
|
|
93
|
+
* bestScore: 5000
|
|
94
|
+
* }
|
|
95
|
+
* }
|
|
96
|
+
* );
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* **Example - Accessing Credential Context:**
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const factory = new CredentialFactory(uri, schemas);
|
|
102
|
+
*
|
|
103
|
+
* // Get credential type (extracted from URI)
|
|
104
|
+
* const type = factory.credentialType; // e.g., 'AccountV1'
|
|
105
|
+
*
|
|
106
|
+
* // Get JSON-LD context for embedding
|
|
107
|
+
* const context = factory.context;
|
|
108
|
+
* // {
|
|
109
|
+
* // AccountV1: { '@id': 'https://example.com/schema/AccountV1' },
|
|
110
|
+
* // Account: { '@id': '...', '@context': {...} }
|
|
111
|
+
* // }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare class CredentialFactory {
|
|
115
|
+
private _uri;
|
|
116
|
+
private _types;
|
|
117
|
+
private _context;
|
|
118
|
+
private _validator;
|
|
119
|
+
/**
|
|
120
|
+
* Creates a new CredentialFactory instance.
|
|
121
|
+
*
|
|
122
|
+
* **Intent:** Initialize a factory with credential schemas and URI, enabling credential
|
|
123
|
+
* generation with automatic validation and JSON-LD context generation.
|
|
124
|
+
*
|
|
125
|
+
* **Use Cases:**
|
|
126
|
+
* - Set up credential factories for different credential types
|
|
127
|
+
* - Configure factories with single or multiple related schemas
|
|
128
|
+
* - Prepare factories for batch credential generation
|
|
129
|
+
*
|
|
130
|
+
* **Behavior:**
|
|
131
|
+
* - Validates that the provided URI is a valid URL
|
|
132
|
+
* - Processes schemas: if a schema doesn't have a URL, creates a new Schema instance
|
|
133
|
+
* with the factory URI as the base URL
|
|
134
|
+
* - Validates all schemas using the Validator
|
|
135
|
+
* - Builds JSON-LD context from schema linked data types
|
|
136
|
+
*
|
|
137
|
+
* @param uri - The base URI for the credential type (e.g., 'https://example.com/schema/AccountV1')
|
|
138
|
+
* Must be a valid URL. The last segment will be used as the credential type name.
|
|
139
|
+
* @param schemas - Array of Schema instances defining the credential subject structure.
|
|
140
|
+
* The first schema is considered the root schema for validation.
|
|
141
|
+
* Schemas can reference each other using `$ref`.
|
|
142
|
+
*
|
|
143
|
+
* @throws Error if the URI is not a valid URL
|
|
144
|
+
* @throws Error if schema validation fails (e.g., invalid JSON Schema structure)
|
|
145
|
+
*
|
|
146
|
+
* **Example:**
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const accountSchema = new Schema({
|
|
149
|
+
* username: { required: true },
|
|
150
|
+
* email: { format: 'email', required: true }
|
|
151
|
+
* }, 'Account');
|
|
152
|
+
*
|
|
153
|
+
* const factory = new CredentialFactory(
|
|
154
|
+
* 'https://example.com/schema/AccountV1',
|
|
155
|
+
* [accountSchema]
|
|
156
|
+
* );
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
constructor(uri: string, schemas: Schema[]);
|
|
160
|
+
/**
|
|
161
|
+
* Returns the credential type name extracted from the factory URI.
|
|
162
|
+
*
|
|
163
|
+
* **Intent:** Provide a convenient way to access the credential type identifier
|
|
164
|
+
* without manually parsing the URI.
|
|
165
|
+
*
|
|
166
|
+
* **Use Cases:**
|
|
167
|
+
* - Access credential type for logging or debugging
|
|
168
|
+
* - Use type name in credential processing logic
|
|
169
|
+
* - Generate type-specific identifiers or filenames
|
|
170
|
+
*
|
|
171
|
+
* **Behavior:**
|
|
172
|
+
* - Extracts the last segment from the URI path
|
|
173
|
+
* - Example: 'https://example.com/schema/AccountV1' → 'AccountV1'
|
|
174
|
+
*
|
|
175
|
+
* @returns The credential type name (last segment of the URI path)
|
|
176
|
+
*
|
|
177
|
+
* **Example:**
|
|
178
|
+
* ```typescript
|
|
179
|
+
* const factory = new CredentialFactory(
|
|
180
|
+
* 'https://example.com/schema/AccountV1',
|
|
181
|
+
* [schema]
|
|
182
|
+
* );
|
|
183
|
+
*
|
|
184
|
+
* console.log(factory.credentialType); // 'AccountV1'
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
get credentialType(): string;
|
|
188
|
+
/**
|
|
189
|
+
* Returns the JSON-LD context object for the credential.
|
|
190
|
+
*
|
|
191
|
+
* **Intent:** Provide the complete JSON-LD context mapping that should be included
|
|
192
|
+
* in the credential's `@context` array for semantic web compatibility.
|
|
193
|
+
*
|
|
194
|
+
* **Use Cases:**
|
|
195
|
+
* - Access context for credential serialization
|
|
196
|
+
* - Inspect context mappings for debugging
|
|
197
|
+
* - Use context in custom credential processing
|
|
198
|
+
* - Embed context in other JSON-LD documents
|
|
199
|
+
*
|
|
200
|
+
* **Structure:**
|
|
201
|
+
* - Contains the credential type mapping (`{ credentialType: { '@id': uri } }`)
|
|
202
|
+
* - Includes all schema-linked data types from the factory's schemas
|
|
203
|
+
* - Each schema's `linkedDataType` is included if the schema was created with a URL
|
|
204
|
+
*
|
|
205
|
+
* @returns Object containing credential type and schema type mappings for JSON-LD context
|
|
206
|
+
*
|
|
207
|
+
* **Example:**
|
|
208
|
+
* ```typescript
|
|
209
|
+
* const factory = new CredentialFactory(uri, schemas);
|
|
210
|
+
* const context = factory.context;
|
|
211
|
+
*
|
|
212
|
+
* // Result structure:
|
|
213
|
+
* // {
|
|
214
|
+
* // AccountV1: { '@id': 'https://example.com/schema/AccountV1' },
|
|
215
|
+
* // Account: {
|
|
216
|
+
* // '@id': 'https://example.com/schema/AccountV1#Account',
|
|
217
|
+
* // '@context': { ... }
|
|
218
|
+
* // }
|
|
219
|
+
* // }
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
get context(): {
|
|
223
|
+
[x: string]: LinkedDataType | {
|
|
224
|
+
'@id': string;
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Creates a Verifiable Credential for the specified subject.
|
|
229
|
+
*
|
|
230
|
+
* **Intent:** Generate a W3C-compliant Verifiable Credential with validated subject data,
|
|
231
|
+
* proper JSON-LD context, and standardized structure ready for issuance and verification.
|
|
232
|
+
*
|
|
233
|
+
* **Use Cases:**
|
|
234
|
+
* - Issue credentials for user accounts, achievements, certifications
|
|
235
|
+
* - Generate credentials with automatic schema validation
|
|
236
|
+
* - Create credentials with embedded semantic context for interoperability
|
|
237
|
+
* - Build credentials for decentralized identity systems
|
|
238
|
+
* - Generate credentials that can be verified using JSON-LD processors
|
|
239
|
+
*
|
|
240
|
+
* **Behavior:**
|
|
241
|
+
* - Validates that `id` and `holder` are valid URLs/URIs
|
|
242
|
+
* - Validates the subject against the root schema (first schema in the factory)
|
|
243
|
+
* - Applies schema defaults and normalizes data according to schema rules
|
|
244
|
+
* - Generates credential with proper `@context`, `type`, `id`, `holder`, and `credentialSubject`
|
|
245
|
+
* - Returns credential ready for signing (issuer, issuanceDate, proof to be added separately)
|
|
246
|
+
*
|
|
247
|
+
* **Credential Structure:**
|
|
248
|
+
* ```typescript
|
|
249
|
+
* {
|
|
250
|
+
* '@context': [
|
|
251
|
+
* 'https://www.w3.org/2018/credentials/v1',
|
|
252
|
+
* factory.context // JSON-LD context from factory
|
|
253
|
+
* ],
|
|
254
|
+
* id: string, // Credential identifier
|
|
255
|
+
* type: ['VerifiableCredential', credentialType],
|
|
256
|
+
* holder: string, // DID or identifier of credential holder
|
|
257
|
+
* credentialSubject: { // Validated and normalized subject data
|
|
258
|
+
* id: string,
|
|
259
|
+
* type: string, // Root schema ID
|
|
260
|
+
* // ... additional subject properties
|
|
261
|
+
* }
|
|
262
|
+
* }
|
|
263
|
+
* ```
|
|
264
|
+
*
|
|
265
|
+
* @param id - Unique identifier for the credential (must be a valid URL/URI)
|
|
266
|
+
* @param holder - DID or identifier of the credential holder (must be a valid URL/URI)
|
|
267
|
+
* @param subject - Object containing the credential subject data to be validated against
|
|
268
|
+
* the root schema. Properties are validated, defaults are applied,
|
|
269
|
+
* and nested objects are validated against referenced schemas.
|
|
270
|
+
*
|
|
271
|
+
* @returns Verifiable Credential object with validated subject and JSON-LD context
|
|
272
|
+
*
|
|
273
|
+
* @throws Error if `id` is not a valid URL/URI
|
|
274
|
+
* @throws Error if `holder` is not a valid URL/URI
|
|
275
|
+
* @throws ValidationError if subject data doesn't match the schema requirements
|
|
276
|
+
*
|
|
277
|
+
* **Example - Basic Credential:**
|
|
278
|
+
* ```typescript
|
|
279
|
+
* const factory = new CredentialFactory(uri, [accountSchema]);
|
|
280
|
+
*
|
|
281
|
+
* const credential = factory.createCredential(
|
|
282
|
+
* 'https://example.com/credentials/123',
|
|
283
|
+
* 'did:holder:456',
|
|
284
|
+
* {
|
|
285
|
+
* id: 'did:holder:456',
|
|
286
|
+
* username: 'alice',
|
|
287
|
+
* createdAt: '2024-01-01T00:00:00Z'
|
|
288
|
+
* }
|
|
289
|
+
* );
|
|
290
|
+
*
|
|
291
|
+
* // Result:
|
|
292
|
+
* // {
|
|
293
|
+
* // '@context': ['https://www.w3.org/2018/credentials/v1', {...}],
|
|
294
|
+
* // id: 'https://example.com/credentials/123',
|
|
295
|
+
* // type: ['VerifiableCredential', 'AccountV1'],
|
|
296
|
+
* // holder: 'did:holder:456',
|
|
297
|
+
* // credentialSubject: {
|
|
298
|
+
* // id: 'did:holder:456',
|
|
299
|
+
* // username: 'alice',
|
|
300
|
+
* // createdAt: '2024-01-01T00:00:00Z',
|
|
301
|
+
* // type: 'Account'
|
|
302
|
+
* // }
|
|
303
|
+
* // }
|
|
304
|
+
* ```
|
|
305
|
+
*
|
|
306
|
+
* **Example - Credential with Nested Objects:**
|
|
307
|
+
* ```typescript
|
|
308
|
+
* const factory = new CredentialFactory(uri, [playerSchema, gameSchema, scoreSchema]);
|
|
309
|
+
*
|
|
310
|
+
* const credential = factory.createCredential(
|
|
311
|
+
* 'https://example.com/credentials/score-789',
|
|
312
|
+
* 'did:player:123',
|
|
313
|
+
* {
|
|
314
|
+
* id: 'did:player:123',
|
|
315
|
+
* hasVideoGameScore: {
|
|
316
|
+
* game: {
|
|
317
|
+
* id: 'did:game:001',
|
|
318
|
+
* name: 'MineSweeper',
|
|
319
|
+
* version: '1.0'
|
|
320
|
+
* },
|
|
321
|
+
* wins: 10,
|
|
322
|
+
* bestScore: 5000
|
|
323
|
+
* }
|
|
324
|
+
* }
|
|
325
|
+
* );
|
|
326
|
+
* ```
|
|
327
|
+
*
|
|
328
|
+
* **Note:** The returned credential does not include `issuer`, `issuanceDate`, or `proof`
|
|
329
|
+
* fields. These should be added by the credential issuance system (e.g., using
|
|
330
|
+
* [@kravc/identity](http://github.com/alexkravets/identity) or similar libraries).
|
|
331
|
+
*/
|
|
332
|
+
createCredential(id: string, holder: string, subject?: TargetObject): {
|
|
333
|
+
'@context': (string | {
|
|
334
|
+
[x: string]: LinkedDataType | {
|
|
335
|
+
'@id': string;
|
|
336
|
+
};
|
|
337
|
+
})[];
|
|
338
|
+
id: string;
|
|
339
|
+
type: string[];
|
|
340
|
+
holder: string;
|
|
341
|
+
credentialSubject: any;
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
export default CredentialFactory;
|
|
345
|
+
//# sourceMappingURL=CredentialFactory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CredentialFactory.d.ts","sourceRoot":"","sources":["../src/CredentialFactory.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,MAAM,EAAE,EAAE,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AAIvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8GG;AACH,cAAM,iBAAiB;IACrB,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,QAAQ,CAAiC;IACjD,OAAO,CAAC,UAAU,CAAY;IAE9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuCG;gBACS,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE;IAqB1C;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACH,IAAI,cAAc,WAIjB;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,IAAI,OAAO;;;;MAKV;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAwGG;IACH,gBAAgB,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,YAAiB;;;;;;;;;;;CAuBxE;AAED,eAAe,iBAAiB,CAAC"}
|