@carrot-foundation/schemas 0.2.3 โ 0.3.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 +144 -116
- package/dist/index.cjs +204 -267
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -23
- package/dist/index.d.ts +23 -23
- package/dist/index.js +204 -267
- package/dist/index.js.map +1 -1
- package/package.json +20 -20
- package/schemas/ipfs/collection/collection.example.json +3 -3
- package/schemas/ipfs/collection/collection.schema.json +77 -77
- package/schemas/ipfs/credit/credit.example.json +4 -4
- package/schemas/ipfs/credit/credit.schema.json +85 -85
- package/schemas/ipfs/credit-purchase-receipt/credit-purchase-receipt.example.json +15 -15
- package/schemas/ipfs/credit-purchase-receipt/credit-purchase-receipt.schema.json +432 -424
- package/schemas/ipfs/credit-retirement-receipt/credit-retirement-receipt.example.json +15 -15
- package/schemas/ipfs/credit-retirement-receipt/credit-retirement-receipt.schema.json +465 -457
- package/schemas/ipfs/gas-id/gas-id.example.json +9 -9
- package/schemas/ipfs/gas-id/gas-id.schema.json +448 -432
- package/schemas/ipfs/mass-id/mass-id.example.json +7 -7
- package/schemas/ipfs/mass-id/mass-id.schema.json +533 -512
- package/schemas/ipfs/mass-id-audit/mass-id-audit.example.json +57 -57
- package/schemas/ipfs/mass-id-audit/mass-id-audit.schema.json +225 -217
- package/schemas/ipfs/methodology/methodology.example.json +5 -5
- package/schemas/ipfs/methodology/methodology.schema.json +124 -124
- package/schemas/ipfs/recycled-id/recycled-id.example.json +9 -9
- package/schemas/ipfs/recycled-id/recycled-id.schema.json +400 -384
- package/schemas/schema-hashes.json +10 -10
package/README.md
CHANGED
|
@@ -1,172 +1,200 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @carrot-foundation/schemas
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@carrot-foundation/schemas)
|
|
4
|
+
[](./LICENSE)
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
Zod validation schemas and generated JSON Schemas for IPFS metadata in the [Carrot](https://carrot.eco) ecosystem. Supports ESM and CJS.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## ๐ฆ Structure
|
|
10
|
-
|
|
11
|
-
- Schemas are organized by type (e.g., `mass-id`, `gas-id`, `shared`) and follow [Semantic Versioning](https://semver.org/), but folder names are not versioned.
|
|
12
|
-
- Each schema lives in its own folder and includes an `example.json` for testing and documentation.
|
|
13
|
-
- Shared components are located in `src/shared/**`.
|
|
14
|
-
|
|
15
|
-
## ๐ Versioning
|
|
16
|
-
|
|
17
|
-
This project uses automated schema reference versioning with a **unified versioning approach** for both development and production environments. All schema `$id` fields always reference Git tags, ensuring consistency across all environments.
|
|
18
|
-
|
|
19
|
-
### How It Works
|
|
20
|
-
|
|
21
|
-
- **All builds**: Schemas reference `refs/tags/{version}` where version is set via `SCHEMA_VERSION` environment variable
|
|
22
|
-
- **Default version**: If `SCHEMA_VERSION` is not set, defaults to the version from `package.json`
|
|
23
|
-
- **No special cases**: Development and production use the exact same versioning mechanism
|
|
24
|
-
|
|
25
|
-
### Local Development
|
|
26
|
-
|
|
27
|
-
For local development, the version from `package.json` is used automatically:
|
|
8
|
+
## Installation
|
|
28
9
|
|
|
29
10
|
```bash
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
### Development Tag Management
|
|
36
|
-
|
|
37
|
-
To test a versioned build locally:
|
|
38
|
-
|
|
39
|
-
```bash
|
|
40
|
-
# Build with a specific version
|
|
41
|
-
SCHEMA_VERSION=1.2.3 pnpm build
|
|
42
|
-
SCHEMA_VERSION=1.2.3 pnpm generate-ipfs-schemas
|
|
43
|
-
SCHEMA_VERSION=1.2.3 pnpm verify-schema-versions
|
|
11
|
+
pnpm add @carrot-foundation/schemas
|
|
12
|
+
# or
|
|
13
|
+
npm install @carrot-foundation/schemas
|
|
44
14
|
```
|
|
45
15
|
|
|
46
|
-
|
|
16
|
+
> Requires Node.js >= 22
|
|
47
17
|
|
|
48
|
-
|
|
18
|
+
## Available Schemas
|
|
49
19
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
20
|
+
| Category | Schema | Description |
|
|
21
|
+
| ------------- | ----------------------------------- | -------------------------------------------------- |
|
|
22
|
+
| **Assets** | `MassIDIpfsSchema` | Waste tracking tokens with chain of custody events |
|
|
23
|
+
| | `GasIDIpfsSchema` | Gas/emissions reduction tokens |
|
|
24
|
+
| | `RecycledIDIpfsSchema` | Recycled material tokens |
|
|
25
|
+
| | `CreditSchema` | Environmental credit tokens |
|
|
26
|
+
| | `CollectionSchema` | Collection metadata for grouped assets |
|
|
27
|
+
| **Receipts** | `CreditPurchaseReceiptIpfsSchema` | Proof of credit purchase transactions |
|
|
28
|
+
| | `CreditRetirementReceiptIpfsSchema` | Proof of credit retirement transactions |
|
|
29
|
+
| **Audit** | `MassIDAuditSchema` | Audit trail records for MassID tokens |
|
|
30
|
+
| **Reference** | `MethodologySchema` | Methodology definitions and rules |
|
|
56
31
|
|
|
57
|
-
|
|
32
|
+
Asset, receipt, audit, and reference schema types also export their data schema where applicable (e.g., `MassIDDataSchema`) and inferred TypeScript types (e.g., `MassIDIpfs`, `MassIDData`).
|
|
58
33
|
|
|
59
|
-
|
|
34
|
+
## Usage
|
|
60
35
|
|
|
61
|
-
|
|
36
|
+
### Validation
|
|
62
37
|
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
```
|
|
38
|
+
```typescript
|
|
39
|
+
import { MassIDIpfsSchema } from '@carrot-foundation/schemas';
|
|
66
40
|
|
|
67
|
-
|
|
41
|
+
const result = MassIDIpfsSchema.safeParse(data);
|
|
68
42
|
|
|
69
|
-
|
|
70
|
-
|
|
43
|
+
if (result.success) {
|
|
44
|
+
console.log('Valid MassID record:', result.data);
|
|
45
|
+
} else {
|
|
46
|
+
console.error('Validation errors:', result.error.issues);
|
|
47
|
+
}
|
|
71
48
|
```
|
|
72
49
|
|
|
73
|
-
|
|
50
|
+
### TypeScript Types
|
|
74
51
|
|
|
75
|
-
|
|
52
|
+
```typescript
|
|
53
|
+
import type { MassIDIpfs, Credit } from '@carrot-foundation/schemas';
|
|
76
54
|
|
|
77
|
-
|
|
55
|
+
// NFT-based schemas include blockchain, attributes, and data
|
|
56
|
+
function processMassID(record: MassIDIpfs) {
|
|
57
|
+
const { data, blockchain, attributes } = record;
|
|
58
|
+
}
|
|
78
59
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
# or
|
|
84
|
-
yarn add @carrot-foundation/schemas
|
|
60
|
+
// Non-NFT schemas extend BaseIpfsSchema directly
|
|
61
|
+
function processCredit(record: Credit) {
|
|
62
|
+
const { schema, created_at, external_id } = record;
|
|
63
|
+
}
|
|
85
64
|
```
|
|
86
65
|
|
|
87
|
-
###
|
|
66
|
+
### Shared Primitives
|
|
88
67
|
|
|
89
|
-
|
|
90
|
-
import { MassIDIpfsSchema, MassIDDataSchema } from '@carrot-foundation/schemas';
|
|
68
|
+
The package exports reusable building blocks for custom schemas:
|
|
91
69
|
|
|
92
|
-
|
|
93
|
-
|
|
70
|
+
```typescript
|
|
71
|
+
import {
|
|
72
|
+
IsoDateTimeSchema, // ISO 8601 date-time strings
|
|
73
|
+
Sha256HashSchema, // SHA-256 hash validation
|
|
74
|
+
IpfsUriSchema, // IPFS URI format (ipfs://...)
|
|
75
|
+
SemanticVersionSchema, // Semver strings
|
|
76
|
+
UuidSchema, // UUID v4
|
|
77
|
+
LatitudeSchema, // Geographic coordinates
|
|
78
|
+
LongitudeSchema,
|
|
79
|
+
ParticipantSchema, // Stakeholder entities
|
|
80
|
+
LocationSchema, // Geographic locations
|
|
81
|
+
} from '@carrot-foundation/schemas';
|
|
94
82
|
```
|
|
95
83
|
|
|
96
84
|
### CommonJS
|
|
97
85
|
|
|
98
86
|
```javascript
|
|
99
|
-
const {
|
|
100
|
-
MassIDIpfsSchema,
|
|
101
|
-
MassIDDataSchema,
|
|
102
|
-
} = require('@carrot-foundation/schemas');
|
|
103
|
-
|
|
104
|
-
// Use the schemas for validation
|
|
105
|
-
const result = MassIDIpfsSchema.safeParse(data);
|
|
87
|
+
const { MassIDIpfsSchema } = require('@carrot-foundation/schemas');
|
|
106
88
|
```
|
|
107
89
|
|
|
108
|
-
###
|
|
90
|
+
### JSON Schema References
|
|
109
91
|
|
|
110
|
-
|
|
92
|
+
Generated JSON Schemas are available at stable URLs and included in the npm package under `schemas/`:
|
|
111
93
|
|
|
112
|
-
```
|
|
113
|
-
|
|
94
|
+
```text
|
|
95
|
+
https://raw.githubusercontent.com/carrot-foundation/schemas/refs/tags/{version}/schemas/ipfs/{type}/{type}.schema.json
|
|
96
|
+
```
|
|
114
97
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
98
|
+
Example JSON data for each schema is also available:
|
|
99
|
+
|
|
100
|
+
```text
|
|
101
|
+
schemas/ipfs/mass-id/mass-id.schema.json
|
|
102
|
+
schemas/ipfs/mass-id/mass-id.example.json
|
|
119
103
|
```
|
|
120
104
|
|
|
121
|
-
|
|
105
|
+
Published examples are generated from the shared reference story and then
|
|
106
|
+
post-processed with schema/version/hash metadata. Edit the reference story
|
|
107
|
+
or emitter modules instead of hand-editing emitted example JSON.
|
|
122
108
|
|
|
123
|
-
|
|
109
|
+
## Architecture
|
|
124
110
|
|
|
125
|
-
|
|
111
|
+
Schemas follow a layered composition pattern using Zod's `.safeExtend()`:
|
|
126
112
|
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
|
|
113
|
+
```text
|
|
114
|
+
BaseIpfsSchema Common IPFS record fields ($schema, created_at, external_id, ...)
|
|
115
|
+
โโ NftIpfsSchema NFT fields (blockchain, name, description, image, attributes)
|
|
116
|
+
โ โโ Asset/Receipt MassIDIpfsSchema, GasIDIpfsSchema, RecycledIDIpfsSchema,
|
|
117
|
+
โ Schemas CreditPurchaseReceiptIpfsSchema, CreditRetirementReceiptIpfsSchema
|
|
118
|
+
โโ Non-NFT Schemas CreditSchema, CollectionSchema, MassIDAuditSchema, MethodologySchema
|
|
119
|
+
```
|
|
130
120
|
|
|
131
|
-
|
|
132
|
-
|
|
121
|
+
### Source Structure
|
|
122
|
+
|
|
123
|
+
```text
|
|
124
|
+
src/
|
|
125
|
+
โโโ mass-id/ # Example: NFT schema type (has all files)
|
|
126
|
+
โ โโโ mass-id.schema.ts # IPFS schema (extends NftIpfsSchema)
|
|
127
|
+
โ โโโ mass-id.data.schema.ts # Type-specific data structure
|
|
128
|
+
โ โโโ mass-id.attributes.ts # NFT display attributes
|
|
129
|
+
โ โโโ index.ts # Public exports
|
|
130
|
+
โ โโโ __tests__/ # Tests (100% coverage enforced)
|
|
131
|
+
โโโ credit/ # Example: non-NFT schema type (simpler structure)
|
|
132
|
+
โ โโโ credit.schema.ts # Schema (extends BaseIpfsSchema directly)
|
|
133
|
+
โ โโโ index.ts
|
|
134
|
+
โ โโโ __tests__/
|
|
135
|
+
โโโ shared/
|
|
136
|
+
โ โโโ schemas/
|
|
137
|
+
โ โ โโโ core/ # BaseIpfsSchema, NftIpfsSchema, attributes
|
|
138
|
+
โ โ โโโ primitives/ # Blockchain, time, geo, IDs, hashes, URIs, enums
|
|
139
|
+
โ โ โโโ entities/ # Participant, Location
|
|
140
|
+
โ โ โโโ references/ # Cross-schema linking (MassID ref, Credit ref, ...)
|
|
141
|
+
โ โ โโโ receipt/ # Receipt base schemas
|
|
142
|
+
โ โ โโโ certificate/ # Certificate schemas
|
|
143
|
+
โ โโโ schema-version.ts # Version management utilities
|
|
144
|
+
โ โโโ schema-helpers.ts # Zod utilities (uniqueArrayItems, uniqueBy)
|
|
145
|
+
โ โโโ schema-validation.ts # Validation helpers
|
|
146
|
+
โ โโโ hash.ts # Canonical hashing (canonicalize + SHA-256)
|
|
147
|
+
โ โโโ data/ # Reference data (Brazil municipalities)
|
|
148
|
+
โโโ test-utils/ # Shared fixtures and test assertions
|
|
149
|
+
โโโ index.ts # Package entry point
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Versioning
|
|
133
153
|
|
|
134
|
-
|
|
135
|
-
pnpm test:coverage
|
|
154
|
+
Schema `$id` fields reference Git tags for stable, immutable URLs:
|
|
136
155
|
|
|
137
|
-
|
|
138
|
-
|
|
156
|
+
```json
|
|
157
|
+
"$id": "https://raw.githubusercontent.com/carrot-foundation/schemas/refs/tags/0.2.4/schemas/ipfs/mass-id/mass-id.schema.json"
|
|
139
158
|
```
|
|
140
159
|
|
|
141
|
-
|
|
160
|
+
- **Default**: version from `package.json`
|
|
161
|
+
- **CI/Release**: set via `SCHEMA_VERSION` environment variable
|
|
162
|
+
- **Local override**: `SCHEMA_VERSION=1.2.3 pnpm build`
|
|
142
163
|
|
|
143
|
-
|
|
164
|
+
### Release Process
|
|
144
165
|
|
|
145
|
-
|
|
146
|
-
- Invalid data is properly rejected
|
|
147
|
-
- Type inference works correctly
|
|
148
|
-
- Edge cases are handled appropriately
|
|
166
|
+
Automated via GitHub Actions on push to `main`:
|
|
149
167
|
|
|
150
|
-
|
|
168
|
+
1. Determines version bump from conventional commits
|
|
169
|
+
2. Builds the package with the version embedded in schema `$id` URLs
|
|
170
|
+
3. Generates JSON Schemas, hashes, and examples
|
|
171
|
+
4. Creates a Git tag, publishes to npm, creates a GitHub release
|
|
151
172
|
|
|
152
|
-
|
|
173
|
+
## Development
|
|
153
174
|
|
|
154
|
-
|
|
175
|
+
```bash
|
|
176
|
+
pnpm install # Install dependencies
|
|
177
|
+
pnpm build # TypeScript -> dist/ (ESM + CJS)
|
|
178
|
+
pnpm test # Run all tests (100% coverage enforced)
|
|
179
|
+
pnpm generate-ipfs-schemas # Generate JSON Schemas from Zod
|
|
180
|
+
pnpm check # Run ALL quality gates
|
|
181
|
+
```
|
|
155
182
|
|
|
156
|
-
|
|
183
|
+
### Quality Gates (`pnpm check`)
|
|
157
184
|
|
|
158
|
-
-
|
|
159
|
-
- Validate metadata files using these schemas
|
|
160
|
-
- Link to them from applications, dashboards, or traceability tools
|
|
185
|
+
Lint, format, type-check, spell-check, schema generation, hash verification, example updates, schema validation, version verification, reference checks, example validation, tests, dead code detection, license check, bundle size, publint, and type export verification.
|
|
161
186
|
|
|
162
|
-
|
|
187
|
+
### Testing
|
|
163
188
|
|
|
164
|
-
|
|
165
|
-
|
|
189
|
+
```bash
|
|
190
|
+
pnpm test # Run once
|
|
191
|
+
pnpm test:watch # Watch mode
|
|
192
|
+
pnpm test:coverage # Coverage report
|
|
193
|
+
pnpm test:ui # Vitest UI
|
|
194
|
+
```
|
|
166
195
|
|
|
167
|
-
|
|
196
|
+
Tests validate schema acceptance of valid data, rejection of invalid data, type inference, and cross-field refinements. Coverage thresholds: **100%** for branches, functions, lines, and statements.
|
|
168
197
|
|
|
169
|
-
##
|
|
198
|
+
## License
|
|
170
199
|
|
|
171
|
-
|
|
172
|
-
See [NOTICE](./NOTICE) for additional terms and usage intentions.
|
|
200
|
+
[Apache License 2.0](./LICENSE). For public reference and validation only. See [NOTICE](./NOTICE) for usage restrictions.
|