@carrot-foundation/schemas

中文文档
6.1.1发布于 2026/11/06Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 1735

Carrot schema definitions and zod validation schemas

原始使用文档
文档目录16 节
  1. Installation
  2. Available Schemas
  3. Usage
  4. Validation
  5. TypeScript Types
  6. Shared Primitives
  7. CommonJS
  8. JSON Schema References
  9. Architecture
  10. Source Structure
  11. Versioning
  12. Release Process
  13. Development
  14. Quality Gates ( pnpm check )
  15. Testing
  16. License

@carrot-foundation/schemas

npm version License

Zod validation schemas and generated JSON Schemas for IPFS metadata in the Carrot ecosystem. Supports ESM and CJS.

Installation

pnpm add @carrot-foundation/schemas
# or
npm install @carrot-foundation/schemas

Requires Node.js >= 22

Available Schemas

CategorySchemaDescription
AssetsMassIDIpfsSchemaWaste tracking tokens with chain of custody events
GasIDIpfsSchemaGas/emissions reduction tokens
RecycledIDIpfsSchemaRecycled material tokens
CreditSchemaEnvironmental credit tokens
CollectionSchemaCollection metadata for grouped assets
ReceiptsCreditPurchaseReceiptIpfsSchemaProof of credit purchase transactions
CreditRetirementReceiptIpfsSchemaProof of credit retirement transactions
AuditMassIDAuditSchemaAudit trail records for MassID tokens
ReferenceMethodologySchemaMethodology definitions and rules

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).

Usage

Validation

import { MassIDIpfsSchema } from '@carrot-foundation/schemas';

const result = MassIDIpfsSchema.safeParse(data);

if (result.success) {
  console.log('Valid MassID record:', result.data);
} else {
  console.error('Validation errors:', result.error.issues);
}

TypeScript Types

import type { MassIDIpfs, Credit } from '@carrot-foundation/schemas';

// NFT-based schemas include blockchain, attributes, and data
function processMassID(record: MassIDIpfs) {
  const { data, blockchain, attributes } = record;
}

// Non-NFT schemas extend BaseIpfsSchema directly
function processCredit(record: Credit) {
  const { schema, created_at, external_id } = record;
}

Shared Primitives

The package exports reusable building blocks for custom schemas:

import {
  IsoDateTimeSchema, // ISO 8601 date-time strings
  Sha256HashSchema, // SHA-256 hash validation
  IpfsUriSchema, // IPFS URI format (ipfs://...)
  SemanticVersionSchema, // Semver strings
  UuidSchema, // UUID v4
  LatitudeSchema, // Geographic coordinates
  LongitudeSchema,
  ParticipantSchema, // Stakeholder entities
  LocationSchema, // Geographic locations
} from '@carrot-foundation/schemas';

CommonJS

const { MassIDIpfsSchema } = require('@carrot-foundation/schemas');

JSON Schema References

Generated JSON Schemas are available at stable URLs and included in the npm package under schemas/:

https://raw.githubusercontent.com/carrot-foundation/schemas/refs/tags/v{version}/schemas/ipfs/{type}/{type}.schema.json

Example JSON data for each schema is also available:

schemas/ipfs/mass-id/mass-id.schema.json
schemas/ipfs/mass-id/mass-id.example.json

Published examples are generated from the shared reference story and then post-processed with schema/version/hash metadata. Edit the reference story or emitter modules instead of hand-editing emitted example JSON.

Architecture

Schemas follow a layered composition pattern using Zod's .safeExtend():

BaseIpfsSchema            Common IPFS record fields ($schema, created_at, external_id, ...)
├─ NftIpfsSchema          NFT fields (blockchain, name, description, image, attributes)
│    └─ Asset/Receipt     MassIDIpfsSchema, GasIDIpfsSchema, RecycledIDIpfsSchema,
│         Schemas         CreditPurchaseReceiptIpfsSchema, CreditRetirementReceiptIpfsSchema
└─ Non-NFT Schemas        CreditSchema, CollectionSchema, MassIDAuditSchema, MethodologySchema

Source Structure

