@kayjs/kay 1.106.0 → 1.108.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 -2
- package/dist/eslint.js +477 -204
- package/editors/zed/extension.toml +2 -2
- package/llms/action-protection.md +1 -1
- package/llms/actions-from-islands.md +1 -1
- package/llms/actions-refused-fields.md +22 -0
- package/llms/actions.md +2 -22
- package/llms/browser-only.md +1 -1
- package/llms/compiler-api.md +1 -1
- package/llms/component-libraries.md +1 -1
- package/llms/components.md +1 -1
- package/llms/deprecated-lints.md +2 -4
- package/llms/island-and-form-lints.md +1 -0
- package/llms/islands.md +11 -2
- package/llms/project-new-and-add.md +1 -1
- package/llms/project.md +2 -2
- package/llms/schemas.md +6 -4
- package/llms/upgrading.md +25 -1
- package/llms/whats-new-archive.md +36 -1
- package/llms/whats-new.md +18 -35
- package/llms.txt +1 -1
- package/package.json +15 -6
- package/src/build/client.ts +23 -26
- package/src/build/entry-files.ts +19 -0
- package/src/build/icon-slots.ts +140 -0
- package/src/build/lazy-components.ts +130 -0
- package/src/build/lazy-manifest.ts +56 -0
- package/src/build/lazy-stubs.ts +31 -0
- package/src/build/libraries.ts +24 -2
- package/src/build/plugin.ts +13 -1
- package/src/build/runtime-chunk.ts +3 -3
- package/src/build/source-files.ts +8 -3
- package/src/build/transform.ts +17 -7
- package/src/build/variants.ts +97 -5
- package/src/check/action-options.ts +56 -0
- package/src/check/project.ts +4 -4
- package/src/check/renamed-packages.ts +3 -63
- package/src/check/schema-json.ts +226 -0
- package/src/check/shadow-types.ts +15 -27
- package/src/compiler/codegen.ts +7 -17
- package/src/compiler/dead-code.ts +22 -0
- package/src/compiler/deprecated-lints-1-105.ts +41 -0
- package/src/compiler/deprecated-lints-1-106.ts +1 -1
- package/src/compiler/deprecated-lints-1-107.ts +14 -0
- package/src/compiler/deprecated-lints.ts +8 -65
- package/src/compiler/dom-property.ts +23 -0
- package/src/compiler/dom.ts +15 -15
- package/src/compiler/import-edits.ts +15 -0
- package/src/compiler/index.ts +8 -3
- package/src/compiler/island-declaration.ts +15 -0
- package/src/compiler/kay-exports.ts +1 -0
- package/src/compiler/lazy-imports.ts +357 -0
- package/src/compiler/parser.ts +2 -2
- package/src/compiler/route-lints.ts +2 -0
- package/src/compiler/schema-lints.ts +13 -0
- package/src/compiler/script-server.ts +6 -3
- package/src/content/validate.ts +1 -1
- package/src/new.ts +6 -2
- package/src/package-names.ts +1 -1
- package/src/runtime/action-call.ts +39 -327
- package/src/runtime/action-run.ts +357 -0
- package/src/runtime/action.ts +47 -4
- package/src/runtime/client.ts +10 -0
- package/src/runtime/dom-lazy.ts +276 -0
- package/src/runtime/field-errors.ts +58 -0
- package/src/runtime/field-refusal.ts +179 -0
- package/src/runtime/form.ts +3 -2
- package/src/runtime/html.ts +6 -1
- package/src/runtime/index.ts +2 -0
- package/src/runtime/island.server.ts +36 -2
- package/src/runtime/schema-record.ts +2 -1
- package/src/runtime/schema.client.ts +104 -14
- package/src/runtime/schema.ts +9 -2
- package/src/runtime/standard-schema.ts +32 -0
- package/src/runtime/validate-run.client.ts +179 -0
- package/src/runtime/validate.client.ts +11 -0
- package/src/runtime/validate.ts +4 -137
- package/src/server/actions.ts +48 -43
- package/src/server/answer.ts +102 -0
- package/src/server/api-input.ts +166 -0
- package/src/server/api.ts +12 -44
- package/src/server/app-stages.ts +15 -8
- package/src/server/app.ts +5 -2
- package/src/server/client.ts +2 -0
- package/src/server/dev.ts +7 -9
- package/src/server/file-limit.ts +7 -1
- package/src/server/forms.ts +1 -1
- package/src/server/host-allowlist.ts +7 -7
- package/src/server/index.ts +2 -0
- package/src/server/kay-routes.ts +221 -0
- package/src/server/kay-server.ts +228 -0
- package/src/server/kept-exports.ts +1 -0
- package/src/server/library.ts +0 -2
- package/src/server/mcp.ts +1 -1
- package/src/server/openapi.ts +12 -3
- package/src/server/phases.ts +249 -0
- package/src/server/protection.ts +6 -6
- package/src/server/proxy-key.ts +8 -8
- package/src/server/query.ts +4 -25
- package/src/server/receive.ts +43 -0
- package/src/server/render-document.ts +13 -3
- package/src/server/request-context.ts +137 -0
- package/src/server/schema-check.ts +1 -1
- package/src/server/socket.ts +19 -47
- package/src/server/stream.ts +1 -1
- package/src/server/validate.ts +346 -0
- package/src/test/mount.ts +6 -2
- package/types/build/client.d.ts +3 -2
- package/types/build/client.d.ts.map +1 -1
- package/types/build/entry-files.d.ts +4 -0
- package/types/build/entry-files.d.ts.map +1 -0
- package/types/build/icon-slots.d.ts +7 -0
- package/types/build/icon-slots.d.ts.map +1 -0
- package/types/build/lazy-components.d.ts +6 -0
- package/types/build/lazy-components.d.ts.map +1 -0
- package/types/build/lazy-manifest.d.ts +19 -0
- package/types/build/lazy-manifest.d.ts.map +1 -0
- package/types/build/lazy-stubs.d.ts +5 -0
- package/types/build/lazy-stubs.d.ts.map +1 -0
- package/types/build/libraries.d.ts +2 -0
- package/types/build/libraries.d.ts.map +1 -1
- package/types/build/plugin.d.ts +2 -0
- package/types/build/plugin.d.ts.map +1 -1
- package/types/build/source-files.d.ts.map +1 -1
- package/types/build/transform.d.ts +3 -1
- package/types/build/transform.d.ts.map +1 -1
- package/types/build/variants.d.ts.map +1 -1
- package/types/check/action-options.d.ts +3 -0
- package/types/check/action-options.d.ts.map +1 -0
- package/types/check/renamed-packages.d.ts +0 -3
- package/types/check/renamed-packages.d.ts.map +1 -1
- package/types/check/schema-json.d.ts +3 -0
- package/types/check/schema-json.d.ts.map +1 -0
- package/types/check/shadow-types.d.ts.map +1 -1
- package/types/compiler/codegen.d.ts +2 -0
- package/types/compiler/codegen.d.ts.map +1 -1
- package/types/compiler/dead-code.d.ts +9 -0
- package/types/compiler/dead-code.d.ts.map +1 -0
- package/types/compiler/deprecated-lints-1-105.d.ts +39 -0
- package/types/compiler/deprecated-lints-1-105.d.ts.map +1 -0
- package/types/compiler/deprecated-lints-1-106.d.ts +1 -1
- package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -1
- package/types/compiler/deprecated-lints-1-107.d.ts +12 -0
- package/types/compiler/deprecated-lints-1-107.d.ts.map +1 -0
- package/types/compiler/deprecated-lints.d.ts +36 -53
- package/types/compiler/deprecated-lints.d.ts.map +1 -1
- package/types/compiler/dom-property.d.ts +9 -0
- package/types/compiler/dom-property.d.ts.map +1 -0
- package/types/compiler/dom.d.ts +1 -0
- package/types/compiler/dom.d.ts.map +1 -1
- package/types/compiler/import-edits.d.ts +1 -0
- package/types/compiler/import-edits.d.ts.map +1 -1
- package/types/compiler/index.d.ts +2 -0
- package/types/compiler/index.d.ts.map +1 -1
- package/types/compiler/island-declaration.d.ts +5 -0
- package/types/compiler/island-declaration.d.ts.map +1 -1
- package/types/compiler/kay-exports.d.ts.map +1 -1
- package/types/compiler/lazy-imports.d.ts +33 -0
- package/types/compiler/lazy-imports.d.ts.map +1 -0
- package/types/compiler/lints.d.ts +44 -53
- package/types/compiler/lints.d.ts.map +1 -1
- package/types/compiler/route-lints.d.ts +8 -0
- package/types/compiler/route-lints.d.ts.map +1 -1
- package/types/compiler/schema-lints.d.ts +11 -0
- package/types/compiler/schema-lints.d.ts.map +1 -0
- package/types/compiler/script-server.d.ts.map +1 -1
- package/types/new.d.ts +1 -0
- package/types/new.d.ts.map +1 -1
- package/types/runtime/action-call.d.ts.map +1 -1
- package/types/runtime/action-run.d.ts +23 -0
- package/types/runtime/action-run.d.ts.map +1 -0
- package/types/runtime/action.d.ts +26 -1
- package/types/runtime/action.d.ts.map +1 -1
- package/types/runtime/client.d.ts.map +1 -1
- package/types/runtime/dom-lazy.d.ts +6 -0
- package/types/runtime/dom-lazy.d.ts.map +1 -0
- package/types/runtime/field-errors.d.ts +4 -0
- package/types/runtime/field-errors.d.ts.map +1 -0
- package/types/runtime/field-refusal.d.ts +37 -0
- package/types/runtime/field-refusal.d.ts.map +1 -0
- package/types/runtime/form.d.ts +1 -1
- package/types/runtime/form.d.ts.map +1 -1
- package/types/runtime/html.d.ts.map +1 -1
- package/types/runtime/index.d.ts +2 -0
- package/types/runtime/index.d.ts.map +1 -1
- package/types/runtime/island.server.d.ts +2 -0
- package/types/runtime/island.server.d.ts.map +1 -1
- package/types/runtime/schema-record.d.ts.map +1 -1
- package/types/runtime/schema.client.d.ts.map +1 -1
- package/types/runtime/schema.d.ts +6 -1
- package/types/runtime/schema.d.ts.map +1 -1
- package/types/runtime/standard-schema.d.ts +37 -0
- package/types/runtime/standard-schema.d.ts.map +1 -0
- package/types/runtime/validate-run.client.d.ts +4 -0
- package/types/runtime/validate-run.client.d.ts.map +1 -0
- package/types/runtime/validate.client.d.ts +4 -0
- package/types/runtime/validate.client.d.ts.map +1 -0
- package/types/runtime/validate.d.ts +1 -21
- package/types/runtime/validate.d.ts.map +1 -1
- package/types/server/actions.d.ts.map +1 -1
- package/types/server/answer.d.ts +6 -0
- package/types/server/answer.d.ts.map +1 -0
- package/types/server/api-input.d.ts +19 -0
- package/types/server/api-input.d.ts.map +1 -0
- package/types/server/api.d.ts.map +1 -1
- package/types/server/app-stages.d.ts +4 -1
- package/types/server/app-stages.d.ts.map +1 -1
- package/types/server/app.d.ts +2 -0
- package/types/server/app.d.ts.map +1 -1
- package/types/server/client.d.ts +1 -0
- package/types/server/client.d.ts.map +1 -1
- package/types/server/dev.d.ts +1 -1
- package/types/server/dev.d.ts.map +1 -1
- package/types/server/file-limit.d.ts.map +1 -1
- package/types/server/host-allowlist.d.ts +3 -3
- package/types/server/host-allowlist.d.ts.map +1 -1
- package/types/server/index.d.ts +2 -0
- package/types/server/index.d.ts.map +1 -1
- package/types/server/kay-routes.d.ts +47 -0
- package/types/server/kay-routes.d.ts.map +1 -0
- package/types/server/kay-server.d.ts +25 -0
- package/types/server/kay-server.d.ts.map +1 -0
- package/types/server/kept-exports.d.ts.map +1 -1
- package/types/server/library.d.ts +0 -1
- package/types/server/library.d.ts.map +1 -1
- package/types/server/openapi.d.ts.map +1 -1
- package/types/server/phases.d.ts +36 -0
- package/types/server/phases.d.ts.map +1 -0
- package/types/server/protection.d.ts +3 -2
- package/types/server/protection.d.ts.map +1 -1
- package/types/server/proxy-key.d.ts +3 -3
- package/types/server/proxy-key.d.ts.map +1 -1
- package/types/server/query.d.ts.map +1 -1
- package/types/server/receive.d.ts +9 -0
- package/types/server/receive.d.ts.map +1 -0
- package/types/server/render-document.d.ts.map +1 -1
- package/types/server/request-context.d.ts +29 -0
- package/types/server/request-context.d.ts.map +1 -0
- package/types/server/schema-check.d.ts +1 -0
- package/types/server/schema-check.d.ts.map +1 -1
- package/types/server/socket.d.ts +3 -5
- package/types/server/socket.d.ts.map +1 -1
- package/types/server/validate.d.ts +26 -0
- package/types/server/validate.d.ts.map +1 -0
- package/types/test/mount.d.ts.map +1 -1
- package/src/check/schema-type.ts +0 -9
- package/src/compiler/deprecated-script-fence.ts +0 -26
- package/src/server/field-errors.ts +0 -136
- package/types/check/schema-type.d.ts +0 -3
- package/types/check/schema-type.d.ts.map +0 -1
- package/types/compiler/deprecated-script-fence.d.ts +0 -3
- package/types/compiler/deprecated-script-fence.d.ts.map +0 -1
- package/types/server/field-errors.d.ts +0 -6
- package/types/server/field-errors.d.ts.map +0 -1
|
@@ -1,24 +1,114 @@
|
|
|
1
1
|
import type { s as server } from './schema'
|
|
2
|
-
import { KayError } from './kay-error'
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
// The symbols TypeBox reads a schema and an optional field by, which `isSchema` and `schemaOf` read here as well.
|
|
4
|
+
const KIND = Symbol.for('TypeBox.Kind')
|
|
5
|
+
const OPTIONAL = Symbol.for('TypeBox.Optional')
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const schema: object = /* @__PURE__ */ new Proxy({}, {
|
|
9
|
-
get: (_, key) => {
|
|
10
|
-
if (typeof key === 'symbol' || key === 'then' || key === 'toJSON')
|
|
11
|
-
return undefined
|
|
7
|
+
type Node = Record<PropertyKey, unknown>
|
|
8
|
+
type Options = Record<string, unknown> | undefined
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
const DEFAULT_MAX_LENGTH = 10_000
|
|
11
|
+
const DEFAULT_MAX_ITEMS = 1000
|
|
12
|
+
const SLUG = '^[a-z0-9]+(?:-[a-z0-9]+)*$'
|
|
13
|
+
const FIELD_KEYS = new Set(['optional', 'default', 'error', 'label', 'description', 'example'])
|
|
14
|
+
|
|
15
|
+
// The JSON Schema a builder stands for, the same keywords the server's builders write, so a schema shared with an island is read by `validate` there: the chunk of the validator is in the page only where `validate` is called, and a module that only builds its schemas costs these few functions.
|
|
16
|
+
function node(kind: string, keywords: Node, options: Options): Node {
|
|
17
|
+
const field: Node = { [KIND]: kind, ...keywords }
|
|
18
|
+
const { default: fallback, error, label, description, example } = options ?? {}
|
|
19
|
+
|
|
20
|
+
if (fallback !== undefined)
|
|
21
|
+
field.default = fallback
|
|
22
|
+
|
|
23
|
+
if (error !== undefined)
|
|
24
|
+
field.error = error
|
|
25
|
+
|
|
26
|
+
if (label !== undefined)
|
|
27
|
+
field.title = label
|
|
28
|
+
|
|
29
|
+
if (description !== undefined)
|
|
30
|
+
field.description = description
|
|
31
|
+
|
|
32
|
+
if (example !== undefined)
|
|
33
|
+
field.examples = [example]
|
|
34
|
+
|
|
35
|
+
for (const [key, value] of Object.entries(options ?? {})) {
|
|
36
|
+
if (!FIELD_KEYS.has(key) && !(key in field) && !['min', 'max', 'pattern', 'extra', 'minSize', 'maxSize', 'type'].includes(key))
|
|
37
|
+
field[key] = value
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (options?.optional === true)
|
|
41
|
+
field[OPTIONAL] = 'Optional'
|
|
42
|
+
|
|
43
|
+
return field
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function text(options: Options, extra: Node = {}): Node {
|
|
47
|
+
const pattern = options?.pattern instanceof RegExp ? options.pattern.source : options?.pattern
|
|
48
|
+
const max = options?.max ?? options?.maxLength
|
|
16
49
|
|
|
17
|
-
|
|
50
|
+
return { type: 'string', ...extra, ...options?.min !== undefined && { minLength: options.min }, maxLength: max ?? DEFAULT_MAX_LENGTH, ...pattern !== undefined && { pattern } }
|
|
51
|
+
}
|
|
18
52
|
|
|
19
|
-
|
|
53
|
+
function numbers(type: string, options: Options): Node {
|
|
54
|
+
return { type, ...options?.min !== undefined && { minimum: options.min }, ...options?.max !== undefined && { maximum: options.max } }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function objectOf(shape: Record<string, Node>, options: Options): Node {
|
|
58
|
+
const required = Object.keys(shape).filter(name => !(OPTIONAL in shape[name]!))
|
|
59
|
+
|
|
60
|
+
return node('Object', {
|
|
61
|
+
type: 'object',
|
|
62
|
+
properties: { ...shape },
|
|
63
|
+
...required.length > 0 && { required },
|
|
64
|
+
...options?.extra === 'strip' ? { 'x-kay-extra': 'strip' } : { additionalProperties: false },
|
|
65
|
+
}, options)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function enumeration(source: readonly unknown[] | object, options: Options): Node {
|
|
69
|
+
const values = Array.isArray(source) ? source : Object.keys(source)
|
|
70
|
+
const labels = Array.isArray(source) ? [] : Object.entries(source).filter(([, label]) => typeof label === 'string')
|
|
71
|
+
|
|
72
|
+
return node('Union', { type: typeof values[0] === 'number' ? 'number' : 'string', enum: values, ...labels.length > 0 && { 'x-labels': Object.fromEntries(labels) } }, options)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function filesOf(options: Options, many: boolean): Node {
|
|
76
|
+
const item = { type: 'string', format: 'binary', ...options?.type !== undefined && { extension: options.type }, ...options?.minSize !== undefined && { minSize: options.minSize }, ...options?.maxSize !== undefined && { maxSize: options.maxSize } }
|
|
77
|
+
|
|
78
|
+
return many ? { type: 'array', items: item, ...options?.min !== undefined && { minItems: options.min }, ...options?.max !== undefined && { maxItems: options.max } } : item
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const builders: Record<string, (...args: never[]) => Node> = {
|
|
82
|
+
string: (options?: Options) => node('String', text(options), options),
|
|
83
|
+
email: (options?: Options) => node('String', text({ max: 254, ...options }, { format: 'email' }), options),
|
|
84
|
+
url: (options?: Options) => node('String', text({ max: 2048, ...options }, { format: 'url' }), options),
|
|
85
|
+
slug: (options?: Options) => node('String', text({ max: 200, ...options, pattern: SLUG }), options),
|
|
86
|
+
secret: (options?: Options) => node('String', text({ ...options, min: Math.max(32, (options?.min as number | undefined) ?? 0) }, { secret: true }), options),
|
|
87
|
+
int: (options?: Options) => node('Integer', numbers('integer', options), options),
|
|
88
|
+
number: (options?: Options) => node('Number', numbers('number', options), options),
|
|
89
|
+
boolean: (options?: Options) => node('Boolean', { type: 'boolean' }, options),
|
|
90
|
+
date: (options?: Options) => node('Date', { type: 'string', format: 'date-time' }, options),
|
|
91
|
+
enum: enumeration,
|
|
92
|
+
literal: (value: unknown, options?: Options) => node('Literal', { const: value }, options),
|
|
93
|
+
union: (members: Node[], options?: Options) => node('Union', { anyOf: [...members] }, options),
|
|
94
|
+
object: objectOf,
|
|
95
|
+
record: (value: Node, options?: Options) => node('Record', { type: 'object', additionalProperties: value }, options),
|
|
96
|
+
list: (item: Node, options?: Options) => node('Array', { type: 'array', items: item, ...options?.min !== undefined && { minItems: options.min }, ...(options?.max ?? options?.maxItems) !== undefined ? { maxItems: options?.max ?? options?.maxItems } : item.format !== 'binary' && { maxItems: DEFAULT_MAX_ITEMS } }, options),
|
|
97
|
+
file: (options?: Options) => node('File', filesOf(options, false), options),
|
|
98
|
+
files: (options?: Options) => node('Files', filesOf(options, true), options),
|
|
99
|
+
public: (field: Node) => ({ ...field, public: true }),
|
|
100
|
+
Optional: (field: Node) => ({ ...field, [OPTIONAL]: 'Optional' }),
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The builders of 1.105 and before reach the browser as an opaque schema that accepts anything, so a module shared with an island still loads.
|
|
104
|
+
const opaque = (): Node => ({ [KIND]: 'Any' })
|
|
105
|
+
|
|
106
|
+
// The browser's `s`: it builds the plain JSON Schema `validate` reads, and carries no Elysia, which would weigh about 190 KB in every island. Unused, a bundler drops it.
|
|
20
107
|
export const s = /* @__PURE__ */ new Proxy({}, {
|
|
21
108
|
get: (_, name) => {
|
|
22
|
-
|
|
109
|
+
if (typeof name === 'symbol' || name === 'then')
|
|
110
|
+
return undefined
|
|
111
|
+
|
|
112
|
+
return Object.hasOwn(builders, name) ? builders[name] : opaque
|
|
23
113
|
},
|
|
24
114
|
}) as typeof server
|
package/src/runtime/schema.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { TSchema, Static as TypeBoxStatic } from 'elysia'
|
|
2
2
|
import type { ByteSize } from './byte-size'
|
|
3
|
+
import type { StandardOutput, StandardSchema } from './standard-schema'
|
|
3
4
|
import { t as schema } from 'elysia'
|
|
4
5
|
import { FormatRegistry } from 'elysia/type-system'
|
|
5
6
|
import { byteSize } from './byte-size'
|
|
@@ -15,6 +16,9 @@ export interface Schema extends TSchema {}
|
|
|
15
16
|
// The value a schema describes. Kept as `Static`, the name an app already reads it by, since a second name would only duplicate it.
|
|
16
17
|
export type Static<Type extends Schema, Params extends unknown[] = []> = TypeBoxStatic<Type, Params>
|
|
17
18
|
|
|
19
|
+
// A Standard Schema validator (Zod, ArkType, Valibot) typed as a schema whose value is the validator's output, which is what `Static` reads of one.
|
|
20
|
+
export type StandardAs<Validator extends StandardSchema> = Schema & { static: StandardOutput<Validator> }
|
|
21
|
+
|
|
18
22
|
// The `email` format is kay's own check, the one the browser, auth and mail share, in place of Elysia's.
|
|
19
23
|
FormatRegistry.Set('email', isEmail)
|
|
20
24
|
FormatRegistry.Set('url', isUrl)
|
|
@@ -41,11 +45,14 @@ export const DEFAULT_MAX_ITEMS = 1000
|
|
|
41
45
|
|
|
42
46
|
const SLUG = '^[a-z0-9]+(?:-[a-z0-9]+)*$'
|
|
43
47
|
|
|
48
|
+
// The words of a refusal: a text, a function called in the request's locale, or a record by the rule that failed (`min`, `max`, `pattern`, `format`, `required`, `type`), with a `default` for the others.
|
|
49
|
+
export type ErrorText = string | (() => string) | Record<string, string | (() => string) | undefined>
|
|
50
|
+
|
|
44
51
|
// What every builder takes beside its own options: the field is `optional`, takes a `default` when the value is left out, words its refusal with `error` (a text or a function called in the request's locale), and carries a `label`, a `description` and an `example` for the form, OpenAPI and MCP.
|
|
45
52
|
export interface FieldBase<Value = unknown> {
|
|
46
53
|
optional?: boolean | undefined
|
|
47
54
|
default?: Value | undefined
|
|
48
|
-
error?:
|
|
55
|
+
error?: ErrorText | undefined
|
|
49
56
|
label?: string | undefined
|
|
50
57
|
description?: string | undefined
|
|
51
58
|
example?: Value | undefined
|
|
@@ -213,7 +220,7 @@ export interface ObjectOptions extends FieldOptions {
|
|
|
213
220
|
|
|
214
221
|
// The fields of an object, `shape` a record of schemas, which is refused with a 422 when it carries a key the shape lacks; `{ extra: 'strip' }` drops it instead. Anywhere kay takes a schema, a plain record is this object, so `s.object()` is rarely written.
|
|
215
222
|
function object<Shape extends Record<string, Schema>, const Options extends ObjectOptions>(shape: Shape, options?: Options): Built<ReturnType<typeof schema.Object<Shape>>, Options> {
|
|
216
|
-
return wrapped(schema.Object(shape, { ...options?.extra
|
|
223
|
+
return wrapped(schema.Object(shape, { ...options?.extra === 'strip' ? { 'x-kay-extra': 'strip' } : { additionalProperties: false }, ...rest(options, [...FIELD_KEYS, 'extra']), ...keywords(options ?? {}) }), options)
|
|
217
224
|
}
|
|
218
225
|
|
|
219
226
|
// An object of any keys, each holding `value`.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// The shape every Standard Schema validator (Zod 4.2+, ArkType 2.1.28+, Valibot) has, https://standardschema.dev: written here so kay needs no dependency to accept one.
|
|
2
|
+
export interface StandardSchema<Input = unknown, Output = Input> {
|
|
3
|
+
readonly '~standard': {
|
|
4
|
+
readonly version: 1
|
|
5
|
+
readonly vendor: string
|
|
6
|
+
readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>
|
|
7
|
+
readonly types?: { readonly input: Input, readonly output: Output } | undefined
|
|
8
|
+
// The JSON Schema extension: what a form's constraints, OpenAPI and MCP are read from.
|
|
9
|
+
readonly jsonSchema?: { readonly input: (options: { target: string }) => Record<string, unknown> } | undefined
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type StandardResult<Output> = { readonly value: Output, readonly issues?: undefined } | { readonly issues: readonly { readonly message: string, readonly path?: readonly (PropertyKey | { readonly key: PropertyKey })[] | undefined }[] }
|
|
14
|
+
|
|
15
|
+
export type StandardOutput<Type> = Type extends { '~standard': { types?: infer Types } } ? NonNullable<Types> extends { output: infer Output } ? Output : unknown : never
|
|
16
|
+
|
|
17
|
+
export function isStandardSchema(value: unknown): value is StandardSchema {
|
|
18
|
+
return typeof value === 'object' && value !== null && '~standard' in value && typeof (value as StandardSchema)['~standard'].validate === 'function'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// The JSON Schema a Standard Schema validator describes its input with, as draft 2020-12; undefined for one without the extension, which validates but gives a form no constraints, nor OpenAPI and MCP a description.
|
|
22
|
+
export function jsonSchemaOf(schema: object): Record<string, unknown> | undefined {
|
|
23
|
+
if (!isStandardSchema(schema))
|
|
24
|
+
return schema as Record<string, unknown>
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
return schema['~standard'].jsonSchema?.input({ target: 'draft-2020-12' })
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return undefined
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type { Issue, Refusal, Source, Validated, ValidateOptions } from '../server/validate'
|
|
2
|
+
import type { FieldRules } from './field-refusal'
|
|
3
|
+
import { errorText, ruleOf } from './field-errors'
|
|
4
|
+
import { issueOf } from './field-refusal'
|
|
5
|
+
import { formInput } from './form'
|
|
6
|
+
import { schemaOf } from './schema-record'
|
|
7
|
+
import { isStandardSchema, jsonSchemaOf } from './standard-schema'
|
|
8
|
+
import { wording } from './words.client'
|
|
9
|
+
|
|
10
|
+
export type { Issue, Refusal, Source, Validated, ValidateOptions }
|
|
11
|
+
|
|
12
|
+
type Node = Record<string, unknown> & FieldRules & { properties?: Record<string, Node>, required?: string[], additionalProperties?: unknown, items?: Node, default?: unknown }
|
|
13
|
+
|
|
14
|
+
const isNode = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
15
|
+
|
|
16
|
+
// A missing field takes its schema's `default`, a nested object's too.
|
|
17
|
+
function withDefaults(schema: Node, value: unknown): unknown {
|
|
18
|
+
if (!isNode(value) || schema.properties === undefined)
|
|
19
|
+
return value
|
|
20
|
+
|
|
21
|
+
const fields: Record<string, unknown> = { ...value }
|
|
22
|
+
|
|
23
|
+
for (const [name, field] of Object.entries(schema.properties)) {
|
|
24
|
+
if (fields[name] === undefined && field.default !== undefined && field.format !== 'binary')
|
|
25
|
+
fields[name] = structuredClone(field.default)
|
|
26
|
+
else if (fields[name] !== undefined)
|
|
27
|
+
fields[name] = withDefaults(field, fields[name])
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return fields
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// A form or a query posts text and a blank list is the empty one: the top-level fields are read as their schema expects, as the server reads them.
|
|
34
|
+
function readText(schema: Node, value: unknown, form: boolean, trim: boolean): unknown {
|
|
35
|
+
const input = isNode(value) ? { ...value } : {}
|
|
36
|
+
|
|
37
|
+
for (const [name, field] of Object.entries(schema.properties ?? {})) {
|
|
38
|
+
if (form && input[name] === '' && field.type === 'array')
|
|
39
|
+
input[name] = []
|
|
40
|
+
else if (form && input[name] === '' && field.default !== undefined && !schema.required?.includes(name))
|
|
41
|
+
delete input[name]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return formInput(schema as never, withDefaults(schema, input), form, form && trim)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const join = (path: string, step: string): string => path === '' ? step : `${path}.${step}`
|
|
48
|
+
|
|
49
|
+
function issue(path: string, key: Issue['key'], english: string, field: Node): Issue {
|
|
50
|
+
return { path, key, params: {}, message: errorText(field.error, ruleOf(key.slice('kay.invalid.'.length))) ?? wording(key, english) }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Every place the value fails the schema, once each, down to the fields of nested objects and the items of lists.
|
|
54
|
+
function walk(field: Node, value: unknown, path: string, source: Source, issues: Issue[]): void {
|
|
55
|
+
if (value === undefined) {
|
|
56
|
+
issues.push(issue(path, 'kay.invalid.required', 'Expected required property', field))
|
|
57
|
+
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A file is read by its bytes on the server alone.
|
|
62
|
+
if (field.format === 'binary' || field.items?.format === 'binary')
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
if (field.anyOf?.some(member => member.type === 'object' || member.type === 'array')) {
|
|
66
|
+
const tries = field.anyOf.map((member) => {
|
|
67
|
+
const found: Issue[] = []
|
|
68
|
+
|
|
69
|
+
walk(member as Node, value, path, source, found)
|
|
70
|
+
|
|
71
|
+
return found
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
if (!tries.some(found => found.length === 0))
|
|
75
|
+
issues.push(issue(path, 'kay.invalid.type', 'Expected union value', field))
|
|
76
|
+
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (field.type === 'object' && field.anyOf === undefined) {
|
|
81
|
+
if (!isNode(value)) {
|
|
82
|
+
issues.push(issue(path, 'kay.invalid.type', 'Expected object', field))
|
|
83
|
+
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const shape = field.properties ?? {}
|
|
88
|
+
|
|
89
|
+
for (const [name, member] of Object.entries(shape)) {
|
|
90
|
+
if (value[name] !== undefined)
|
|
91
|
+
walk(member, value[name], join(path, name), source, issues)
|
|
92
|
+
else if (field.required?.includes(name))
|
|
93
|
+
issues.push(issue(join(path, name), 'kay.invalid.required', 'Expected required property', member))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (const [name, member] of Object.entries(value)) {
|
|
97
|
+
if (Object.hasOwn(shape, name))
|
|
98
|
+
continue
|
|
99
|
+
|
|
100
|
+
if (isNode(field.additionalProperties))
|
|
101
|
+
walk(field.additionalProperties, member, join(path, name), source, issues)
|
|
102
|
+
else if (field.additionalProperties === false)
|
|
103
|
+
issues.push(issue(join(path, name), 'kay.invalid.type', 'Unexpected property', field))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const found = issueOf(path, field, value)
|
|
110
|
+
|
|
111
|
+
if (found !== undefined) {
|
|
112
|
+
issues.push({ path, ...found })
|
|
113
|
+
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (field.type === 'array' && field.items !== undefined && Array.isArray(value)) {
|
|
118
|
+
for (const [index, item] of value.entries())
|
|
119
|
+
walk(field.items, item, join(path, String(index)), source, issues)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// What an object keeps of a value: a form's unknown top-level fields are dropped, as a browser posts controls the app does not own, and an object that opted into `extra: 'strip'` loses its unknown keys.
|
|
124
|
+
function stripped(schema: Node, value: unknown, dropUnknown = false): unknown {
|
|
125
|
+
if (Array.isArray(value))
|
|
126
|
+
return schema.items === undefined ? value : value.map(item => stripped(schema.items!, item))
|
|
127
|
+
|
|
128
|
+
if (!isNode(value) || schema.properties === undefined)
|
|
129
|
+
return value
|
|
130
|
+
|
|
131
|
+
const shape = schema.properties
|
|
132
|
+
const keep = schema['x-kay-extra'] !== 'strip' && !dropUnknown
|
|
133
|
+
|
|
134
|
+
return Object.fromEntries(Object.entries(value).filter(([name]) => keep || Object.hasOwn(shape, name)).map(([name, member]) => [name, Object.hasOwn(shape, name) ? stripped(shape[name]!, member) : member]))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const refused = (errors: Issue[]): Refusal => ({ errors, status: 422 })
|
|
138
|
+
|
|
139
|
+
interface StandardResult {
|
|
140
|
+
value?: unknown
|
|
141
|
+
issues?: readonly { message?: string, path?: unknown }[]
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function standard(schema: object, source: Source, value: unknown, trim: boolean): Promise<Validated> {
|
|
145
|
+
const rules = source === 'json' ? undefined : jsonSchemaOf(schema) as Node | undefined
|
|
146
|
+
const read = rules === undefined ? value : readText(rules, value, source !== 'env' && source !== 'params', trim)
|
|
147
|
+
const result = await (schema as { '~standard': { validate: (value: unknown) => StandardResult | Promise<StandardResult> } })['~standard'].validate(read)
|
|
148
|
+
|
|
149
|
+
if (result.issues === undefined)
|
|
150
|
+
return { value: result.value }
|
|
151
|
+
|
|
152
|
+
const seen = new Set<string>()
|
|
153
|
+
const errors: Issue[] = []
|
|
154
|
+
|
|
155
|
+
for (const found of result.issues) {
|
|
156
|
+
const path = Array.isArray(found.path) ? found.path.map(step => String(isNode(step) ? step.key : step)).join('.') : ''
|
|
157
|
+
|
|
158
|
+
if (!seen.has(path)) {
|
|
159
|
+
seen.add(path)
|
|
160
|
+
errors.push({ path, key: 'kay.invalid.type', params: {}, message: found.message ?? wording('kay.invalid.type', 'Invalid value') })
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return refused(errors)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// The value a schema reads from `source`, or where and why it is refused: the server's `validate` in the browser, for a form before it posts or a row a local collection takes. A date stays its text, and a file is checked by the server alone, which reads its bytes; the messages are the page's locale.
|
|
168
|
+
export async function validate<Value = unknown>(schema: object, value: unknown, { source, trim = true }: ValidateOptions): Promise<Validated<Value>> {
|
|
169
|
+
if (isStandardSchema(schema))
|
|
170
|
+
return await standard(schema, source, value, trim) as Validated<Value>
|
|
171
|
+
|
|
172
|
+
const rules = schemaOf(schema as never) as unknown as Node
|
|
173
|
+
const read = stripped(rules, source === 'json' ? withDefaults(rules, value) : readText(rules, value, source !== 'env' && source !== 'params', trim), source === 'form')
|
|
174
|
+
const issues: Issue[] = []
|
|
175
|
+
|
|
176
|
+
walk(rules, read, '', source, issues)
|
|
177
|
+
|
|
178
|
+
return issues.length === 0 ? { value: read as Value } : refused(issues)
|
|
179
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Validated, ValidateOptions } from '../server/validate'
|
|
2
|
+
|
|
3
|
+
export type { Issue, Refusal, Source, Validated, ValidateOptions } from '../server/validate'
|
|
4
|
+
|
|
5
|
+
// The server's `validate` in the browser. The validator is a chunk of its own, loaded by the first call, so a page whose islands never call `validate` does not fetch it and one that does fetches it once.
|
|
6
|
+
// kept: free
|
|
7
|
+
export async function validate<Value = unknown>(schema: object, value: unknown, options: ValidateOptions): Promise<Validated<Value>> {
|
|
8
|
+
const { validate: run } = await import('./validate-run.client')
|
|
9
|
+
|
|
10
|
+
return run<Value>(schema, value, options)
|
|
11
|
+
}
|
package/src/runtime/validate.ts
CHANGED
|
@@ -1,47 +1,17 @@
|
|
|
1
1
|
import type { TSchema } from 'elysia'
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
2
|
+
import type { FormRules } from './field-refusal'
|
|
3
|
+
import { fieldMessage } from './field-refusal'
|
|
4
4
|
import { focusInvalid } from './focus'
|
|
5
|
-
import { formInput
|
|
5
|
+
import { formInput } from './form'
|
|
6
6
|
import { controlsOf } from './form-controls'
|
|
7
7
|
import { markInvalid } from './invalid'
|
|
8
8
|
import { ISLAND } from './markers'
|
|
9
9
|
import { uid } from './uid'
|
|
10
|
-
import { wording } from './words.client'
|
|
11
|
-
|
|
12
|
-
// What the server sends of an action's schema, for the forms of a page to be checked before they post: the top-level fields, the keywords the browser checks, and their `error` messages.
|
|
13
|
-
export interface FieldRules {
|
|
14
|
-
type?: string | undefined
|
|
15
|
-
format?: string | undefined
|
|
16
|
-
pattern?: string | undefined
|
|
17
|
-
minLength?: number | undefined
|
|
18
|
-
maxLength?: number | undefined
|
|
19
|
-
minimum?: number | undefined
|
|
20
|
-
maximum?: number | undefined
|
|
21
|
-
exclusiveMinimum?: number | undefined
|
|
22
|
-
exclusiveMaximum?: number | undefined
|
|
23
|
-
minItems?: number | undefined
|
|
24
|
-
maxItems?: number | undefined
|
|
25
|
-
const?: unknown | undefined
|
|
26
|
-
items?: FieldRules | undefined
|
|
27
|
-
anyOf?: FieldRules[] | undefined
|
|
28
|
-
error?: string | undefined
|
|
29
|
-
}
|
|
30
10
|
|
|
31
|
-
export
|
|
32
|
-
properties: Record<string, FieldRules>
|
|
33
|
-
required: string[]
|
|
34
|
-
}
|
|
11
|
+
export type { FieldRules, FormRules } from './field-refusal'
|
|
35
12
|
|
|
36
13
|
// The schemas of the actions a page's forms post to, by action path, in one JSON script of the page's head, and one more after each streamed block bringing forms the page lacked.
|
|
37
14
|
export const FORMS_SCRIPT = 'script[data-kay-forms]'
|
|
38
|
-
type Format = RegExp | ((value: string) => boolean)
|
|
39
|
-
|
|
40
|
-
// The formats the browser checks too, as the server does (`email` is kay's own, the rest Elysia's); any other format is left to the server.
|
|
41
|
-
const FORMATS: Record<string, Format> = {
|
|
42
|
-
email: isEmail,
|
|
43
|
-
uuid: /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i,
|
|
44
|
-
}
|
|
45
15
|
|
|
46
16
|
// Checks a form against the schema of the action it posts to, as the server would, reporting each refused field with the message the server would give; true when it may post.
|
|
47
17
|
export function checkForm(form: HTMLFormElement, action: string): boolean {
|
|
@@ -205,106 +175,3 @@ function controlOf(form: HTMLFormElement, name: string): HTMLInputElement | HTML
|
|
|
205
175
|
|
|
206
176
|
return control instanceof HTMLInputElement || control instanceof HTMLSelectElement || control instanceof HTMLTextAreaElement ? control : undefined
|
|
207
177
|
}
|
|
208
|
-
|
|
209
|
-
function formatFails(format: Format | undefined, value: string): boolean {
|
|
210
|
-
if (format === undefined)
|
|
211
|
-
return false
|
|
212
|
-
|
|
213
|
-
return typeof format === 'function' ? !format(value) : !format.test(value)
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// The schema's own `error` for a refused field, else the `kay.invalid.*` text of the keyword it fails, which the server words the same way; a site without the key reads Elysia's English.
|
|
217
|
-
function fieldMessage(name: string, field: FieldRules, value: unknown): string | undefined {
|
|
218
|
-
const message = refusal(name, field, value)
|
|
219
|
-
|
|
220
|
-
if (message !== undefined)
|
|
221
|
-
return field.error ?? wording(message.kind, message.english, message.params)
|
|
222
|
-
|
|
223
|
-
// An item is named by its index, as the server names it, and refused with its own `error`.
|
|
224
|
-
return field.items && Array.isArray(value) ? value.map((item, index) => fieldMessage(`${name}.${index}`, field.items!, item)).find(found => found !== undefined) : undefined
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// What a field's value fails: the `kay.invalid.*` key the server's refusal of it would use, the limit it names, and Elysia's own summary, which is its English.
|
|
228
|
-
interface Refusal {
|
|
229
|
-
kind: InvalidWord
|
|
230
|
-
english: string
|
|
231
|
-
params?: Record<string, string | number>
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function typeRefusal(english: string): Refusal {
|
|
235
|
-
return { kind: 'kay.invalid.type', english }
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function refusal(name: string, field: FieldRules, value: unknown): Refusal | undefined {
|
|
239
|
-
if (field.anyOf) {
|
|
240
|
-
const bound = bounds(field, value)
|
|
241
|
-
|
|
242
|
-
if (bound === undefined && field.anyOf.some(member => refusal(name, member, value) === undefined))
|
|
243
|
-
return undefined
|
|
244
|
-
|
|
245
|
-
// A union is summarized by its members, though a bound of its own gives the kind, as the server words it.
|
|
246
|
-
return { kind: bound?.kind ?? 'kay.invalid.type', english: `Property '${name}' should be one of: ${field.anyOf.map(member => `'${member.format ?? member.type}'`).join(', ')}`, ...bound?.params && { params: bound.params } }
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (field.const !== undefined)
|
|
250
|
-
return value === field.const ? undefined : typeRefusal(`Expected ${typeof field.const === 'string' ? `'${field.const}'` : String(field.const)}`)
|
|
251
|
-
|
|
252
|
-
switch (field.type) {
|
|
253
|
-
case 'string':
|
|
254
|
-
if (typeof value !== 'string')
|
|
255
|
-
return typeRefusal('Expected string')
|
|
256
|
-
|
|
257
|
-
if (field.format !== undefined && NUMERIC_FORMATS.has(field.format))
|
|
258
|
-
return typeRefusal('Expected string')
|
|
259
|
-
|
|
260
|
-
if (field.minLength !== undefined && value.length < field.minLength)
|
|
261
|
-
return { kind: value === '' && field.minLength === 1 ? 'kay.invalid.required' : 'kay.invalid.tooShort', english: `Expected string length greater or equal to ${field.minLength}`, params: { min: field.minLength } }
|
|
262
|
-
|
|
263
|
-
if (field.maxLength !== undefined && value.length > field.maxLength)
|
|
264
|
-
return { kind: 'kay.invalid.tooLong', english: `Expected string length less or equal to ${field.maxLength}`, params: { max: field.maxLength } }
|
|
265
|
-
|
|
266
|
-
if (field.pattern !== undefined && !new RegExp(field.pattern).test(value))
|
|
267
|
-
return { kind: 'kay.invalid.pattern', english: `Expected string to match '${field.pattern}'` }
|
|
268
|
-
|
|
269
|
-
return field.format !== undefined && formatFails(FORMATS[field.format], value) ? { kind: 'kay.invalid.format', english: `Property '${name}' should be ${field.format}`, params: { format: field.format } } : undefined
|
|
270
|
-
case 'number':
|
|
271
|
-
case 'integer':
|
|
272
|
-
if (typeof value !== 'number' || Number.isNaN(value))
|
|
273
|
-
return typeRefusal(`Expected ${field.type}`)
|
|
274
|
-
|
|
275
|
-
// A bound a number fails is worded before its type, as the server does.
|
|
276
|
-
return bounds(field, value) ?? (field.type === 'integer' && !Number.isInteger(value) ? typeRefusal('Expected integer') : undefined)
|
|
277
|
-
case 'boolean':
|
|
278
|
-
return typeof value === 'boolean' ? undefined : typeRefusal('Expected boolean')
|
|
279
|
-
case 'array':
|
|
280
|
-
if (!Array.isArray(value))
|
|
281
|
-
return typeRefusal('Expected array')
|
|
282
|
-
|
|
283
|
-
if (field.minItems !== undefined && value.length < field.minItems)
|
|
284
|
-
return { kind: 'kay.invalid.tooFewItems', english: `Expected array length to be greater or equal to ${field.minItems}`, params: { min: field.minItems } }
|
|
285
|
-
|
|
286
|
-
return field.maxItems !== undefined && value.length > field.maxItems ? { kind: 'kay.invalid.tooManyItems', english: `Expected array length to be less or equal to ${field.maxItems}`, params: { max: field.maxItems } } : undefined
|
|
287
|
-
case undefined:
|
|
288
|
-
default:
|
|
289
|
-
return undefined
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// An exclusive bound is worded as a type refusal, as the server does.
|
|
294
|
-
function bounds(field: FieldRules, value: unknown): Refusal | undefined {
|
|
295
|
-
if (typeof value !== 'number')
|
|
296
|
-
return undefined
|
|
297
|
-
|
|
298
|
-
const type = field.type ?? 'number'
|
|
299
|
-
|
|
300
|
-
if (field.minimum !== undefined && value < field.minimum)
|
|
301
|
-
return { kind: 'kay.invalid.tooSmall', english: `Expected ${type} to be greater or equal to ${field.minimum}`, params: { min: field.minimum } }
|
|
302
|
-
|
|
303
|
-
if (field.maximum !== undefined && value > field.maximum)
|
|
304
|
-
return { kind: 'kay.invalid.tooBig', english: `Expected ${type} to be less or equal to ${field.maximum}`, params: { max: field.maximum } }
|
|
305
|
-
|
|
306
|
-
if (field.exclusiveMinimum !== undefined && value <= field.exclusiveMinimum)
|
|
307
|
-
return typeRefusal(`Expected ${type} to be greater than ${field.exclusiveMinimum}`)
|
|
308
|
-
|
|
309
|
-
return field.exclusiveMaximum !== undefined && value >= field.exclusiveMaximum ? typeRefusal(`Expected ${type} to be less than ${field.exclusiveMaximum}`) : undefined
|
|
310
|
-
}
|