@selvajs/schemas
v4.5.0
Published
Schema definitions, generated TypeScript types, and code generators for Selva
Readme
Schema Generation
Single source of truth for type-safe C# and TypeScript types across the entire codebase.
Quick Start
- Edit
ui-schema.json - Run:
cd packages/schemas && pnpm run generate:all - Types auto-update in:
- TypeScript:
packages/schemas/src/generated/schema.ts - C#:
Plugin/Selva.Schema/Models/UISchema.Generated.cs
- TypeScript:
Adding a Type
{
"definitions": {
"MyType": {
"type": "object",
"properties": {
"id": { "type": "string" },
"name": { "type": "string" }
},
"required": ["id", "name"]
}
}
}Adding a Property to an Existing Type
- Open
ui-schema.json - Find the type in
definitions(e.g.,User) - Add property to
properties:"properties": { "id": { "type": "string" }, "name": { "type": "string" }, "email": { "type": "string" } // ← New property } - (Optional) Make it required — Add to
requiredarray:"required": ["id", "name", "email"] - Save and run:
cd packages/schemas && pnpm run generate:all - TypeScript and C# types update automatically ✓
Full Example
{
"definitions": {
"User": {
"type": "object",
"properties": {
"id": { "type": "string" },
"name": { "type": "string" },
"email": { "type": "string" },
"age": { "type": "integer" },
"active": { "type": "boolean" }
},
"required": ["id", "name", "email"]
}
}
}Optional properties (age, active) are not in required and become nullable in C# (int?, bool?).
Type Mappings
| JSON | TypeScript | C# (required) | C# (optional) |
| --------- | ---------- | ------------- | ------------- |
| string | string | string | string |
| number | number | double | double? |
| integer | number | int | int? |
| boolean | boolean | bool | bool? |
| array | T[] | List<T> | List<T> |
A property is "required" when it appears in the parent schema's required array.
Discriminated Unions
Define variants with const discriminator:
{
"Widget": {
"oneOf": [{ "$ref": "#/definitions/TextWidget" }, { "$ref": "#/definitions/NumberWidget" }]
},
"TextWidget": {
"type": "object",
"properties": {
"type": { "type": "string", "const": "text" },
"placeholder": { "type": "string" }
}
},
"NumberWidget": {
"type": "object",
"properties": {
"type": { "type": "string", "const": "number" },
"min": { "type": "number" }
}
}
}The C# and TypeScript generators automatically create the classes and converters.
