@carrot-foundation/schemas 0.2.4 โ†’ 0.3.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.
Files changed (27) hide show
  1. package/README.md +144 -116
  2. package/dist/index.cjs +115 -113
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +7 -7
  5. package/dist/index.d.ts +7 -7
  6. package/dist/index.js +115 -113
  7. package/dist/index.js.map +1 -1
  8. package/package.json +12 -12
  9. package/schemas/ipfs/collection/collection.example.json +3 -3
  10. package/schemas/ipfs/collection/collection.schema.json +7 -7
  11. package/schemas/ipfs/credit/credit.example.json +4 -4
  12. package/schemas/ipfs/credit/credit.schema.json +6 -6
  13. package/schemas/ipfs/credit-purchase-receipt/credit-purchase-receipt.example.json +15 -15
  14. package/schemas/ipfs/credit-purchase-receipt/credit-purchase-receipt.schema.json +56 -56
  15. package/schemas/ipfs/credit-retirement-receipt/credit-retirement-receipt.example.json +15 -15
  16. package/schemas/ipfs/credit-retirement-receipt/credit-retirement-receipt.schema.json +54 -54
  17. package/schemas/ipfs/gas-id/gas-id.example.json +9 -9
  18. package/schemas/ipfs/gas-id/gas-id.schema.json +49 -41
  19. package/schemas/ipfs/mass-id/mass-id.example.json +7 -7
  20. package/schemas/ipfs/mass-id/mass-id.schema.json +60 -43
  21. package/schemas/ipfs/mass-id-audit/mass-id-audit.example.json +57 -57
  22. package/schemas/ipfs/mass-id-audit/mass-id-audit.schema.json +14 -14
  23. package/schemas/ipfs/methodology/methodology.example.json +5 -5
  24. package/schemas/ipfs/methodology/methodology.schema.json +7 -7
  25. package/schemas/ipfs/recycled-id/recycled-id.example.json +9 -9
  26. package/schemas/ipfs/recycled-id/recycled-id.schema.json +43 -35
  27. package/schemas/schema-hashes.json +10 -10
package/README.md CHANGED
@@ -1,172 +1,200 @@
1
- # ๐Ÿฅ• Carrot IPFS Schemas
1
+ # @carrot-foundation/schemas
2
2
 
3
- This repository contains all JSON Schemas used across the Carrot ecosystem.
3
+ [![npm version](https://img.shields.io/npm/v/@carrot-foundation/schemas)](https://www.npmjs.com/package/@carrot-foundation/schemas)
4
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](./LICENSE)
4
5
 
5
- These schemas define the structure of IPFS metadata for tokenized assets and other schemas.
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
- They are versioned, publicly referenceable, and used for validation, traceability, and frontend/backend integration.
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
- # Build with package.json version as default
31
- pnpm build
32
- pnpm generate-ipfs-schemas
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
- ### Release Process
16
+ > Requires Node.js >= 22
47
17
 
48
- The release process automatically:
18
+ ## Available Schemas
49
19
 
50
- 1. Calculates the next version based on conventional commits
51
- 2. Builds the package with the new version embedded
52
- 3. Generates JSON schemas with versioned `$id` references
53
- 4. Creates a Git tag
54
- 5. Publishes to npm
55
- 6. Creates a GitHub release
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
- All of this happens automatically via the GitHub Actions workflow when you push to `main`.
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
- ### Example Schema References
34
+ ## Usage
60
35
 
61
- **Development (uses package.json version by default):**
36
+ ### Validation
62
37
 
63
- ```json
64
- "$id": "https://raw.githubusercontent.com/carrot-foundation/schemas/refs/tags/0.1.28/schemas/ipfs/mass-id/mass-id.schema.json"
65
- ```
38
+ ```typescript
39
+ import { MassIDIpfsSchema } from '@carrot-foundation/schemas';
66
40
 
67
- **Production (versioned via SCHEMA_VERSION env var):**
41
+ const result = MassIDIpfsSchema.safeParse(data);
68
42
 
69
- ```json
70
- "$id": "https://raw.githubusercontent.com/carrot-foundation/schemas/refs/tags/1.2.3/schemas/ipfs/mass-id/mass-id.schema.json"
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
- ## ๐Ÿ“š Package Usage
50
+ ### TypeScript Types
74
51
 
75
- This package is published to npm as `@carrot-foundation/schemas` and supports both **ESM** and **CommonJS** module formats.
52
+ ```typescript
53
+ import type { MassIDIpfs, Credit } from '@carrot-foundation/schemas';
76
54
 
77
- ### Installation
55
+ // NFT-based schemas include blockchain, attributes, and data
56
+ function processMassID(record: MassIDIpfs) {
57
+ const { data, blockchain, attributes } = record;
58
+ }
78
59
 
79
- ```bash
80
- npm install @carrot-foundation/schemas
81
- # or
82
- pnpm add @carrot-foundation/schemas
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
- ### ESM (ECMAScript Modules)
66
+ ### Shared Primitives
88
67
 
89
- ```typescript
90
- import { MassIDIpfsSchema, MassIDDataSchema } from '@carrot-foundation/schemas';
68
+ The package exports reusable building blocks for custom schemas:
91
69
 
92
- // Use the schemas for validation
93
- const result = MassIDIpfsSchema.safeParse(data);
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
- ### TypeScript Support
90
+ ### JSON Schema References
109
91
 
110
- The package includes full TypeScript definitions:
92
+ Generated JSON Schemas are available at stable URLs and included in the npm package under `schemas/`:
111
93
 
112
- ```typescript
113
- import { MassIDIpfs, MassIDData } from '@carrot-foundation/schemas';
94
+ ```text
95
+ https://raw.githubusercontent.com/carrot-foundation/schemas/refs/tags/{version}/schemas/ipfs/{type}/{type}.schema.json
96
+ ```
114
97
 
115
- // Types are automatically inferred from the schemas
116
- const massIDData: MassIDData = {
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
- ## ๐Ÿงช Testing
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
- This repository uses [Vitest](https://vitest.dev/) for automated testing. Tests validate that schemas correctly validate example data and reject invalid inputs.
109
+ ## Architecture
124
110
 
125
- ### Running Tests
111
+ Schemas follow a layered composition pattern using Zod's `.safeExtend()`:
126
112
 
127
- ```bash
128
- # Run all tests once
129
- pnpm test
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
- # Run tests in watch mode
132
- pnpm test:watch
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
- # Run tests with coverage report
135
- pnpm test:coverage
154
+ Schema `$id` fields reference Git tags for stable, immutable URLs:
136
155
 
137
- # Open Vitest UI for interactive testing
138
- pnpm test:ui
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
- ### Test Structure
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
- Tests are located alongside their schemas in `src/**/*.spec.ts` files. Each test file validates:
164
+ ### Release Process
144
165
 
145
- - Example JSON files pass schema validation
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
- ### Coverage
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
- Coverage thresholds are set at **100%** for branches, functions, lines, and statements.
173
+ ## Development
153
174
 
154
- ## โœ… Usage
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
- You may:
183
+ ### Quality Gates (`pnpm check`)
157
184
 
158
- - Reference any schema via its `$id` (e.g. in IPFS metadata)
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
- You may not:
187
+ ### Testing
163
188
 
164
- - Redistribute, rebrand, or fork these schemas for other ecosystems
165
- - Create derivative schemas that use Carrot's identity
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
- See [NOTICE](./NOTICE) for full usage guidance.
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
- ## ๐Ÿ” License
198
+ ## License
170
199
 
171
- Licensed under the [Apache License 2.0](./LICENSE).
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.