@20baht/validator 0.1.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/assignment.d.ts +5 -0
- package/dist/assignment.js +120 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/schema.d.ts +6 -1
- package/dist/schema.js +39 -35
- package/dist/shape.d.ts +2 -3
- package/dist/shape.js +52 -48
- package/dist/submission.js +2 -2
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -94,6 +94,8 @@ Peers on [`@20baht/engine`](https://www.npmjs.com/package/@20baht/engine) of the
|
|
|
94
94
|
- Approval steps: a `decision` must be present, the comment must be a string, and the comment policy (`required` / `requireOnReject`) is honored (a blank or zero-width comment does not count).
|
|
95
95
|
- **Canonical data holds only what the UI could emit:** repeater rows keep their declared columns only (a row with its own `__proto__` key is rejected), file values must be uploaded refs `{ name, size, url, type? }` with an `http(s)` or relative `url` and keep only those keys, `-0` is stored as `0`.
|
|
96
96
|
- **Strict by default (0.5.0):** a `custom` validator or `submit` script with no own entry in the registry, and a script that throws, are issues instead of being skipped. Pass `{ strict: false }` as the fifth argument to get the client's warn-and-skip behaviour (previews, docs tooling).
|
|
97
|
+
- **Field types of the app's own** (spec 01 §5.1): pass the registry to the gate too, `parseSchema(json, registry)`, and a `type` with an entry in `registry.fieldTypes` is accepted, as a field or as a repeater column. `validateSubmission` runs the entry's `check`; a type the registry it was given does not have is an issue on the field, with `strict: false` as well.
|
|
98
|
+
- **`assignment` on an approval step** (spec 03 §4.1) is checked by `parseSchema`: known target types, `field` / `rows` targets that an earlier form collects, `hops` and `quorum` whole numbers from 1, a `when` that reads fields the workflow has. Whether a group, a role or a `custom` name exists is yours to check when you store the definition. Keys the spec does not name (`due_in_hours`) pass through.
|
|
97
99
|
- **Client `submit` scripts** (spec 01 §14): re-run headlessly with the same `registry.scripts` — `g.setValue()` lands in canonical data, `g.addErrorMessage()` becomes an issue on `$` (`form_name.$` in a workflow), returning `false` blocks. `load` / `change` scripts are UI-only and never run here, so a field a client script hides is still validated: keep submit-relevant logic in rules or validators. `parseSchema` rejects a `scripts[]` entry with an unknown `on`, no `handler`, or a `change` hook on a field that does not exist.
|
|
98
100
|
|
|
99
101
|
## API
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type WorkflowDefinition, type WorkflowForm } from '@20baht/engine';
|
|
2
|
+
/** Checks a target's `when` against the workflow's refs; row-scoped refs are refused. */
|
|
3
|
+
export type CheckWhen = (when: unknown, where: string) => void;
|
|
4
|
+
/** Throws when `form.assignment` is present and not what spec 03 §4.1 describes. */
|
|
5
|
+
export declare function assertAssignment(form: WorkflowForm, wf: WorkflowDefinition, checkWhen: CheckWhen): void;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// parseSchema's checks of `assignment` on an approval step (spec 03 §4.1). What can be
|
|
3
|
+
// known from the definition alone: the shape, the target vocabulary, and that a target
|
|
4
|
+
// reading the request reads something an earlier form collects. Whether a group, a role
|
|
5
|
+
// or a `custom` name exists is the host's gate.
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
import { rowSubFields, } from '@20baht/engine';
|
|
8
|
+
import { isRecord, isStatic } from './common.js';
|
|
9
|
+
const isName = (value) => typeof value === 'string' && value !== '';
|
|
10
|
+
const isCount = (value) => typeof value === 'number' && Number.isInteger(value) && value >= 1;
|
|
11
|
+
/** The value-carrying fields of the forms before `order`, by key. */
|
|
12
|
+
function fieldsBefore(wf, order) {
|
|
13
|
+
const fields = new Map();
|
|
14
|
+
for (const form of wf.forms) {
|
|
15
|
+
if (form.order >= order)
|
|
16
|
+
continue;
|
|
17
|
+
for (const field of form.fields)
|
|
18
|
+
if (!isStatic(field))
|
|
19
|
+
fields.set(field.key, field);
|
|
20
|
+
}
|
|
21
|
+
return fields;
|
|
22
|
+
}
|
|
23
|
+
function collected(key, earlier, where) {
|
|
24
|
+
const field = typeof key === 'string' ? earlier.get(key) : undefined;
|
|
25
|
+
if (!field) {
|
|
26
|
+
throw new Error(`${where}: field ${JSON.stringify(key)} is not collected before this step`);
|
|
27
|
+
}
|
|
28
|
+
return field;
|
|
29
|
+
}
|
|
30
|
+
function assertRowsTarget(target, earlier, where) {
|
|
31
|
+
const repeater = collected(target.field, earlier, where);
|
|
32
|
+
if (repeater.type !== 'repeater') {
|
|
33
|
+
throw new Error(`${where}: ${JSON.stringify(target.field)} is not a repeater`);
|
|
34
|
+
}
|
|
35
|
+
const columns = new Set(rowSubFields(repeater).map((sub) => sub.key));
|
|
36
|
+
for (const prop of ['assignee', 'group', 'label']) {
|
|
37
|
+
const column = target[prop];
|
|
38
|
+
if (column !== undefined && (typeof column !== 'string' || !columns.has(column))) {
|
|
39
|
+
throw new Error(`${where}: ${JSON.stringify(column)} is not a column of ${repeater.key}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (target.manager_of_assignee !== undefined && typeof target.manager_of_assignee !== 'boolean') {
|
|
43
|
+
throw new Error(`${where}: manager_of_assignee must be true or false`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function assertCustomTarget(target, where) {
|
|
47
|
+
if (!isName(target.fn)) {
|
|
48
|
+
throw new Error(`${where}: a custom target needs fn, the name the host registered`);
|
|
49
|
+
}
|
|
50
|
+
if (target.params !== undefined && !isRecord(target.params)) {
|
|
51
|
+
throw new Error(`${where}: params of the custom target must be an object`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** Per target type: what must hold beyond `type` itself. */
|
|
55
|
+
const TARGET_CHECKS = {
|
|
56
|
+
requester: () => undefined,
|
|
57
|
+
field: (target, earlier, where) => void collected(target.value, earlier, where),
|
|
58
|
+
group: (target, _earlier, where) => {
|
|
59
|
+
if (!isName(target.value))
|
|
60
|
+
throw new Error(`${where}: a group target needs value, a group id`);
|
|
61
|
+
if (target.task !== undefined && !isName(target.task)) {
|
|
62
|
+
throw new Error(`${where}: task of the group target must be a label`);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
role: (target, _earlier, where) => {
|
|
66
|
+
if (!isName(target.value))
|
|
67
|
+
throw new Error(`${where}: a role target needs value, a role name`);
|
|
68
|
+
},
|
|
69
|
+
manager: (target, _earlier, where) => {
|
|
70
|
+
if (target.hops !== undefined && !isCount(target.hops)) {
|
|
71
|
+
throw new Error(`${where}: hops must be a whole number from 1`);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
head: (target, _earlier, where) => {
|
|
75
|
+
if (target.department !== undefined && !isName(target.department)) {
|
|
76
|
+
throw new Error(`${where}: department of the head target must be a name`);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
rows: assertRowsTarget,
|
|
80
|
+
custom: (target, _earlier, where) => {
|
|
81
|
+
assertCustomTarget(target, where);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
function assertRule(rule, where) {
|
|
85
|
+
if (rule === 'any' || rule === 'all')
|
|
86
|
+
return;
|
|
87
|
+
if (isRecord(rule) && isCount(rule.quorum))
|
|
88
|
+
return;
|
|
89
|
+
throw new Error(`${where}: rule must be "any", "all" or { "quorum": k } with k a whole number from 1, ` +
|
|
90
|
+
`got ${JSON.stringify(rule)}`);
|
|
91
|
+
}
|
|
92
|
+
/** Throws when `form.assignment` is present and not what spec 03 §4.1 describes. */
|
|
93
|
+
export function assertAssignment(form, wf, checkWhen) {
|
|
94
|
+
const assignment = form.assignment;
|
|
95
|
+
if (assignment === undefined)
|
|
96
|
+
return;
|
|
97
|
+
const where = `assignment on ${form.form_name}`;
|
|
98
|
+
if (form.kind !== 'approval')
|
|
99
|
+
throw new Error(`${where}: only an approval step may carry one`);
|
|
100
|
+
if (!isRecord(assignment))
|
|
101
|
+
throw new Error(`${where} must be an object`);
|
|
102
|
+
if (assignment.label !== undefined && typeof assignment.label !== 'string') {
|
|
103
|
+
throw new Error(`${where}: label must be text`);
|
|
104
|
+
}
|
|
105
|
+
const targets = assignment.assign_to;
|
|
106
|
+
if (!Array.isArray(targets) || targets.length === 0) {
|
|
107
|
+
throw new Error(`${where}: assign_to must be a non-empty list of targets`);
|
|
108
|
+
}
|
|
109
|
+
const earlier = fieldsBefore(wf, form.order);
|
|
110
|
+
for (const target of targets) {
|
|
111
|
+
const type = isRecord(target) ? target.type : undefined;
|
|
112
|
+
const check = typeof type === 'string' ? TARGET_CHECKS[type] : undefined;
|
|
113
|
+
if (!isRecord(target) || !check || !Object.hasOwn(TARGET_CHECKS, type)) {
|
|
114
|
+
throw new Error(`${where}: unknown target type ${JSON.stringify(type)}`);
|
|
115
|
+
}
|
|
116
|
+
check(target, earlier, where);
|
|
117
|
+
checkWhen(target.when, where);
|
|
118
|
+
}
|
|
119
|
+
assertRule(assignment.rule, where);
|
|
120
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export { validateFormData, validateSubmission, validateWorkflowData } from './su
|
|
|
4
4
|
export { verifyOptions, verifyReferences, type VerifyOptions } from './verify.js';
|
|
5
5
|
export { parseSchema } from './schema.js';
|
|
6
6
|
import { applyFlags, approvalFields, defaultValue, evaluate, resolveViewFields, setLocale, validateField, type FormDefinition, type NestedData, type Registry, type WorkflowDefinition, type WorkflowForm } from '@20baht/engine';
|
|
7
|
+
export { BUILT_IN_FIELD_TYPES } from '@20baht/engine';
|
|
7
8
|
export { applyFlags, approvalFields, defaultValue, evaluate, resolveViewFields, setLocale, validateField, };
|
|
8
9
|
export type { FormDefinition, NestedData, Registry, WorkflowDefinition, WorkflowForm };
|
|
9
10
|
export type { FeatureFlags, FormViewSpec, Locale, MessageCatalog } from '@20baht/engine';
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,9 @@ export { validateFormData, validateSubmission, validateWorkflowData } from './su
|
|
|
17
17
|
export { verifyOptions, verifyReferences } from './verify.js';
|
|
18
18
|
export { parseSchema } from './schema.js';
|
|
19
19
|
import { applyFlags, approvalFields, defaultValue, evaluate, resolveViewFields, setLocale, validateField, } from '@20baht/engine';
|
|
20
|
+
// The built-in field types `parseSchema` accepts with no registry (spec 01 §2), for tools that
|
|
21
|
+
// offer a list of them.
|
|
22
|
+
export { BUILT_IN_FIELD_TYPES } from '@20baht/engine';
|
|
20
23
|
// Re-exported so servers can type stores and registries without a direct
|
|
21
24
|
// dependency on @20baht/ui. `setLocale` localizes the issues this package
|
|
22
25
|
// emits (same catalog the client renders). `applyFlags` resolves feature
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Registry } from '@20baht/engine';
|
|
1
2
|
import { type SchemaDefinition } from './common.js';
|
|
2
3
|
/**
|
|
3
4
|
* Structural gate for storing builder output — the same checks the builder's
|
|
@@ -8,5 +9,9 @@ import { type SchemaDefinition } from './common.js';
|
|
|
8
9
|
* refs, transition endpoints, on_reject.goto) and compiles every `pattern`
|
|
9
10
|
* validator, so authoring defects surface at store time — not as a 500 or a
|
|
10
11
|
* submission that can never pass.
|
|
12
|
+
*
|
|
13
|
+
* `registry` is read for one thing: `fieldTypes`, the field types of the app's own (spec 01
|
|
14
|
+
* §5.1). A `type` that is neither built-in nor registered there is refused, so pass the registry
|
|
15
|
+
* the form will be rendered and validated with.
|
|
11
16
|
*/
|
|
12
|
-
export declare function parseSchema(json: unknown): SchemaDefinition;
|
|
17
|
+
export declare function parseSchema(json: unknown, registry?: Pick<Registry, 'fieldTypes'>): SchemaDefinition;
|
package/dist/schema.js
CHANGED
|
@@ -1,36 +1,7 @@
|
|
|
1
|
-
import { approvalFields, conditionRowRefs, parseDisabledEntry, parseIsoDate, parseRowRef, rangeBounds, rowSubFields, UNSAFE_KEYS, isUnsafePattern, normalizeLegacySchema, } from '@20baht/engine';
|
|
1
|
+
import { approvalFields, fieldTypeSpec, isBuiltInFieldType, isTimeOfDay, conditionRowRefs, parseDisabledEntry, parseIsoDate, parseRowRef, rangeBounds, rowSubFields, UNSAFE_KEYS, isUnsafePattern, normalizeLegacySchema, } from '@20baht/engine';
|
|
2
2
|
import { isFormDefinition, isRecord, isStatic, isWorkflowDefinition, } from './common.js';
|
|
3
|
-
import {
|
|
3
|
+
import { assertAssignment } from './assignment.js';
|
|
4
4
|
const DATE_MODES = new Set(['single', 'range', 'multiple']);
|
|
5
|
-
/** Every `FieldType`; `satisfies` rejects a name that is not one, the check below a type left out. */
|
|
6
|
-
const FIELD_TYPE_LIST = [
|
|
7
|
-
'text',
|
|
8
|
-
'textarea',
|
|
9
|
-
'markdown',
|
|
10
|
-
'number',
|
|
11
|
-
'currency',
|
|
12
|
-
'email',
|
|
13
|
-
'password',
|
|
14
|
-
'dropdown',
|
|
15
|
-
'multiselect',
|
|
16
|
-
'checkbox',
|
|
17
|
-
'radio',
|
|
18
|
-
'toggle',
|
|
19
|
-
'date',
|
|
20
|
-
'time',
|
|
21
|
-
'file',
|
|
22
|
-
'label',
|
|
23
|
-
'section',
|
|
24
|
-
'repeater',
|
|
25
|
-
'tags',
|
|
26
|
-
'rating',
|
|
27
|
-
'range',
|
|
28
|
-
'reference',
|
|
29
|
-
];
|
|
30
|
-
const FIELD_TYPES = new Set(FIELD_TYPE_LIST);
|
|
31
|
-
/** Fails to compile when the engine gains a field type this list does not have. */
|
|
32
|
-
const _everyFieldTypeListed = true;
|
|
33
|
-
void _everyFieldTypeListed;
|
|
34
5
|
/**
|
|
35
6
|
* Structural gate for storing builder output — the same checks the builder's
|
|
36
7
|
* Import applies, so the server only ever stores schemas the client could
|
|
@@ -40,8 +11,12 @@ void _everyFieldTypeListed;
|
|
|
40
11
|
* refs, transition endpoints, on_reject.goto) and compiles every `pattern`
|
|
41
12
|
* validator, so authoring defects surface at store time — not as a 500 or a
|
|
42
13
|
* submission that can never pass.
|
|
14
|
+
*
|
|
15
|
+
* `registry` is read for one thing: `fieldTypes`, the field types of the app's own (spec 01
|
|
16
|
+
* §5.1). A `type` that is neither built-in nor registered there is refused, so pass the registry
|
|
17
|
+
* the form will be rendered and validated with.
|
|
43
18
|
*/
|
|
44
|
-
export function parseSchema(json) {
|
|
19
|
+
export function parseSchema(json, registry) {
|
|
45
20
|
const raw = typeof json === 'string' ? JSON.parse(json) : json;
|
|
46
21
|
if (!isRecord(raw))
|
|
47
22
|
throw new Error('not an object');
|
|
@@ -52,15 +27,42 @@ export function parseSchema(json) {
|
|
|
52
27
|
console.warn(`[@20baht/validator] legacy camelCase schema keys migrated (${renamed.length}): ${renamed.slice(0, 5).join(', ')}${renamed.length > 5 ? ', …' : ''} — re-save the schema with snake_case keys.`);
|
|
53
28
|
}
|
|
54
29
|
if (isWorkflowDefinition(parsed)) {
|
|
30
|
+
assertFieldTypes(parsed.forms, registry);
|
|
55
31
|
assertValidWorkflow(parsed);
|
|
56
32
|
return parsed;
|
|
57
33
|
}
|
|
58
34
|
if (isFormDefinition(parsed)) {
|
|
35
|
+
assertFieldTypes([parsed], registry);
|
|
59
36
|
assertFormParts(parsed);
|
|
60
37
|
return parsed;
|
|
61
38
|
}
|
|
62
39
|
throw new Error('missing workflow_id/forms/rules (workflow) or form_id/form_name/fields (form)');
|
|
63
40
|
}
|
|
41
|
+
const isKnownType = (type, registry) => isBuiltInFieldType(type) || fieldTypeSpec(registry, type) !== undefined;
|
|
42
|
+
/** A repeater's columns are held to the same list as the form's own fields. */
|
|
43
|
+
function assertColumnTypes(field, name, registry) {
|
|
44
|
+
for (const sub of (Array.isArray(field.fields) ? field.fields : []).filter(isRecord)) {
|
|
45
|
+
if (!isKnownType(sub.type, registry)) {
|
|
46
|
+
throw new Error(`unknown field type ${JSON.stringify(sub.type)} on ${name}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Every field's `type` is one the form can be rendered with: built-in, or registered by the app
|
|
52
|
+
* (spec 01 §5.1). Runs first and reads the JSON as untrusted: what is not shaped like a form or
|
|
53
|
+
* a field is left for the checks after it to name.
|
|
54
|
+
*/
|
|
55
|
+
function assertFieldTypes(forms, registry) {
|
|
56
|
+
for (const form of forms.filter(isRecord)) {
|
|
57
|
+
const name = String(form.form_name);
|
|
58
|
+
for (const field of (Array.isArray(form.fields) ? form.fields : []).filter(isRecord)) {
|
|
59
|
+
if (!isKnownType(field.type, registry)) {
|
|
60
|
+
throw new Error(`unknown field type ${JSON.stringify(field.type)} on ${name}`);
|
|
61
|
+
}
|
|
62
|
+
assertColumnTypes(field, name, registry);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
64
66
|
function assertFormParts(form) {
|
|
65
67
|
assertValidFields(form);
|
|
66
68
|
assertValidViews(form);
|
|
@@ -134,6 +136,11 @@ function assertValidWorkflow(parsed) {
|
|
|
134
136
|
const refs = fieldRefs(parsed);
|
|
135
137
|
for (const f of parsed.forms)
|
|
136
138
|
assertApprovalConfig(f, nameSet);
|
|
139
|
+
for (const f of parsed.forms) {
|
|
140
|
+
assertAssignment(f, parsed, (when, where) => {
|
|
141
|
+
assertValidCondition(when, refs, where, false);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
137
144
|
assertValidRules(parsed, nameSet, refs);
|
|
138
145
|
assertTransitions(parsed, nameSet, refs);
|
|
139
146
|
}
|
|
@@ -247,9 +254,6 @@ function assertFieldValidators(field, form_name) {
|
|
|
247
254
|
}
|
|
248
255
|
}
|
|
249
256
|
function assertFieldShape(field, form_name) {
|
|
250
|
-
if (typeof field.type !== 'string' || !FIELD_TYPES.has(field.type)) {
|
|
251
|
-
throw new Error(`unknown field type ${JSON.stringify(field.type)} on ${form_name}`);
|
|
252
|
-
}
|
|
253
257
|
assertFieldKey(field, form_name);
|
|
254
258
|
if (field.type === 'date')
|
|
255
259
|
assertDateProps(field, form_name);
|
package/dist/shape.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { type FieldDescriptor, type NestedData, type WorkflowDefinition } from '@20baht/engine';
|
|
1
|
+
import { type FieldDescriptor, type NestedData, type Registry, type WorkflowDefinition } from '@20baht/engine';
|
|
2
2
|
import { type ValidationIssue } from './common.js';
|
|
3
|
-
export declare const isTimeOfDay: (v: unknown) => boolean;
|
|
4
3
|
/** Shape issues for one field, recursing one level into repeater rows. */
|
|
5
|
-
export declare function shapeIssues(field: FieldDescriptor, value: unknown, ref: string): ValidationIssue[];
|
|
4
|
+
export declare function shapeIssues(field: FieldDescriptor, value: unknown, ref: string, registry?: Registry): ValidationIssue[];
|
|
6
5
|
/**
|
|
7
6
|
* Canonical value for one field, as the client would emit it: `tags` via
|
|
8
7
|
* `canonicalTags` (the transform `validateField` applies internally, so the
|
package/dist/shape.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { normalizeTagsValue, parseIsoDate, parseIsoDateTime, ratingMax, rowSubFields, UNSAFE_KEYS, t, isUploadedFile, } from '@20baht/engine';
|
|
1
|
+
import { fieldTypeSpec, isBuiltInFieldType, normalizeTagsValue, parseIsoDate, parseIsoDateTime, ratingMax, rowSubFields, UNSAFE_KEYS, t, isTimeOfDay, isUploadedFile, } from '@20baht/engine';
|
|
2
2
|
import { isRecord } from './common.js';
|
|
3
3
|
// ---- value-shape conformance -------------------------------------------------
|
|
4
4
|
/**
|
|
@@ -27,10 +27,6 @@ function dateIssue(field, value) {
|
|
|
27
27
|
return shapeMessage(Array.isArray(value) && value.every(isDay));
|
|
28
28
|
return shapeMessage(isDay(value));
|
|
29
29
|
}
|
|
30
|
-
// A time of day, 24-hour, to the minute. Kept here rather than imported from the engine so this
|
|
31
|
-
// release still runs against a @20baht/ui 0.19.0 peer, whose engine has no time type.
|
|
32
|
-
const TIME_OF_DAY = /^(?:[01]\d|2[0-3]):[0-5]\d$/;
|
|
33
|
-
export const isTimeOfDay = (v) => typeof v === 'string' && TIME_OF_DAY.test(v);
|
|
34
30
|
function choiceIssue(value, optionKeys) {
|
|
35
31
|
if (typeof value !== 'string')
|
|
36
32
|
return t('pattern');
|
|
@@ -69,54 +65,62 @@ function fileIssue(field, value) {
|
|
|
69
65
|
return shapeMessage(Array.isArray(value) && value.every(isUploadedFile));
|
|
70
66
|
return shapeMessage(isUploadedFile(value));
|
|
71
67
|
}
|
|
72
|
-
|
|
68
|
+
const isText = (_field, value) => shapeMessage(typeof value === 'string');
|
|
69
|
+
const isNumber = (_field, value) => shapeMessage(typeof value === 'number' && Number.isFinite(value));
|
|
70
|
+
const isBoolean = (_field, value) => shapeMessage(typeof value === 'boolean');
|
|
71
|
+
const isOneChoice = (_field, value, optionKeys) => choiceIssue(value, optionKeys);
|
|
72
|
+
const noValue = () => null; // a static block carries none
|
|
73
|
+
/**
|
|
74
|
+
* The shape of every built-in type. A `Record`, so a type added to the engine does not compile
|
|
75
|
+
* here until it says what a direct API call may send for it.
|
|
76
|
+
*/
|
|
77
|
+
const SHAPES = {
|
|
78
|
+
text: isText,
|
|
79
|
+
textarea: isText,
|
|
80
|
+
markdown: isText,
|
|
81
|
+
email: isText,
|
|
82
|
+
password: isText,
|
|
83
|
+
reference: isText,
|
|
84
|
+
date: dateIssue,
|
|
85
|
+
time: (_field, value) => shapeMessage(isTimeOfDay(value)),
|
|
86
|
+
number: isNumber,
|
|
87
|
+
currency: isNumber,
|
|
88
|
+
range: isNumber,
|
|
89
|
+
checkbox: isBoolean,
|
|
90
|
+
toggle: isBoolean,
|
|
91
|
+
dropdown: isOneChoice,
|
|
92
|
+
radio: isOneChoice,
|
|
93
|
+
multiselect: (_field, value, optionKeys) => multiChoiceIssue(value, optionKeys),
|
|
94
|
+
tags: tagsShapeIssue,
|
|
95
|
+
rating: (field, value) => shapeMessage(typeof value === 'number' &&
|
|
96
|
+
Number.isInteger(value) &&
|
|
97
|
+
value >= 1 &&
|
|
98
|
+
value <= ratingMax(field)),
|
|
99
|
+
file: fileIssue,
|
|
100
|
+
repeater: (_field, value) => shapeMessage(Array.isArray(value)),
|
|
101
|
+
label: noValue,
|
|
102
|
+
section: noValue,
|
|
103
|
+
};
|
|
104
|
+
const UNKNOWN_TYPE = (field) => `unknown field type ${JSON.stringify(field.type)}`;
|
|
105
|
+
/**
|
|
106
|
+
* The issue with one value, or null. A type nobody can check is never valid, whatever the
|
|
107
|
+
* value: `parseSchema` refuses such a schema, so one that reaches here was stored without it or
|
|
108
|
+
* with another registry. A type of the app's own (spec 01 §5.1) has no shape here: its `check`
|
|
109
|
+
* runs in `validateField`.
|
|
110
|
+
*/
|
|
111
|
+
function typeIssue(field, value, registry) {
|
|
112
|
+
if (!isBuiltInFieldType(field.type)) {
|
|
113
|
+
return fieldTypeSpec(registry, field.type) ? null : UNKNOWN_TYPE(field);
|
|
114
|
+
}
|
|
73
115
|
if (value === null || value === undefined)
|
|
74
116
|
return null;
|
|
75
117
|
// dynamic options (options_from) can't be checked without the registry fetch
|
|
76
118
|
const optionKeys = field.options_from ? null : field.options?.map((o) => o.key);
|
|
77
|
-
|
|
78
|
-
case 'text':
|
|
79
|
-
case 'textarea':
|
|
80
|
-
case 'markdown':
|
|
81
|
-
case 'email':
|
|
82
|
-
case 'password':
|
|
83
|
-
case 'reference':
|
|
84
|
-
return shapeMessage(typeof value === 'string');
|
|
85
|
-
case 'date':
|
|
86
|
-
return dateIssue(field, value);
|
|
87
|
-
case 'time':
|
|
88
|
-
return shapeMessage(isTimeOfDay(value));
|
|
89
|
-
case 'number':
|
|
90
|
-
case 'currency':
|
|
91
|
-
case 'range':
|
|
92
|
-
return shapeMessage(typeof value === 'number' && Number.isFinite(value));
|
|
93
|
-
case 'checkbox':
|
|
94
|
-
case 'toggle':
|
|
95
|
-
return shapeMessage(typeof value === 'boolean');
|
|
96
|
-
case 'dropdown':
|
|
97
|
-
case 'radio':
|
|
98
|
-
return choiceIssue(value, optionKeys);
|
|
99
|
-
case 'multiselect':
|
|
100
|
-
return multiChoiceIssue(value, optionKeys);
|
|
101
|
-
case 'tags':
|
|
102
|
-
return tagsShapeIssue(field, value, optionKeys);
|
|
103
|
-
case 'rating':
|
|
104
|
-
return shapeMessage(typeof value === 'number' &&
|
|
105
|
-
Number.isInteger(value) &&
|
|
106
|
-
value >= 1 &&
|
|
107
|
-
value <= ratingMax(field));
|
|
108
|
-
case 'file':
|
|
109
|
-
return fileIssue(field, value);
|
|
110
|
-
case 'repeater':
|
|
111
|
-
return shapeMessage(Array.isArray(value));
|
|
112
|
-
case 'label':
|
|
113
|
-
case 'section':
|
|
114
|
-
return null; // static blocks carry no value
|
|
115
|
-
}
|
|
119
|
+
return SHAPES[field.type](field, value, optionKeys);
|
|
116
120
|
}
|
|
117
121
|
/** Shape issues for one field, recursing one level into repeater rows. */
|
|
118
|
-
export function shapeIssues(field, value, ref) {
|
|
119
|
-
const message = typeIssue(field, value);
|
|
122
|
+
export function shapeIssues(field, value, ref, registry) {
|
|
123
|
+
const message = typeIssue(field, value, registry);
|
|
120
124
|
if (message !== null)
|
|
121
125
|
return [{ ref, message }];
|
|
122
126
|
const issues = [];
|
|
@@ -134,7 +138,7 @@ export function shapeIssues(field, value, ref) {
|
|
|
134
138
|
if (UNSAFE_KEYS.has(sub.key))
|
|
135
139
|
continue;
|
|
136
140
|
const v = Object.hasOwn(row, sub.key) ? row[sub.key] : undefined;
|
|
137
|
-
const m = typeIssue(sub, v);
|
|
141
|
+
const m = typeIssue(sub, v, registry);
|
|
138
142
|
if (m !== null)
|
|
139
143
|
issues.push({ ref: `${ref}[${i}].${sub.key}`, message: m });
|
|
140
144
|
}
|
package/dist/submission.js
CHANGED
|
@@ -73,7 +73,7 @@ function serverRegistry(forms, registry, options) {
|
|
|
73
73
|
* descriptor default.
|
|
74
74
|
*/
|
|
75
75
|
function validatedFieldData(field, raw, registry, data) {
|
|
76
|
-
const shape = shapeIssues(field, raw, field.key);
|
|
76
|
+
const shape = shapeIssues(field, raw, field.key, registry);
|
|
77
77
|
if (shape.length > 0)
|
|
78
78
|
return { value: defaultValue(field), issues: shape };
|
|
79
79
|
const value = normalizeCanonicalValue(field, raw);
|
|
@@ -238,7 +238,7 @@ function workflowFieldIssues(form, field, derived, registry) {
|
|
|
238
238
|
if (state && !state.visible)
|
|
239
239
|
return []; // hidden fields excluded (spec 02 §5)
|
|
240
240
|
const formData = derived.data[form.form_name] ?? {};
|
|
241
|
-
const shape = shapeIssues(field, formData[field.key], ref);
|
|
241
|
+
const shape = shapeIssues(field, formData[field.key], ref, registry);
|
|
242
242
|
if (shape.length > 0) {
|
|
243
243
|
formData[field.key] = defaultValue(field); // junk never reaches canonical data
|
|
244
244
|
return shape;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@20baht/validator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20.19.0"
|
|
@@ -50,12 +50,13 @@
|
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@20baht/engine": "workspace:*",
|
|
53
|
+
"@20baht/formats": "workspace:*",
|
|
53
54
|
"elysia": "^1.4.0",
|
|
54
|
-
"typescript": "^5.
|
|
55
|
+
"typescript": "^5.9.3",
|
|
55
56
|
"vitest": "^3.2.4"
|
|
56
57
|
},
|
|
57
58
|
"peerDependencies": {
|
|
58
|
-
"@20baht/engine": ">=0.
|
|
59
|
+
"@20baht/engine": ">=0.3.0 <0.4.0",
|
|
59
60
|
"elysia": ">=1.4.0 <2.0.0"
|
|
60
61
|
},
|
|
61
62
|
"peerDependenciesMeta": {
|