@20baht/validator 0.3.1 → 0.3.3
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 +70 -0
- package/dist/common.d.ts +73 -1
- package/dist/elysia.d.ts +89 -2
- package/dist/elysia.js +90 -1
- package/dist/index.d.ts +1 -1
- package/dist/submission.d.ts +4 -4
- package/dist/submission.js +0 -24
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -70,6 +70,61 @@ new Elysia()
|
|
|
70
70
|
`bahtValidator(options)` takes `{ registry?, locale?, strict?, verify? }`; `verify: true` also
|
|
71
71
|
awaits `verifyOptions` and `verifyReferences`. The `t` body schema is not derived from the form.
|
|
72
72
|
|
|
73
|
+
### Typed: the form as the route's `body`
|
|
74
|
+
|
|
75
|
+
`formBody(schema, options?)` is the form as a route `body`, the way a `t.Object` is one. Elysia
|
|
76
|
+
validates the request with it; the handler's `body` is the canonical data, **typed from the
|
|
77
|
+
form**, and an Eden client of the route is asked for the same type. A refused body answers the
|
|
78
|
+
same `422 { issues }`. It needs no plugin; the options are `bahtValidator`'s.
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { defineForm } from '@20baht/engine';
|
|
82
|
+
import { formBody } from '@20baht/validator/elysia';
|
|
83
|
+
|
|
84
|
+
export const contactForm = defineForm({
|
|
85
|
+
form_id: 'contact',
|
|
86
|
+
form_name: 'Contact',
|
|
87
|
+
fields: [
|
|
88
|
+
{ order: 1, type: 'text', key: 'name', required: true },
|
|
89
|
+
{ order: 2, type: 'number', key: 'age' },
|
|
90
|
+
],
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
new Elysia().post('/contact', ({ body }) => save(body), { body: formBody(contactForm, { registry }) });
|
|
94
|
+
// body: { name: string; age: number | null }
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`formBody({ schema: workflow, form: 'Rule' })` is one form of a workflow, `formBody(workflow)` the
|
|
98
|
+
whole of it. The types come from `defineForm` / `defineWorkflow`; a schema typed `FormDefinition`
|
|
99
|
+
(parsed JSON, a stored definition) validates just the same and reads as `Record<string, unknown>`.
|
|
100
|
+
The form is a [Standard Schema](https://standardschema.dev), which Elysia takes as a body since
|
|
101
|
+
1.4.
|
|
102
|
+
|
|
103
|
+
**OpenAPI and MCP.** The body also describes itself as JSON Schema (`jsonSchemaOf` of
|
|
104
|
+
`@20baht/engine`), so `@elysiajs/openapi` puts the form in the document as the route's request
|
|
105
|
+
body with no `t` schema beside it, and `@20baht/elysia-openapi-mcp` gives an agent a tool whose
|
|
106
|
+
arguments are the form's fields (label as `title`, help as `description`, option keys as `enum`,
|
|
107
|
+
lengths and bounds). The schema is a description, not the check: a form on its own lists its
|
|
108
|
+
`required` fields; a workflow lists none, because a rule can hide a field or lift `required`, and
|
|
109
|
+
`compare`, `custom` and `options_from` have no JSON Schema at all. The validator still decides,
|
|
110
|
+
and a refused call comes back as `422 { issues }`.
|
|
111
|
+
|
|
112
|
+
**The refusal, typed.** `formRoute(form, options?)` is `formBody` plus the 422 as a declared
|
|
113
|
+
response, so the answer is part of the route's type and of the document:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
new Elysia().post('/contact', ({ body }) => save(body), formRoute(contactForm, { registry }));
|
|
117
|
+
|
|
118
|
+
// an Eden client
|
|
119
|
+
const { error } = await api.contact.post(data);
|
|
120
|
+
if (error?.status === 422) error.value.issues[0].ref; // 'name' | 'age' | '$'
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`ref` is a field key, `lines[0].sku` for a repeater row, `$` for a script's own error
|
|
124
|
+
(`IssueRef`); a workflow's are `Form.key`. Spread it beside other options
|
|
125
|
+
(`{ ...formRoute(form), detail }`); a route that declares its own responses adds
|
|
126
|
+
`422: formIssues(form)` to them.
|
|
127
|
+
|
|
73
128
|
## Compatibility
|
|
74
129
|
|
|
75
130
|
Peers on [`@20baht/engine`](https://www.npmjs.com/package/@20baht/engine) of the same minor (the engine and the `FieldType` union come from there; see `peerDependencies`). It no longer peers on `@20baht/ui`, so a server installs no Lit. `@20baht/ui/engine` re-exports the same package: types and `setLocale` imported from either are the same ones. The `@20baht/validator/elysia` entry also peers, optionally, on `elysia` **>=1.4 <2**. All bahtui libraries except the stickers share one version number; keep them equal.
|
|
@@ -106,6 +161,21 @@ Peers on [`@20baht/engine`](https://www.npmjs.com/package/@20baht/engine) of the
|
|
|
106
161
|
| `validateFormData(form, data, registry?, locale?, options?)` | mirrors `<baht-form>.validate()` for a standalone form |
|
|
107
162
|
| `validateWorkflowData(workflow, data, registry?, locale?, options?)` | evaluates rules, then validates every visible form |
|
|
108
163
|
|
|
164
|
+
**Typed results.** With a schema declared by `defineForm` / `defineWorkflow`, the result is
|
|
165
|
+
typed from it: check `valid`, and `data` is `SubmissionData<typeof schema>`.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
const result = validateSubmission(contactForm, body);
|
|
169
|
+
if (!result.valid) return status(422, { issues: result.issues });
|
|
170
|
+
result.data.name; // string: `required` ruled the empty value out
|
|
171
|
+
result.data.age; // number | null
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
A form gives `ValidValues<F>` (required fields never null); a workflow `WorkflowValues<W>`, keyed
|
|
175
|
+
by form name with forms and fields optional, since a rule may hide either and may lift `required`;
|
|
176
|
+
`{ form: 'Name' }` that one form's flat data. A refused result's `data` is untyped. So is every
|
|
177
|
+
result of a schema typed `FormDefinition` / `WorkflowDefinition`, as before.
|
|
178
|
+
|
|
109
179
|
`options` is `{ strict?: boolean; form?: string; context?: NestedData }`. `strict` defaults to `true` (see above).
|
|
110
180
|
`form` (workflows only) validates `data` as the flat body of that one form of the workflow,
|
|
111
181
|
rules applied: issue refs are field keys and `data` is that form's own canonical data. It is
|
package/dist/common.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type FieldDescriptor, type FormDefinition, type NestedData, type WorkflowDefinition } from '@20baht/engine';
|
|
1
|
+
import { type FieldDescriptor, type FormDefinition, type NestedData, type ValidValues, type WorkflowDefinition, type WorkflowFormValues, type WorkflowValues } from '@20baht/engine';
|
|
2
2
|
export type SchemaDefinition = FormDefinition | WorkflowDefinition;
|
|
3
3
|
/** One violation. `ref` is the field key for forms, `form_name.key` for workflows. */
|
|
4
4
|
export interface ValidationIssue {
|
|
@@ -40,6 +40,78 @@ export interface ValidationResult {
|
|
|
40
40
|
*/
|
|
41
41
|
data: Record<string, unknown> | NestedData;
|
|
42
42
|
}
|
|
43
|
+
/** One form of a workflow, picked by name; a workflow typed wide has none to pick. */
|
|
44
|
+
type OneFormData<S extends {
|
|
45
|
+
readonly forms: readonly unknown[];
|
|
46
|
+
}, N extends string> = [
|
|
47
|
+
Extract<S['forms'][number], {
|
|
48
|
+
readonly form_name: N;
|
|
49
|
+
}>
|
|
50
|
+
] extends [infer F extends {
|
|
51
|
+
readonly fields: readonly unknown[];
|
|
52
|
+
}] ? WorkflowFormValues<F> : Record<string, unknown>;
|
|
53
|
+
/**
|
|
54
|
+
* The data a valid submission to schema `S` holds, validated with options `O`:
|
|
55
|
+
*
|
|
56
|
+
* - a form: `ValidValues<S>`, the empty value ruled out of its required fields;
|
|
57
|
+
* - a workflow: `WorkflowValues<S>`, keyed by form name, forms and fields optional (a rule
|
|
58
|
+
* may hide either);
|
|
59
|
+
* - a workflow with `{ form: 'Name' }`: that form's flat data, fields optional.
|
|
60
|
+
*
|
|
61
|
+
* Exact for a schema declared with `defineForm` / `defineWorkflow`. A schema typed
|
|
62
|
+
* `FormDefinition` / `WorkflowDefinition` (parsed JSON), or options whose `form` is not known
|
|
63
|
+
* until run time, give the untyped records.
|
|
64
|
+
*/
|
|
65
|
+
export type SubmissionData<S, O = Record<never, never>> = S extends {
|
|
66
|
+
readonly forms: readonly unknown[];
|
|
67
|
+
} ? O extends {
|
|
68
|
+
readonly form: infer N extends string;
|
|
69
|
+
} ? string extends N ? Record<string, unknown> : OneFormData<S, N> : 'form' extends keyof O ? Record<string, unknown> | NestedData : WorkflowValues<S> : S extends {
|
|
70
|
+
readonly fields: readonly unknown[];
|
|
71
|
+
} ? ValidValues<S> : Record<string, unknown> | NestedData;
|
|
72
|
+
/**
|
|
73
|
+
* `ValidationResult`, typed from the schema: check `valid` and `data` is `SubmissionData`.
|
|
74
|
+
* A refused result's `data` is what could be made of the body, and promises nothing.
|
|
75
|
+
*/
|
|
76
|
+
export type Validated<S, O = Record<never, never>> = {
|
|
77
|
+
valid: true;
|
|
78
|
+
issues: ValidationIssue[];
|
|
79
|
+
data: SubmissionData<S, O>;
|
|
80
|
+
} | {
|
|
81
|
+
valid: false;
|
|
82
|
+
issues: ValidationIssue[];
|
|
83
|
+
data: Record<string, unknown> | NestedData;
|
|
84
|
+
};
|
|
85
|
+
/** The refs an issue of one form may carry: a field, a repeater row's column, or `$` (a script). */
|
|
86
|
+
type FormRefs<F> = F extends {
|
|
87
|
+
readonly fields: readonly (infer D)[];
|
|
88
|
+
} ? (keyof ValidValues<F> & string) | (D extends {
|
|
89
|
+
readonly type: 'repeater';
|
|
90
|
+
readonly key: infer K extends string;
|
|
91
|
+
readonly fields: readonly (infer Column)[];
|
|
92
|
+
} ? Column extends {
|
|
93
|
+
readonly key: infer C extends string;
|
|
94
|
+
} ? `${K}[${number}].${C}` : never : never) | (F extends {
|
|
95
|
+
readonly kind: 'approval';
|
|
96
|
+
} ? 'decision' | 'comment' : never) | '$' : string;
|
|
97
|
+
/**
|
|
98
|
+
* The `ref` of an issue of schema `S`: a field key for a form (`lines[0].sku` for a repeater
|
|
99
|
+
* row, `$` for a script's own error), `Form.key` for a workflow, a field key again with
|
|
100
|
+
* `{ form: 'Name' }`. A schema typed wide gives `string`.
|
|
101
|
+
*/
|
|
102
|
+
export type IssueRef<S, O = Record<never, never>> = S extends {
|
|
103
|
+
readonly forms: readonly (infer F)[];
|
|
104
|
+
} ? string extends (F extends {
|
|
105
|
+
readonly form_name: infer N;
|
|
106
|
+
} ? N : never) ? string : O extends {
|
|
107
|
+
readonly form: infer N extends string;
|
|
108
|
+
} ? FormRefs<Extract<F, {
|
|
109
|
+
readonly form_name: N;
|
|
110
|
+
}>> : (F extends {
|
|
111
|
+
readonly form_name: infer N extends string;
|
|
112
|
+
} ? `${N}.${FormRefs<F>}` : never) | '$' : string extends keyof ValidValues<S & {
|
|
113
|
+
readonly fields: readonly unknown[];
|
|
114
|
+
}> ? string : FormRefs<S>;
|
|
43
115
|
/** `fn` in `locale`'s catalog, the previous one put back after: the engine's, shared with the form session. */
|
|
44
116
|
export { withLocale } from '@20baht/engine';
|
|
45
117
|
/** Static blocks (`label`, `section`): no value, never in the payload, never validated. */
|
package/dist/elysia.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Elysia } from 'elysia';
|
|
2
|
-
import { type SchemaDefinition, type ValidationIssue } from './index.js';
|
|
3
|
-
import type
|
|
2
|
+
import { type IssueRef, type SchemaDefinition, type SubmissionData, type ValidationIssue } from './index.js';
|
|
3
|
+
import { type JsonSchema, type Locale, type Registry } from '@20baht/engine';
|
|
4
4
|
/** What `bahtValidator()` is configured with, once per app. */
|
|
5
5
|
export interface ElysiaValidatorOptions {
|
|
6
6
|
/** The registry the client uses: custom validators, submit scripts, option fetchers, lookups. */
|
|
@@ -215,3 +215,90 @@ export declare function bahtValidator(options?: ElysiaValidatorOptions): Elysia<
|
|
|
215
215
|
standaloneSchema: {};
|
|
216
216
|
response: {};
|
|
217
217
|
}>;
|
|
218
|
+
/** The data a route gets for `target`: the schema's, or that of the one form `target` names. */
|
|
219
|
+
export type TargetData<T extends ValidateTarget> = T extends {
|
|
220
|
+
readonly schema: infer S;
|
|
221
|
+
readonly form: infer N;
|
|
222
|
+
} ? SubmissionData<S, {
|
|
223
|
+
readonly form: N;
|
|
224
|
+
}> : SubmissionData<T>;
|
|
225
|
+
/** A form or workflow as a route `body`: a Standard Schema (v1) that Elysia validates with. */
|
|
226
|
+
export interface FormBody<Data> {
|
|
227
|
+
readonly '~standard': {
|
|
228
|
+
readonly version: 1;
|
|
229
|
+
readonly vendor: '20baht';
|
|
230
|
+
readonly validate: (value: unknown) => Promise<{
|
|
231
|
+
value: Data;
|
|
232
|
+
}>;
|
|
233
|
+
readonly types?: {
|
|
234
|
+
readonly input: Data;
|
|
235
|
+
readonly output: Data;
|
|
236
|
+
};
|
|
237
|
+
/**
|
|
238
|
+
* The body as JSON Schema (Standard JSON Schema): what `@elysiajs/openapi` puts in the
|
|
239
|
+
* document. A description: see `jsonSchemaOf`. `target` is not read: the schema uses only
|
|
240
|
+
* what draft-07 and draft 2020-12 share.
|
|
241
|
+
*/
|
|
242
|
+
readonly jsonSchema: {
|
|
243
|
+
readonly input: (options?: {
|
|
244
|
+
readonly target?: string;
|
|
245
|
+
}) => JsonSchema;
|
|
246
|
+
readonly output: (options?: {
|
|
247
|
+
readonly target?: string;
|
|
248
|
+
}) => JsonSchema;
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* A form or workflow as the route's `body`, the way a `t.Object` is one:
|
|
254
|
+
*
|
|
255
|
+
* .post('/contact', ({ body }) => save(body), { body: formBody(contactForm, { registry }) })
|
|
256
|
+
*
|
|
257
|
+
* Elysia validates the request with it before the handler runs. A refused body answers
|
|
258
|
+
* `422 { issues }`, exactly as the `validate` option does; a valid one reaches the handler as
|
|
259
|
+
* `body`, the canonical data (defaults filled, hidden fields dropped, unknown keys gone), typed
|
|
260
|
+
* from the schema when it is declared with `defineForm` / `defineWorkflow` (`SubmissionData`).
|
|
261
|
+
* An Eden client of the route is asked for the same type, and an OpenAPI document
|
|
262
|
+
* (`@elysiajs/openapi`) describes the body from the form (`jsonSchemaOf`).
|
|
263
|
+
*
|
|
264
|
+
* Needs no plugin: the options `bahtValidator()` takes are the second argument. Elysia >= 1.4.
|
|
265
|
+
*/
|
|
266
|
+
export declare function formBody<const T extends ValidateTarget>(target: T, options?: ElysiaValidatorOptions): FormBody<TargetData<T>>;
|
|
267
|
+
/** The refs the issues of `target` may carry: see `IssueRef`. */
|
|
268
|
+
export type TargetRef<T extends ValidateTarget> = T extends {
|
|
269
|
+
readonly schema: infer S;
|
|
270
|
+
readonly form: infer N;
|
|
271
|
+
} ? IssueRef<S, {
|
|
272
|
+
readonly form: N;
|
|
273
|
+
}> : IssueRef<T>;
|
|
274
|
+
/** The 422 answer of a route whose body is `target`: each issue's `ref` is one of its fields. */
|
|
275
|
+
export interface FormFailure<T extends ValidateTarget> {
|
|
276
|
+
issues: {
|
|
277
|
+
ref: TargetRef<T>;
|
|
278
|
+
message: string;
|
|
279
|
+
}[];
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* The `422 { issues }` a `formBody` route answers, as a response schema, so the answer is part
|
|
283
|
+
* of the route's type: an Eden client reads `error.value.issues[0].ref` as one of the form's
|
|
284
|
+
* field keys, and an OpenAPI document lists the 422.
|
|
285
|
+
*
|
|
286
|
+
* { body: formBody(contactForm), response: { 422: formIssues(contactForm) } }
|
|
287
|
+
*
|
|
288
|
+
* It describes the answer and checks nothing: `formBody` is what makes it.
|
|
289
|
+
*/
|
|
290
|
+
export declare function formIssues<const T extends ValidateTarget>(_target: T): FormBody<FormFailure<T>>;
|
|
291
|
+
/**
|
|
292
|
+
* `formBody` and `formIssues` together, as the route's options:
|
|
293
|
+
*
|
|
294
|
+
* .post('/contact', ({ body }) => save(body), formRoute(contactForm, { registry }))
|
|
295
|
+
*
|
|
296
|
+
* Spread it beside other options (`{ ...formRoute(form), detail: { summary } }`). A route that
|
|
297
|
+
* declares responses of its own adds `422: formIssues(form)` to them instead.
|
|
298
|
+
*/
|
|
299
|
+
export declare function formRoute<const T extends ValidateTarget>(target: T, options?: ElysiaValidatorOptions): {
|
|
300
|
+
body: FormBody<TargetData<T>>;
|
|
301
|
+
response: {
|
|
302
|
+
422: FormBody<FormFailure<T>>;
|
|
303
|
+
};
|
|
304
|
+
};
|
package/dist/elysia.js
CHANGED
|
@@ -6,12 +6,17 @@
|
|
|
6
6
|
// .use(bahtValidator({ registry }))
|
|
7
7
|
// .post('/images', ({ validated }) => save(validated), { validate: importForm })
|
|
8
8
|
//
|
|
9
|
+
// or, typed from the form, the form as the route's `body`:
|
|
10
|
+
//
|
|
11
|
+
// .post('/images', ({ body }) => save(body), { body: formBody(importForm, { registry }) })
|
|
12
|
+
//
|
|
9
13
|
// A body the form refuses never reaches the handler: the route answers
|
|
10
14
|
// `422 { issues: [{ ref, message }] }`. The handler gets `validated`, the canonical data
|
|
11
15
|
// (defaults filled, hidden fields dropped): use it, never the raw `body`.
|
|
12
16
|
// ---------------------------------------------------------------------------
|
|
13
|
-
import { Elysia } from 'elysia';
|
|
17
|
+
import { Elysia, status } from 'elysia';
|
|
14
18
|
import { validateSubmission, verifyOptions, verifyReferences, } from './index.js';
|
|
19
|
+
import { jsonSchemaOf } from '@20baht/engine';
|
|
15
20
|
function split(target) {
|
|
16
21
|
return 'schema' in target
|
|
17
22
|
? { schema: target.schema, form: target.form }
|
|
@@ -54,3 +59,87 @@ export function bahtValidator(options = {}) {
|
|
|
54
59
|
}),
|
|
55
60
|
});
|
|
56
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* A form or workflow as the route's `body`, the way a `t.Object` is one:
|
|
64
|
+
*
|
|
65
|
+
* .post('/contact', ({ body }) => save(body), { body: formBody(contactForm, { registry }) })
|
|
66
|
+
*
|
|
67
|
+
* Elysia validates the request with it before the handler runs. A refused body answers
|
|
68
|
+
* `422 { issues }`, exactly as the `validate` option does; a valid one reaches the handler as
|
|
69
|
+
* `body`, the canonical data (defaults filled, hidden fields dropped, unknown keys gone), typed
|
|
70
|
+
* from the schema when it is declared with `defineForm` / `defineWorkflow` (`SubmissionData`).
|
|
71
|
+
* An Eden client of the route is asked for the same type, and an OpenAPI document
|
|
72
|
+
* (`@elysiajs/openapi`) describes the body from the form (`jsonSchemaOf`).
|
|
73
|
+
*
|
|
74
|
+
* Needs no plugin: the options `bahtValidator()` takes are the second argument. Elysia >= 1.4.
|
|
75
|
+
*/
|
|
76
|
+
export function formBody(target, options = {}) {
|
|
77
|
+
const { schema, form } = split(target);
|
|
78
|
+
const describe = (io) => () => jsonSchemaOf(schema, { io, ...(form === undefined ? {} : { form }) });
|
|
79
|
+
return {
|
|
80
|
+
'~standard': {
|
|
81
|
+
version: 1,
|
|
82
|
+
vendor: '20baht',
|
|
83
|
+
async validate(value) {
|
|
84
|
+
const { data, issues } = await check(target, value, options);
|
|
85
|
+
// thrown, not returned as Standard Schema issues: the answer is this package's 422
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/only-throw-error -- Elysia answers with a thrown `status()`; an Error would be a 500
|
|
87
|
+
if (issues.length > 0)
|
|
88
|
+
throw status(422, { issues });
|
|
89
|
+
// the validator's promise: valid data has the shape the schema's types describe
|
|
90
|
+
return { value: data };
|
|
91
|
+
},
|
|
92
|
+
jsonSchema: { input: describe('input'), output: describe('output') },
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const ISSUES_SCHEMA = {
|
|
97
|
+
type: 'object',
|
|
98
|
+
title: 'The body was refused',
|
|
99
|
+
properties: {
|
|
100
|
+
issues: {
|
|
101
|
+
type: 'array',
|
|
102
|
+
items: {
|
|
103
|
+
type: 'object',
|
|
104
|
+
properties: {
|
|
105
|
+
ref: { type: 'string', description: 'The field at fault' },
|
|
106
|
+
message: { type: 'string' },
|
|
107
|
+
},
|
|
108
|
+
required: ['ref', 'message'],
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
required: ['issues'],
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* The `422 { issues }` a `formBody` route answers, as a response schema, so the answer is part
|
|
116
|
+
* of the route's type: an Eden client reads `error.value.issues[0].ref` as one of the form's
|
|
117
|
+
* field keys, and an OpenAPI document lists the 422.
|
|
118
|
+
*
|
|
119
|
+
* { body: formBody(contactForm), response: { 422: formIssues(contactForm) } }
|
|
120
|
+
*
|
|
121
|
+
* It describes the answer and checks nothing: `formBody` is what makes it.
|
|
122
|
+
*/
|
|
123
|
+
export function formIssues(_target) {
|
|
124
|
+
const describe = () => ISSUES_SCHEMA;
|
|
125
|
+
return {
|
|
126
|
+
'~standard': {
|
|
127
|
+
version: 1,
|
|
128
|
+
vendor: '20baht',
|
|
129
|
+
// the answer is this package's own: there is nothing of a client's to check in it
|
|
130
|
+
validate: (value) => Promise.resolve({ value: value }),
|
|
131
|
+
jsonSchema: { input: describe, output: describe },
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* `formBody` and `formIssues` together, as the route's options:
|
|
137
|
+
*
|
|
138
|
+
* .post('/contact', ({ body }) => save(body), formRoute(contactForm, { registry }))
|
|
139
|
+
*
|
|
140
|
+
* Spread it beside other options (`{ ...formRoute(form), detail: { summary } }`). A route that
|
|
141
|
+
* declares responses of its own adds `422: formIssues(form)` to them instead.
|
|
142
|
+
*/
|
|
143
|
+
export function formRoute(target, options = {}) {
|
|
144
|
+
return { body: formBody(target, options), response: { 422: formIssues(target) } };
|
|
145
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { SchemaDefinition, ValidateOptions, ValidationIssue, ValidationResult, } from './common.js';
|
|
1
|
+
export type { SchemaDefinition, IssueRef, SubmissionData, ValidateOptions, Validated, ValidationIssue, ValidationResult, } from './common.js';
|
|
2
2
|
export { isFormDefinition, isWorkflowDefinition } from './common.js';
|
|
3
3
|
export { validateFormData, validateSubmission, validateWorkflowData } from './submission.js';
|
|
4
4
|
export { verifyOptions, verifyReferences, type VerifyOptions } from './verify.js';
|
package/dist/submission.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type FormDefinition, type Locale, type Registry, type WorkflowDefinition } from '@20baht/engine';
|
|
2
|
-
import { type SchemaDefinition, type ValidateOptions, type
|
|
2
|
+
import { type SchemaDefinition, type ValidateOptions, type Validated } from './common.js';
|
|
3
3
|
/**
|
|
4
4
|
* Validate a standalone form submission — mirrors `<baht-form>.validate()`
|
|
5
5
|
* with no derived field states: every non-label field, missing values fall
|
|
@@ -7,7 +7,7 @@ import { type SchemaDefinition, type ValidateOptions, type ValidationResult } fr
|
|
|
7
7
|
* views are client-side display only, the server always validates the full
|
|
8
8
|
* form (a view can never weaken a validation rule).
|
|
9
9
|
*/
|
|
10
|
-
export declare function validateFormData(form:
|
|
10
|
+
export declare function validateFormData<S extends FormDefinition, const O extends ValidateOptions = Record<never, never>>(form: S, input: unknown, registry?: Registry, locale?: Locale, options?: O): Validated<S, O>;
|
|
11
11
|
/**
|
|
12
12
|
* Validate a whole workflow submission — mirrors what `<baht-workflow>`
|
|
13
13
|
* enforces across its steps: rules are evaluated first (`evaluate`), invisible
|
|
@@ -17,7 +17,7 @@ export declare function validateFormData(form: FormDefinition, input: unknown, r
|
|
|
17
17
|
* client fires `baht-complete { status: 'rejected' }` immediately, so later
|
|
18
18
|
* forms are never visited.
|
|
19
19
|
*/
|
|
20
|
-
export declare function validateWorkflowData(workflow:
|
|
20
|
+
export declare function validateWorkflowData<S extends WorkflowDefinition, const O extends ValidateOptions = Record<never, never>>(workflow: S, input: unknown, registry?: Registry, locale?: Locale, options?: O): Validated<S, O>;
|
|
21
21
|
/**
|
|
22
22
|
* Validate a submission body's `data` against whichever schema kind `id`
|
|
23
23
|
* resolved to. This is the single server entry point:
|
|
@@ -25,4 +25,4 @@ export declare function validateWorkflowData(workflow: WorkflowDefinition, input
|
|
|
25
25
|
* const schema = store.get(body.id);
|
|
26
26
|
* const result = validateSubmission(schema, body.data);
|
|
27
27
|
*/
|
|
28
|
-
export declare function validateSubmission(schema:
|
|
28
|
+
export declare function validateSubmission<S extends SchemaDefinition, const O extends ValidateOptions = Record<never, never>>(schema: S, data: unknown, registry?: Registry, locale?: Locale, options?: O): Validated<S, O>;
|
package/dist/submission.js
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
import { approvalCommentRequired, approvalFields, defaultValue, evaluate, hasScripts, UNSAFE_KEYS, t, isBlank, submitScriptErrors, validateField, validateShownField, visibleForms, walkPath, } from '@20baht/engine';
|
|
2
2
|
import { isFormDefinition, isRecord, isStatic, isWorkflowDefinition, ownEntry, sorted, withLocale, } from './common.js';
|
|
3
3
|
import { normalizeCanonicalValue, shapeIssues, withNormalizedTags } from './shape.js';
|
|
4
|
-
// ---- form validation -----------------------------------------------------------
|
|
5
|
-
/**
|
|
6
|
-
* Validate a standalone form submission — mirrors `<baht-form>.validate()`
|
|
7
|
-
* with no derived field states: every non-label field, missing values fall
|
|
8
|
-
* back to the field default before checking. `form.views` are ignored here:
|
|
9
|
-
* views are client-side display only, the server always validates the full
|
|
10
|
-
* form (a view can never weaken a validation rule).
|
|
11
|
-
*/
|
|
12
4
|
export function validateFormData(form, input, registry, locale, options) {
|
|
13
5
|
const reg = serverRegistry([form], registry, options);
|
|
14
6
|
return withLocale(locale, () => formDataResult(form, input, reg));
|
|
@@ -136,15 +128,6 @@ function submitScriptIssues(form, data, registry, refPrefix = '') {
|
|
|
136
128
|
});
|
|
137
129
|
return errors.map((err) => ({ ref: `${refPrefix}$`, message: err.message }));
|
|
138
130
|
}
|
|
139
|
-
/**
|
|
140
|
-
* Validate a whole workflow submission — mirrors what `<baht-workflow>`
|
|
141
|
-
* enforces across its steps: rules are evaluated first (`evaluate`), invisible
|
|
142
|
-
* forms/fields are excluded, rule-derived `required` overrides the descriptor,
|
|
143
|
-
* approval steps demand a decision plus their comment policy, and a rejected
|
|
144
|
-
* approval with `on_reject: end` (the default) stops validation there — the
|
|
145
|
-
* client fires `baht-complete { status: 'rejected' }` immediately, so later
|
|
146
|
-
* forms are never visited.
|
|
147
|
-
*/
|
|
148
131
|
export function validateWorkflowData(workflow, input, registry, locale, options) {
|
|
149
132
|
const reg = serverRegistry(workflow.forms, registry, options);
|
|
150
133
|
const form = options?.form;
|
|
@@ -337,13 +320,6 @@ function approvalIssues(form, data) {
|
|
|
337
320
|
}
|
|
338
321
|
return issues;
|
|
339
322
|
}
|
|
340
|
-
/**
|
|
341
|
-
* Validate a submission body's `data` against whichever schema kind `id`
|
|
342
|
-
* resolved to. This is the single server entry point:
|
|
343
|
-
*
|
|
344
|
-
* const schema = store.get(body.id);
|
|
345
|
-
* const result = validateSubmission(schema, body.data);
|
|
346
|
-
*/
|
|
347
323
|
export function validateSubmission(schema, data, registry, locale, options) {
|
|
348
324
|
if (isWorkflowDefinition(schema)) {
|
|
349
325
|
return validateWorkflowData(schema, data, registry, locale, options);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@20baht/validator",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20.19.0"
|
|
@@ -51,12 +51,13 @@
|
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@20baht/engine": "workspace:*",
|
|
53
53
|
"@20baht/formats": "workspace:*",
|
|
54
|
+
"@elysiajs/openapi": "^1.4.16",
|
|
54
55
|
"elysia": "^1.4.0",
|
|
55
56
|
"typescript": "^5.9.3",
|
|
56
57
|
"vitest": "^3.2.4"
|
|
57
58
|
},
|
|
58
59
|
"peerDependencies": {
|
|
59
|
-
"@20baht/engine": ">=0.3.
|
|
60
|
+
"@20baht/engine": ">=0.3.3 <0.4.0",
|
|
60
61
|
"elysia": ">=1.4.0 <2.0.0"
|
|
61
62
|
},
|
|
62
63
|
"peerDependenciesMeta": {
|