Repository navigation
Expand file tree
/
Copy pathserver-owned-value.ts
More file actions
126 lines (120 loc) · 5.53 KB
/
Copy pathserver-owned-value.ts
File metadata and controls
126 lines (120 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* The ONE client-side answer to "is this field's value the PRODUCER's to
* supply?" — the other half of {@link isMissingForRequired}.
*
* A `required` verdict is a question about two facts: is the value absent
* (`required-presence.ts`), and was the client ever supposed to provide it.
* This module owns the second one. Both live here in `@object-ui/core` because
* every layer that decides required-ness reads them: the form renderer
* (`resolveFieldRuleState`), the wizard's cross-step gate, and the
* create-form field builders in `@object-ui/plugin-form`.
*
* ## What makes a value server-owned
*
* A `defaultValue` may be a literal, or an *instruction* the server resolves
* per insert:
*
* - a runtime token — `'NOW()'` / `'current_user'`, the whole
* `DEFAULT_VALUE_TOKENS` family from `@objectstack/spec/data`
* - a CEL Expression envelope — `{ dialect: 'cel', source: 'today()' }`
*
* A CREATE form deliberately leaves such a control EMPTY and omits the key
* from the payload, because `ObjectQL.applyFieldDefaults` resolves a declared
* default only for a field that arrives absent or null (#4047 / #4068). The
* field is therefore not "missing" on that submit — it is server-owned, and
* nothing the client evaluates may declare it required (#4069 / #4085).
*
* ## Create only
*
* On an EDIT form the token was already resolved at insert. The control shows
* a persisted value, so blanking it is a real removal and `required` (static
* or conditional) enforces normally.
*/
import { isRuntimeDefaultToken } from '@objectstack/spec/data';
/**
* Is `v` a CEL/template Expression envelope rather than a literal value?
*
* Same shape test the engine applies before handing a default to
* `ExpressionEngine` (`{ dialect, source }`), kept structural on purpose: the
* point is "the server evaluates this", which is true for every dialect.
*/
function isExpressionEnvelope(v: unknown): boolean {
return (
typeof v === 'object' &&
v !== null &&
typeof (v as { dialect?: unknown }).dialect === 'string' &&
typeof (v as { source?: unknown }).source === 'string'
);
}
/**
* Is this declared `defaultValue` a RUNTIME INSTRUCTION the server resolves per
* insert, rather than a literal value?
*
* True for the `DEFAULT_VALUE_TOKENS` family (`'NOW()'` / `'current_user'`) and
* for CEL/template Expression envelopes. This is THE classifier for "the
* producer owns this field's create value", and every consumer reads THIS one
* — a second copy would be free to disagree about, say, a CEL envelope, and
* then a form would seed a field it also refuses to submit. Today's consumers:
*
* 1. seeding (#4047 / #4068) — such a default is not seeded, because putting
* the literal text `NOW()` into a datetime input and submitting it
* suppresses the very resolution the declaration asked for;
* 2. the create-mode static `required` rule (#4069) —
* `isRequiredInForm` in `@object-ui/plugin-form`;
* 3. the create-mode `requiredWhen` rule (#4085) —
* {@link isServerOwnedValue}, read by `resolveFieldRuleState`;
* 4. the "Set automatically when saved." hint (objectui#12108) — the form
* renderer reads the same {@link isServerOwnedValue} answer it hands to
* `resolveFieldRuleState`, so the hint and the lowered `required` are
* one verdict.
*/
export function isRuntimeDefault(v: unknown): boolean {
return isRuntimeDefaultToken(v) || isExpressionEnvelope(v);
}
/**
* A runtime `FormField` as far as this module is concerned: the resolved
* object-field metadata stash.
*
* Stated structurally rather than as `@object-ui/types`' `FormField` so the
* one key this module reads is visible in the signature. No index signature:
* `FormField` declares its keys and carries none (objectstack#25923), and an
* interface without one is not assignable to a type that has one — a
* `FormField` value is accepted here because it declares this `field` slot.
*/
export interface FieldWithDeclaredDefault {
field?: { defaultValue?: unknown } | null;
}
/**
* Does the PRODUCER supply this field's value for the submit this form is
* about to make? (#4085)
*
* The single derivation of that fact, so the three call sites that feed
* `resolveFieldRuleState` cannot each answer "where does the declared default
* live" differently. It reads the runtime FormField's `field` slot — the
* resolved object-field metadata the object-bound builders stash there
* (`ObjectForm`, `sectionFields`, and the containers' no-sections paths), and
* the only spelling of a declared `defaultValue` that reaches the renderer.
* A hand-authored FormField carries no object metadata, so it is never
* server-owned and its rules are untouched.
*
* `isCreateForm` is the CALLER's fact, never inferred here: the form renderer
* reads it off `FormSchema.previousValues` (whose absence *is* the declared
* "this is an insert" signal, objectui#3484), while the wizard's step gate
* reads its own `mode`. An evaluator guessing from a `previous` argument would
* call an edit-mode caller that simply passes no `previous` a create form, and
* silently drop a `requiredWhen` the author meant to enforce.
*/
export function isServerOwnedValue(
field: FieldWithDeclaredDefault | null | undefined,
isCreateForm: boolean,
): boolean {
if (!isCreateForm) return false;
return isRuntimeDefault(field?.field?.defaultValue);
}