src/
├── mass-id/                    # Example: NFT schema type (has all files)
│   ├── mass-id.schema.ts       #   IPFS schema (extends NftIpfsSchema)
│   ├── mass-id.data.schema.ts  #   Type-specific data structure
│   ├── mass-id.attributes.ts   #   NFT display attributes
│   ├── index.ts                #   Public exports
│   └── __tests__/              #   Tests (100% coverage enforced)
├── credit/                     # Example: non-NFT schema type (simpler structure)
│   ├── credit.schema.ts        #   Schema (extends BaseIpfsSchema directly)
│   ├── index.ts
│   └── __tests__/
├── shared/
│   ├── schemas/
│   │   ├── core/               #   BaseIpfsSchema, NftIpfsSchema, attributes
│   │   ├── primitives/         #   Blockchain, time, geo, IDs, hashes, URIs, enums
│   │   ├── entities/           #   Participant, Location
│   │   ├── references/         #   Cross-schema linking (MassID ref, Credit ref, ...)
│   │   ├── receipt/            #   Receipt base schemas
│   │   └── certificate/        #   Certificate schemas
│   ├── schema-version.ts       #   Version management utilities
│   ├── schema-helpers.ts       #   Zod utilities (uniqueArrayItems, uniqueBy)
│   ├── schema-validation.ts    #   Validation helpers
│   ├── hash.ts                 #   Canonical hashing (canonicalize + SHA-256)
│   └── data/                   #   Reference data (Brazil municipalities)
├── test-utils/                 #   Shared fixtures and test assertions
└── index.ts                    #   Package entry point

Versioning

Schema $id fields reference Git tags for stable, immutable URLs:

"$id": "https://raw.githubusercontent.com/carrot-foundation/schemas/refs/tags/v0.2.4/schemas/ipfs/mass-id/mass-id.schema.json"
  • Default: version from package.json
  • CI/Release: set via SCHEMA_VERSION environment variable
  • Local override: SCHEMA_VERSION=1.2.3 pnpm build

Release Process

Automated via GitHub Actions on push to main:

  1. Determines version bump from conventional commits
  2. Builds the package with the version embedded in schema $id URLs
  3. Generates JSON Schemas, hashes, and examples
  4. Creates a Git tag, publishes to npm, creates a GitHub release

Development

pnpm install                      # Install dependencies
pnpm build                        # TypeScript -> dist/ (ESM + CJS)
pnpm test                         # Run all tests (100% coverage enforced)
pnpm generate-ipfs-schemas        # Generate JSON Schemas from Zod
pnpm check                        # Run ALL quality gates

Quality Gates (pnpm check)

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.

Testing

pnpm test                         # Run once
pnpm test:watch                   # Watch mode
pnpm test:coverage                # Coverage report
pnpm test:ui                      # Vitest UI

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.

License

Apache License 2.0. For public reference and validation only. See NOTICE for usage restrictions.

版本与发布记录

@carrot-foundation/schemas 共发布 77 个版本,首次发布于 2025/10/08;过去 12 个月发布了 70 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.1.1latest2026/11/063 天前
6.1.02026/10/061 个月前
6.0.02026/10/031 个月前
5.2.02026/09/092 个月前
5.1.12026/08/272 个月前
5.1.02026/08/252 个月前

关于 @carrot-foundation/schemas 的常见问题

@carrot-foundation/schemas 是什么?

Carrot schema definitions and zod validation schemas

如何安装 @carrot-foundation/schemas?

在项目目录运行 npm install @carrot-foundation/schemas,或使用 pnpm add / yarn add / bun add。

@carrot-foundation/schemas 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@carrot-foundation/schemas 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@carrot-foundation/schemas 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@carrot-foundation/schemas 可以商用吗?

@carrot-foundation/schemas 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@carrot-foundation/schemas 的下载量有多少?

根据 npm 官方下载统计,@carrot-foundation/schemas 在 2026/10/09 – 2026/11/07 这 30 天内被下载 1,735 次,日均约 58 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@carrot-foundation/schemas 还在维护吗?

最新版本 6.1.1 发布于 2026/11/06(3 天前);至今共发布 77 个版本,其中过去 12 个月发布了 70 个;首次发布于 2025/10/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。