用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/carrot-foundation/schemas --skill rule-zod命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
基于 SOC 职业分类
| name | rule-zod |
| description | Zod schema authoring — strictObject, safeExtend, meta(), and composition layers |
Apply this rule whenever work touches:
src/**/*.schema.tssrc/**/*.schemas.tsZod schemas are the single source of truth in this project. They drive TypeScript types, JSON Schema generation, documentation, and runtime validation. Getting them right is critical.
z.strictObject() — alwaysUse z.strictObject() for all object schemas. This rejects unknown properties at parse time, which is essential for IPFS data integrity (extra fields change content hashes).
// BAD: chaining .strict() on z.object()
const LocationSchema = z
.object({
latitude: LatitudeSchema,
longitude: LongitudeSchema,
})
.strict();
// GOOD: strictObject from the start
const LocationSchema = z.strictObject({
latitude: LatitudeSchema,
longitude: LongitudeSchema,
});
.safeExtend()Schemas compose in layers. Use .safeExtend() to build from base to specific:
BaseIpfsSchema (shared $schema, schema, created_at, external_id, external_url, ...)
-> NftIpfsSchema (adds blockchain, name, description, image, attributes, ...)
-> MassIDNftIpfsSchema (adds mass-id-specific data and attributes)
// Base layer — common IPFS record fields
export const BaseIpfsSchema = z.strictObject({
$schema: z.url().meta({ ... }),
schema: SchemaInfoSchema,
created_at: IsoDateTimeSchema.meta({ ... }),
external_id: ExternalIdSchema,
external_url: ExternalUrlSchema,
viewer_reference: ViewerReferenceSchema.optional(),
environment: RecordEnvironmentSchema.optional(),
data: z.record(z.string(), z.unknown()).optional().meta({ ... }),
});
// NFT layer extends base with blockchain and display fields
export const NftIpfsSchema = BaseIpfsSchema.safeExtend({
blockchain: BlockchainReferenceSchema,
name: NonEmptyStringSchema.max(100).meta({ ... }),
short_name: NonEmptyStringSchema.max(50).meta({ ... }),
description: .().({ ... }),
: .({ ... }),
: .({ ... }),
: (, ...).({ ... }),
});
= .({
: .({ ... }),
});
Never use .merge() — it does not preserve strict object behavior. Always use .safeExtend().
.meta() on every fieldEvery field must include .meta() with at minimum title and description. Add examples based on field complexity.
examples (plural array)Use examples by default to show variety — 2 to 4 values is typical:
waste_type: z.string().min(1).max(100).meta({
title: 'Waste Type',
description: 'Category or type of waste material',
examples: ['Organic', 'Plastic', 'Metal', 'Paper'],
}),
example (singular)Use singular example for canonical patterns where one value is sufficient:
$schema: z.url().meta({
title: 'JSON Schema URI',
description: 'URI of the JSON Schema used to validate this record',
example: 'https://raw.githubusercontent.com/carrot-foundation/schemas/...',
}),
Skip examples when a base schema already provides them:
external_id: ExternalIdSchema.meta({
title: 'External ID',
description: 'UUID identifier for external system references',
// No examples needed — ExternalIdSchema (UuidSchema) already has examples
}),
export const MassIDDataSchema = z.strictObject({ ... });
export const LocationSchema = z.strictObject({ ... });
export const ParticipantSchema = z.strictObject({ ... });
export type MassIDData = z.infer<typeof MassIDDataSchema>;
export type Location = z.infer<typeof LocationSchema>;
export type Participant = z.infer<typeof ParticipantSchema>;
.refine()Use .refine() only when built-in validators cannot express the constraint:
// BAD: refine for something built-in
z.string().refine((s) => s.includes('@'), 'Must be email');
// GOOD: built-in validator
z.string().email('Must be valid email');
// BAD: refine for length
z.string().refine((s) => s.length >= 1, 'Required');
// GOOD: built-in constraint
z.string().min(1, 'Required');
// GOOD: refine for cross-field validation (no built-in alternative)
.refine((data) => {
const ids = new Set(data.participants.map((p) => p.id));
return data.events.every((e) => ids.has(e.participant_id));
}, 'All event participant IDs must exist in participants array')
// GOOD: extracted — reused, 3+ fields, domain concept
export const CoordinatesSchema = z.strictObject({
latitude: LatitudeSchema,
longitude: LongitudeSchema,
});
// GOOD: inline — simple, single-use
name: z.string().min(1).max(100).meta({
title: 'Name',
description: 'Name of the entity',
examples: ['Example Name'],
}),
Use consistent casing based on semantic meaning:
['None', 'Low', 'Medium', 'High']['mainnet', 'testnet']['KILOGRAM', 'DATE', 'CURRENCY']Use IsoDateTimeSchema for ISO 8601 strings and unix milliseconds for epoch timestamps. Always include the pattern in the field name:
created_at: IsoDateTimeSchema.meta({ ... }), // ISO 8601 string
pickup_date: IsoDateSchema.meta({ ... }), // ISO 8601 date only
minted_at_ms: z.number().int().meta({ ... }), // Unix milliseconds