Skip to content

Commit 79abf50

Browse files
docs(fields): narrow two snippets to ObjectUI's own contract (#6126) (#6137)
The Sequence Management block on auto-number.mdx called db.transaction on a db declared nowhere in the workspace, and the Backend Validation block on object.mdx imported ajv, which no package.json in this repository declares. Both are narrowed to what ObjectUI actually exposes rather than satisfied with an ambient declaration or a new dependency, and both pages join the doc-snippet gate's compile population. Claude-Session: https://claude.ai/code/session_019b5UBNMtTzKbVtZZGvFuxe Co-authored-by: Claude <noreply@anthropic.com>
1 parent b567362 commit 79abf50

3 files changed

Lines changed: 87 additions & 43 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
---
3+
4+
Docs only, publishes nothing: two `content/docs/fields` snippets referenced
5+
identifiers the snippet program cannot resolve, so both pages were held out of
6+
objectui#5867's batch 3 (objectui#6126). Neither is fixed by making the compiler
7+
happy — each was narrowed to what ObjectUI actually owns.
8+
9+
`auto-number`'s Sequence Management block called `db.transaction` on a `db`
10+
declared nowhere in the workspace and exported by nothing (TS2304, plus TS7006
11+
on the `tx` that fell out of it). Declaring an ambient `db` would have had the
12+
renderer's documentation mint a backend contract it does not own, so the block
13+
is now the metadata the backend actually reads — a literal annotated with the
14+
exported `AutoNumberFieldMetadata`, carrying `format` and `starting_number` —
15+
and the transactional sketch is prose: one counter per object-and-field pair,
16+
incremented in the same transaction that inserts the record. The page now also
17+
says the thing it never said, which is that ObjectUI never allocates a value at
18+
all and renders a placeholder until the saved record comes back.
19+
20+
`object`'s Backend Validation block opened `import Ajv from 'ajv'`, and `ajv` is
21+
declared by no `package.json` in this repository and resolves from nowhere
22+
(TS2307). Adding it as a dependency to satisfy a checker was refused, so the
23+
block keeps the ObjectUI half — an `ObjectFieldMetadata` literal whose `schema`
24+
is the JSON Schema a server validates against — and the Ajv call sequence, which
25+
was Ajv's documentation rather than ObjectUI's, is a prose sentence naming it as
26+
one option among any JSON Schema validator. The section now states the fact a
27+
reader most needs: `ObjectField` checks JSON syntax only and never enforces
28+
`schema`, so structural validation is the server's.
29+
30+
Both pages join the compile population with the batch's own classifier: the four
31+
`plaintext`-fenced blocks whose first line starts with `import` or `interface`
32+
are now `ts`. The gate's blocks-to-compile count rises from 206 to 210 — exactly
33+
those four — with diagnostics at 0, no new `FRAGMENT_MARKER` declarations, and
34+
the covered/ungated and declared-fragment sets unmoved.

‎content/docs/fields/auto-number.mdx‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The AutoNumber Field component displays auto-generated sequence numbers. This is
1919

2020
## Field Schema
2121

