Skip to content

Commit f1bd2ef

Browse files
Copilothotlong
andcommitted
Update README files with ObjectSchema.create() examples
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 82448b4 commit f1bd2ef

2 files changed

Lines changed: 64 additions & 8 deletions

File tree

examples/app-react-crud/README.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,35 @@ Create an `objectstack.config.ts` to define your data models and application str
3636
```typescript
3737
// objectstack.config.ts
3838
import { defineStack } from '@objectstack/spec';
39+
import { ObjectSchema, Field } from '@objectstack/spec/data';
3940

40-
export const TaskObject = {
41+
export const Task = ObjectSchema.create({
4142
name: 'task',
4243
label: 'Task',
44+
icon: 'check-square',
45+
4346
fields: {
44-
subject: { type: 'text', required: true },
45-
priority: { type: 'number', defaultValue: 1 },
46-
isCompleted: { type: 'boolean', defaultValue: false }
47-
}
48-
};
47+
subject: Field.text({
48+
label: 'Subject',
49+
required: true,
50+
}),
51+
52+
priority: Field.number({
53+
label: 'Priority',
54+
defaultValue: 1,
55+
min: 1,
56+
max: 5,
57+
}),
58+
59+
is_completed: Field.boolean({
60+
label: 'Completed',
61+
defaultValue: false,
62+
}),
63+
},
64+
});
4965

5066
export default defineStack({
51-
objects: [TaskObject]
67+
objects: [Task]
5268
});
5369
```
5470

packages/spec/README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,49 @@ The **Source of Truth** for the ObjectStack Protocol. Contains strictly typed Zo
1212

1313
## Usage
1414

15+
**Recommended: Use `ObjectSchema.create()` with `Field.*` helpers for strict TypeScript validation:**
16+
17+
```typescript
18+
import { ObjectSchema, Field } from '@objectstack/spec/data';
19+
20+
// Create a validated object definition with type checking
21+
export const Task = ObjectSchema.create({
22+
name: 'task',
23+
label: 'Task',
24+
icon: 'check-square',
25+
26+
fields: {
27+
title: Field.text({
28+
label: 'Title',
29+
required: true,
30+
maxLength: 200,
31+
}),
32+
33+
status: Field.select({
34+
label: 'Status',
35+
options: [
36+
{ label: 'To Do', value: 'todo', default: true },
37+
{ label: 'In Progress', value: 'in_progress' },
38+
{ label: 'Done', value: 'done' },
39+
],
40+
}),
41+
},
42+
43+
enable: {
44+
trackHistory: true,
45+
apiEnabled: true,
46+
},
47+
});
48+
```
49+
50+
**Alternative: Runtime validation of existing objects:**
51+
1552
```typescript
16-
import { ObjectSchema, ViewSchema } from '@objectstack/spec';
53+
import { ObjectSchema } from '@objectstack/spec/data';
1754

1855
// Validate a JSON object against the schema
1956
const result = ObjectSchema.parse(myObjectDefinition);
57+
if (result.success) {
58+
console.log('Valid object:', result.data);
59+
}
2060
```

0 commit comments

Comments
 (0)