22-
```plaintext
22+
```ts
2323
interface AutoNumberFieldSchema {
2424
type: 'auto_number';
2525
name: string; // Field name/ID
@@ -104,27 +104,34 @@ generateAutoNumber('ORD-{YYYY}-{0000}', 42);
104104

105105
## Sequence Management
106106

107-
The backend maintains sequence counters:
108-
109-
```plaintext
110-
interface SequenceCounter {
111-
object: string; // Object name
112-
field: string; // Field name
113-
current_value: number; // Current sequence number
114-
prefix?: string; // Optional prefix for partitioning
115-
}
116-
117-
// Increment sequence atomically
118-
const getNextSequence = async (object: string, field: string) => {
119-
return await db.transaction(async (tx) => {
120-
const counter = await tx.findOne('sequences', { object, field });
121-
const nextValue = (counter?.current_value || 0) + 1;
122-
await tx.upsert('sequences', { object, field }, { current_value: nextValue });
123-
return nextValue;
124-
});
107+
Allocating the next number is the backend's job, not the renderer's. ObjectUI
108+
never generates a value: `AutoNumberField` displays whatever the saved record
109+
already carries, and renders a muted placeholder dash while the field is still
110+
empty. What ObjectUI owns is the metadata the backend reads — where the
111+
sequence starts, and how each value is formatted:
112+
113+
```ts
114+
import type { AutoNumberFieldMetadata } from '@object-ui/types';
115+
116+
const orderNumber: AutoNumberFieldMetadata = {
117+
type: 'auto_number',
118+
name: 'order_number',
119+
label: 'Order Number',
120+
format: 'ORD-{YYYY}-{0000}',
121+
starting_number: 1,
125122
};
126123
```
127124

125+
On the backend, keep one counter per object-and-field pair and increment it in
126+
the same transaction that inserts the record, so two concurrent inserts cannot
127+
read the same current value. Partition the counter (per year, per prefix) only
128+
if the format resets — `'ORD-{YYYY}-{0000}'` needs one counter per year if the
129+
sequence is meant to restart each January.
130+
131+
Because the number is assigned at insert time, it does not exist while the
132+
record is still being drafted: a create form shows the field empty, and the
133+
value appears once the saved record comes back.
134+
128135
## Use Cases
129136

130137
- **Order Management**: Order numbers, PO numbers

‎content/docs/fields/object.mdx‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The Object Field component provides a JSON editor for storing and editing struct
2323

2424
## Field Schema
2525

26-
```plaintext
26+
```ts
2727
interface ObjectFieldSchema {
2828
type: 'object';
2929
name: string; // Field name/ID
@@ -199,33 +199,36 @@ For typed object fields, you can define a schema in two formats:
199199

200200
## Backend Validation
201201

202-
Example backend validation:
203-
204-
```plaintext
205-
import Ajv from 'ajv';
202+
ObjectUI does not enforce `schema`. `ObjectField` checks JSON **syntax** only —
203+
it accepts any value `JSON.parse` accepts, and simply declines to propagate a
204+
draft it cannot parse — so nothing on the client rejects a well-formed object
205+
whose shape is wrong. Structural validation belongs on the server.
206206

207-
const ajv = new Ajv();
207+
The `schema` you author is carried on the field metadata untouched, and in the
208+
JSON Schema Format above it is an ordinary JSON Schema document. That is the
209+
whole integration point: the server validates the incoming value against the
210+
very same object, using whichever JSON Schema validator it already has (Ajv,
211+
for instance — ObjectUI ships none and names none).
208212

209-
const validateObjectField = (value: any, schema: any) => {
210-
if (!schema) return { valid: true };
211-
212-
const validate = ajv.compile(schema);
213-
const valid = validate(value);
214-
215-
return {
216-
valid,
217-
errors: validate.errors
218-
};
219-
};
213+
```ts
214+
import type { ObjectFieldMetadata } from '@object-ui/types';
220215

221-
// Example schema
222-
const configSchema = {
216+
const apiConfig: ObjectFieldMetadata = {
223217
type: 'object',
224-
properties: {
225-
api_key: { type: 'string', minLength: 1 },
226-
timeout: { type: 'number', minimum: 0 },
227-
enabled: { type: 'boolean' }
218+
name: 'api_config',
219+
label: 'API Configuration',
220+
schema: {
221+
type: 'object',
222+
properties: {
223+
api_key: { type: 'string', minLength: 1 },
224+
timeout: { type: 'number', minimum: 0 },
225+
enabled: { type: 'boolean' },
226+
},
227+
required: ['api_key'],
228228
},
229-
required: ['api_key']
230229
};
231230
```
231+
232+
One caveat when you do: the Simplified Format above is
233+
documentation for a reader, not a validator input — only the JSON Schema Format
234+
can be handed to a JSON Schema validator as-is.

0 commit comments

Comments
 (0)