typesea 0.1.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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +320 -0
- package/dist/adapters/index.d.ts +152 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +396 -0
- package/dist/aot/index.d.ts +33 -0
- package/dist/aot/index.d.ts.map +1 -0
- package/dist/aot/index.js +295 -0
- package/dist/async/index.d.ts +111 -0
- package/dist/async/index.d.ts.map +1 -0
- package/dist/async/index.js +221 -0
- package/dist/builders/composite.d.ts +31 -0
- package/dist/builders/composite.d.ts.map +1 -0
- package/dist/builders/composite.js +165 -0
- package/dist/builders/index.d.ts +11 -0
- package/dist/builders/index.d.ts.map +1 -0
- package/dist/builders/index.js +9 -0
- package/dist/builders/modifier.d.ts +26 -0
- package/dist/builders/modifier.d.ts.map +1 -0
- package/dist/builders/modifier.js +67 -0
- package/dist/builders/object/guard.d.ts +62 -0
- package/dist/builders/object/guard.d.ts.map +1 -0
- package/dist/builders/object/guard.js +113 -0
- package/dist/builders/object/index.d.ts +7 -0
- package/dist/builders/object/index.d.ts.map +1 -0
- package/dist/builders/object/index.js +5 -0
- package/dist/builders/object/schema.d.ts +44 -0
- package/dist/builders/object/schema.d.ts.map +1 -0
- package/dist/builders/object/schema.js +257 -0
- package/dist/builders/object/types.d.ts +63 -0
- package/dist/builders/object/types.d.ts.map +1 -0
- package/dist/builders/object/types.js +5 -0
- package/dist/builders/scalar.d.ts +39 -0
- package/dist/builders/scalar.d.ts.map +1 -0
- package/dist/builders/scalar.js +63 -0
- package/dist/builders/table.d.ts +53 -0
- package/dist/builders/table.d.ts.map +1 -0
- package/dist/builders/table.js +48 -0
- package/dist/builders/types.d.ts +26 -0
- package/dist/builders/types.d.ts.map +1 -0
- package/dist/builders/types.js +5 -0
- package/dist/compile/check-composite.d.ts +34 -0
- package/dist/compile/check-composite.d.ts.map +1 -0
- package/dist/compile/check-composite.js +117 -0
- package/dist/compile/check-scalar.d.ts +24 -0
- package/dist/compile/check-scalar.d.ts.map +1 -0
- package/dist/compile/check-scalar.js +73 -0
- package/dist/compile/check.d.ts +15 -0
- package/dist/compile/check.d.ts.map +1 -0
- package/dist/compile/check.js +98 -0
- package/dist/compile/context.d.ts +35 -0
- package/dist/compile/context.d.ts.map +1 -0
- package/dist/compile/context.js +72 -0
- package/dist/compile/graph-predicate.d.ts +19 -0
- package/dist/compile/graph-predicate.d.ts.map +1 -0
- package/dist/compile/graph-predicate.js +460 -0
- package/dist/compile/guard.d.ts +41 -0
- package/dist/compile/guard.d.ts.map +1 -0
- package/dist/compile/guard.js +180 -0
- package/dist/compile/index.d.ts +8 -0
- package/dist/compile/index.d.ts.map +1 -0
- package/dist/compile/index.js +6 -0
- package/dist/compile/issue.d.ts +18 -0
- package/dist/compile/issue.d.ts.map +1 -0
- package/dist/compile/issue.js +28 -0
- package/dist/compile/names.d.ts +16 -0
- package/dist/compile/names.d.ts.map +1 -0
- package/dist/compile/names.js +82 -0
- package/dist/compile/predicate.d.ts +23 -0
- package/dist/compile/predicate.d.ts.map +1 -0
- package/dist/compile/predicate.js +317 -0
- package/dist/compile/runtime.d.ts +55 -0
- package/dist/compile/runtime.d.ts.map +1 -0
- package/dist/compile/runtime.js +63 -0
- package/dist/compile/source.d.ts +11 -0
- package/dist/compile/source.d.ts.map +1 -0
- package/dist/compile/source.js +51 -0
- package/dist/compile/types.d.ts +52 -0
- package/dist/compile/types.d.ts.map +1 -0
- package/dist/compile/types.js +5 -0
- package/dist/decoder/index.d.ts +106 -0
- package/dist/decoder/index.d.ts.map +1 -0
- package/dist/decoder/index.js +262 -0
- package/dist/evaluate/check-composite.d.ts +39 -0
- package/dist/evaluate/check-composite.d.ts.map +1 -0
- package/dist/evaluate/check-composite.js +184 -0
- package/dist/evaluate/check-scalar.d.ts +20 -0
- package/dist/evaluate/check-scalar.d.ts.map +1 -0
- package/dist/evaluate/check-scalar.js +81 -0
- package/dist/evaluate/check.d.ts +11 -0
- package/dist/evaluate/check.d.ts.map +1 -0
- package/dist/evaluate/check.js +126 -0
- package/dist/evaluate/index.d.ts +7 -0
- package/dist/evaluate/index.d.ts.map +1 -0
- package/dist/evaluate/index.js +6 -0
- package/dist/evaluate/issue.d.ts +10 -0
- package/dist/evaluate/issue.d.ts.map +1 -0
- package/dist/evaluate/issue.js +11 -0
- package/dist/evaluate/predicate.d.ts +26 -0
- package/dist/evaluate/predicate.d.ts.map +1 -0
- package/dist/evaluate/predicate.js +37 -0
- package/dist/evaluate/shared.d.ts +59 -0
- package/dist/evaluate/shared.d.ts.map +1 -0
- package/dist/evaluate/shared.js +96 -0
- package/dist/evaluate/state.d.ts +65 -0
- package/dist/evaluate/state.d.ts.map +1 -0
- package/dist/evaluate/state.js +66 -0
- package/dist/guard/base.d.ts +72 -0
- package/dist/guard/base.d.ts.map +1 -0
- package/dist/guard/base.js +136 -0
- package/dist/guard/error.d.ts +19 -0
- package/dist/guard/error.d.ts.map +1 -0
- package/dist/guard/error.js +22 -0
- package/dist/guard/index.d.ts +10 -0
- package/dist/guard/index.d.ts.map +1 -0
- package/dist/guard/index.js +8 -0
- package/dist/guard/number.d.ts +32 -0
- package/dist/guard/number.d.ts.map +1 -0
- package/dist/guard/number.js +71 -0
- package/dist/guard/props.d.ts +18 -0
- package/dist/guard/props.d.ts.map +1 -0
- package/dist/guard/props.js +35 -0
- package/dist/guard/read.d.ts +42 -0
- package/dist/guard/read.d.ts.map +1 -0
- package/dist/guard/read.js +114 -0
- package/dist/guard/registry.d.ts +15 -0
- package/dist/guard/registry.d.ts.map +1 -0
- package/dist/guard/registry.js +21 -0
- package/dist/guard/string.d.ts +36 -0
- package/dist/guard/string.d.ts.map +1 -0
- package/dist/guard/string.js +95 -0
- package/dist/guard/types.d.ts +103 -0
- package/dist/guard/types.d.ts.map +1 -0
- package/dist/guard/types.js +5 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/internal/index.d.ts +34 -0
- package/dist/internal/index.d.ts.map +1 -0
- package/dist/internal/index.js +56 -0
- package/dist/ir/builder.d.ts +173 -0
- package/dist/ir/builder.d.ts.map +1 -0
- package/dist/ir/builder.js +481 -0
- package/dist/ir/freeze.d.ts +10 -0
- package/dist/ir/freeze.d.ts.map +1 -0
- package/dist/ir/freeze.js +102 -0
- package/dist/ir/index.d.ts +9 -0
- package/dist/ir/index.d.ts.map +1 -0
- package/dist/ir/index.js +7 -0
- package/dist/ir/regexp.d.ts +6 -0
- package/dist/ir/regexp.d.ts.map +1 -0
- package/dist/ir/regexp.js +12 -0
- package/dist/ir/types.d.ts +215 -0
- package/dist/ir/types.d.ts.map +1 -0
- package/dist/ir/types.js +5 -0
- package/dist/ir/validate.d.ts +10 -0
- package/dist/ir/validate.d.ts.map +1 -0
- package/dist/ir/validate.js +271 -0
- package/dist/issue/index.d.ts +44 -0
- package/dist/issue/index.d.ts.map +1 -0
- package/dist/issue/index.js +152 -0
- package/dist/json-schema/emit-combinator.d.ts +28 -0
- package/dist/json-schema/emit-combinator.d.ts.map +1 -0
- package/dist/json-schema/emit-combinator.js +96 -0
- package/dist/json-schema/emit-composite.d.ts +28 -0
- package/dist/json-schema/emit-composite.d.ts.map +1 -0
- package/dist/json-schema/emit-composite.js +127 -0
- package/dist/json-schema/emit-scalar.d.ts +25 -0
- package/dist/json-schema/emit-scalar.d.ts.map +1 -0
- package/dist/json-schema/emit-scalar.js +104 -0
- package/dist/json-schema/emit-types.d.ts +12 -0
- package/dist/json-schema/emit-types.d.ts.map +1 -0
- package/dist/json-schema/emit-types.js +5 -0
- package/dist/json-schema/emit.d.ts +12 -0
- package/dist/json-schema/emit.d.ts.map +1 -0
- package/dist/json-schema/emit.js +62 -0
- package/dist/json-schema/freeze.d.ts +14 -0
- package/dist/json-schema/freeze.d.ts.map +1 -0
- package/dist/json-schema/freeze.js +114 -0
- package/dist/json-schema/index.d.ts +20 -0
- package/dist/json-schema/index.d.ts.map +1 -0
- package/dist/json-schema/index.js +76 -0
- package/dist/json-schema/issue.d.ts +11 -0
- package/dist/json-schema/issue.d.ts.map +1 -0
- package/dist/json-schema/issue.js +14 -0
- package/dist/json-schema/read.d.ts +29 -0
- package/dist/json-schema/read.d.ts.map +1 -0
- package/dist/json-schema/read.js +87 -0
- package/dist/json-schema/types.d.ts +106 -0
- package/dist/json-schema/types.d.ts.map +1 -0
- package/dist/json-schema/types.js +5 -0
- package/dist/kind/index.d.ts +119 -0
- package/dist/kind/index.d.ts.map +1 -0
- package/dist/kind/index.js +94 -0
- package/dist/lower/index.d.ts +7 -0
- package/dist/lower/index.d.ts.map +1 -0
- package/dist/lower/index.js +199 -0
- package/dist/message/index.d.ts +51 -0
- package/dist/message/index.d.ts.map +1 -0
- package/dist/message/index.js +269 -0
- package/dist/optimize/compact.d.ts +10 -0
- package/dist/optimize/compact.d.ts.map +1 -0
- package/dist/optimize/compact.js +60 -0
- package/dist/optimize/fold-boolean.d.ts +15 -0
- package/dist/optimize/fold-boolean.d.ts.map +1 -0
- package/dist/optimize/fold-boolean.js +75 -0
- package/dist/optimize/fold-common.d.ts +45 -0
- package/dist/optimize/fold-common.d.ts.map +1 -0
- package/dist/optimize/fold-common.js +71 -0
- package/dist/optimize/fold-scalar.d.ts +59 -0
- package/dist/optimize/fold-scalar.d.ts.map +1 -0
- package/dist/optimize/fold-scalar.js +174 -0
- package/dist/optimize/fold.d.ts +10 -0
- package/dist/optimize/fold.d.ts.map +1 -0
- package/dist/optimize/fold.js +103 -0
- package/dist/optimize/index.d.ts +10 -0
- package/dist/optimize/index.d.ts.map +1 -0
- package/dist/optimize/index.js +23 -0
- package/dist/optimize/map-node.d.ts +21 -0
- package/dist/optimize/map-node.d.ts.map +1 -0
- package/dist/optimize/map-node.js +222 -0
- package/dist/optimize/remap.d.ts +30 -0
- package/dist/optimize/remap.d.ts.map +1 -0
- package/dist/optimize/remap.js +46 -0
- package/dist/optimize/rewrite.d.ts +22 -0
- package/dist/optimize/rewrite.d.ts.map +1 -0
- package/dist/optimize/rewrite.js +34 -0
- package/dist/plan/cache.d.ts +20 -0
- package/dist/plan/cache.d.ts.map +1 -0
- package/dist/plan/cache.js +122 -0
- package/dist/plan/index.d.ts +8 -0
- package/dist/plan/index.d.ts.map +1 -0
- package/dist/plan/index.js +6 -0
- package/dist/plan/predicate.d.ts +27 -0
- package/dist/plan/predicate.d.ts.map +1 -0
- package/dist/plan/predicate.js +415 -0
- package/dist/plan/schema-predicate.d.ts +15 -0
- package/dist/plan/schema-predicate.d.ts.map +1 -0
- package/dist/plan/schema-predicate.js +277 -0
- package/dist/plan/types.d.ts +18 -0
- package/dist/plan/types.d.ts.map +1 -0
- package/dist/plan/types.js +5 -0
- package/dist/result/index.d.ts +27 -0
- package/dist/result/index.d.ts.map +1 -0
- package/dist/result/index.js +20 -0
- package/dist/schema/common.d.ts +30 -0
- package/dist/schema/common.d.ts.map +1 -0
- package/dist/schema/common.js +102 -0
- package/dist/schema/freeze.d.ts +10 -0
- package/dist/schema/freeze.d.ts.map +1 -0
- package/dist/schema/freeze.js +163 -0
- package/dist/schema/index.d.ts +11 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +9 -0
- package/dist/schema/lazy.d.ts +10 -0
- package/dist/schema/lazy.d.ts.map +1 -0
- package/dist/schema/lazy.js +25 -0
- package/dist/schema/literal.d.ts +10 -0
- package/dist/schema/literal.d.ts.map +1 -0
- package/dist/schema/literal.js +17 -0
- package/dist/schema/types.d.ts +243 -0
- package/dist/schema/types.d.ts.map +1 -0
- package/dist/schema/types.js +9 -0
- package/dist/schema/validate.d.ts +10 -0
- package/dist/schema/validate.d.ts.map +1 -0
- package/dist/schema/validate.js +268 -0
- package/docs/api.md +301 -0
- package/docs/engine-notes.md +153 -0
- package/docs/index.html +1242 -0
- package/package.json +68 -0
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { isAsyncDecoderValue } from "../async/index.js";
|
|
2
|
+
import { isDecoderValue } from "../decoder/index.js";
|
|
3
|
+
import { TypeSeaAssertionError } from "../guard/index.js";
|
|
4
|
+
import { formatIssue } from "../message/index.js";
|
|
5
|
+
import { toJsonSchema } from "../json-schema/index.js";
|
|
6
|
+
import { isSchemaValue } from "../schema/index.js";
|
|
7
|
+
/**
|
|
8
|
+
* @brief to trpc parser.
|
|
9
|
+
*/
|
|
10
|
+
export function toTrpcParser(source) {
|
|
11
|
+
readSyncAdapterSource(source, "tRPC parser source");
|
|
12
|
+
return Object.freeze({
|
|
13
|
+
parse(value) {
|
|
14
|
+
const result = decodeSyncSource(source, value);
|
|
15
|
+
if (result.ok) {
|
|
16
|
+
return result.value;
|
|
17
|
+
}
|
|
18
|
+
throw new TypeSeaAssertionError(result.error);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* @brief to async trpc parser.
|
|
24
|
+
*/
|
|
25
|
+
export function toAsyncTrpcParser(source) {
|
|
26
|
+
readAsyncAdapterSource(source, "async tRPC parser source");
|
|
27
|
+
return Object.freeze({
|
|
28
|
+
async parseAsync(value) {
|
|
29
|
+
const result = await decodeAsyncSource(source, value);
|
|
30
|
+
if (result.ok) {
|
|
31
|
+
return result.value;
|
|
32
|
+
}
|
|
33
|
+
throw new TypeSeaAssertionError(result.error);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @brief to fastify route schema.
|
|
39
|
+
*/
|
|
40
|
+
export function toFastifyRouteSchema(guard, options) {
|
|
41
|
+
const part = readFastifyPart(options);
|
|
42
|
+
const schema = toJsonSchema(guard, readFastifyJsonSchemaOptions(options));
|
|
43
|
+
if (!schema.ok) {
|
|
44
|
+
return schema;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
ok: true,
|
|
48
|
+
value: Object.freeze({
|
|
49
|
+
[part]: schema.value
|
|
50
|
+
})
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @brief to fastify validator compiler.
|
|
55
|
+
*/
|
|
56
|
+
export function toFastifyValidatorCompiler(source) {
|
|
57
|
+
const readSource = readFastifyValidatorCompilerSource(source);
|
|
58
|
+
return Object.freeze((route) => {
|
|
59
|
+
const selectedSource = readSource(route);
|
|
60
|
+
return (value) => {
|
|
61
|
+
const result = decodeSyncSource(selectedSource, value);
|
|
62
|
+
if (result.ok) {
|
|
63
|
+
return {
|
|
64
|
+
value: result.value
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
error: new TypeSeaAssertionError(result.error)
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @brief to react hook form resolver.
|
|
75
|
+
*/
|
|
76
|
+
export function toReactHookFormResolver(source, options) {
|
|
77
|
+
const config = readReactHookFormOptions(options);
|
|
78
|
+
readAsyncAdapterSource(source, "React Hook Form resolver source");
|
|
79
|
+
return async (values) => {
|
|
80
|
+
const result = await decodeAsyncSource(source, values);
|
|
81
|
+
if (result.ok) {
|
|
82
|
+
return Object.freeze({
|
|
83
|
+
values: result.value,
|
|
84
|
+
errors: emptyErrors
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return Object.freeze({
|
|
88
|
+
values: emptyValues,
|
|
89
|
+
errors: issuesToReactHookFormErrors(result.error, config.messages)
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* @brief decode sync source.
|
|
95
|
+
*/
|
|
96
|
+
function decodeSyncSource(source, value) {
|
|
97
|
+
if (isDecoderValue(source)) {
|
|
98
|
+
return source.decode(value);
|
|
99
|
+
}
|
|
100
|
+
const guard = readGuard(source, "adapter source");
|
|
101
|
+
return guard.check(value);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @brief decode async source.
|
|
105
|
+
*/
|
|
106
|
+
async function decodeAsyncSource(source, value) {
|
|
107
|
+
if (isAsyncDecoderValue(source)) {
|
|
108
|
+
return await source.decodeAsync(value);
|
|
109
|
+
}
|
|
110
|
+
if (isDecoderValue(source)) {
|
|
111
|
+
return source.decode(value);
|
|
112
|
+
}
|
|
113
|
+
const guard = readGuard(source, "adapter source");
|
|
114
|
+
return guard.check(value);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* @brief read guard.
|
|
118
|
+
*/
|
|
119
|
+
function readGuard(value, label) {
|
|
120
|
+
if (!isRecord(value)) {
|
|
121
|
+
throw new TypeError(`${label} must be a TypeSea guard or decoder`);
|
|
122
|
+
}
|
|
123
|
+
const guard = value;
|
|
124
|
+
const schema = value["schema"];
|
|
125
|
+
if (!isSchemaValue(schema) || typeof guard.check !== "function") {
|
|
126
|
+
throw new TypeError(`${label} must be a TypeSea guard or decoder`);
|
|
127
|
+
}
|
|
128
|
+
return guard;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* @brief read sync adapter source.
|
|
132
|
+
*/
|
|
133
|
+
function readSyncAdapterSource(source, label) {
|
|
134
|
+
if (isDecoderValue(source)) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
readGuard(source, label);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* @brief read fastify validator compiler source.
|
|
141
|
+
* @details Normalizes a single validator source or a route-part source table into one selector.
|
|
142
|
+
* @returns Source selector used by the Fastify compiler callback.
|
|
143
|
+
*/
|
|
144
|
+
function readFastifyValidatorCompilerSource(source) {
|
|
145
|
+
if (isDecoderValue(source) || isGuardValue(source)) {
|
|
146
|
+
readSyncAdapterSource(source, "Fastify validator source");
|
|
147
|
+
return () => source;
|
|
148
|
+
}
|
|
149
|
+
if (!isRecord(source)) {
|
|
150
|
+
throw new TypeError("Fastify validator source must be a TypeSea guard, decoder, or route map");
|
|
151
|
+
}
|
|
152
|
+
readFastifyCompilerMap(source);
|
|
153
|
+
return (route) => {
|
|
154
|
+
const part = readFastifyRouteHttpPart(route);
|
|
155
|
+
const selected = source[part];
|
|
156
|
+
if (selected === undefined) {
|
|
157
|
+
throw new TypeError(`Fastify validator source is missing ${part}`);
|
|
158
|
+
}
|
|
159
|
+
return selected;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* @brief is guard value.
|
|
164
|
+
* @details Performs the same structural guard check as `readGuard` without throwing.
|
|
165
|
+
* @returns True when the value is a TypeSea guard object.
|
|
166
|
+
*/
|
|
167
|
+
function isGuardValue(value) {
|
|
168
|
+
if (!isRecord(value)) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
const guard = value;
|
|
172
|
+
return isSchemaValue(value["schema"]) && typeof guard.check === "function";
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* @brief read fastify compiler map.
|
|
176
|
+
* @details Validates every present route-part source before Fastify starts compiling routes.
|
|
177
|
+
* @post No result value is produced; malformed sources throw TypeError.
|
|
178
|
+
*/
|
|
179
|
+
function readFastifyCompilerMap(source) {
|
|
180
|
+
readOptionalFastifyCompilerPart(source, "body");
|
|
181
|
+
readOptionalFastifyCompilerPart(source, "querystring");
|
|
182
|
+
readOptionalFastifyCompilerPart(source, "params");
|
|
183
|
+
readOptionalFastifyCompilerPart(source, "headers");
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* @brief read optional fastify compiler part.
|
|
187
|
+
* @details Checks one optional source-map slot without consulting prototype state.
|
|
188
|
+
* @post No result value is produced; malformed present sources throw TypeError.
|
|
189
|
+
*/
|
|
190
|
+
function readOptionalFastifyCompilerPart(source, part) {
|
|
191
|
+
if (!Object.prototype.hasOwnProperty.call(source, part)) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const partSource = source[part];
|
|
195
|
+
if (partSource === undefined) {
|
|
196
|
+
throw new TypeError(`Fastify validator source for ${part} must be defined`);
|
|
197
|
+
}
|
|
198
|
+
readSyncAdapterSource(partSource, `Fastify validator source for ${part}`);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* @brief read fastify route http part.
|
|
202
|
+
* @details Converts Fastify's route descriptor slot into TypeSea's closed part union.
|
|
203
|
+
* @returns Validated Fastify HTTP part.
|
|
204
|
+
*/
|
|
205
|
+
function readFastifyRouteHttpPart(route) {
|
|
206
|
+
switch (route.httpPart) {
|
|
207
|
+
case "body":
|
|
208
|
+
case "querystring":
|
|
209
|
+
case "params":
|
|
210
|
+
case "headers":
|
|
211
|
+
return route.httpPart;
|
|
212
|
+
default:
|
|
213
|
+
throw new TypeError("Fastify validator route httpPart is invalid");
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* @brief read async adapter source.
|
|
218
|
+
*/
|
|
219
|
+
function readAsyncAdapterSource(source, label) {
|
|
220
|
+
if (isAsyncDecoderValue(source) || isDecoderValue(source)) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
readGuard(source, label);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* @brief read fastify part.
|
|
227
|
+
*/
|
|
228
|
+
function readFastifyPart(options) {
|
|
229
|
+
if (options?.part === undefined) {
|
|
230
|
+
return "body";
|
|
231
|
+
}
|
|
232
|
+
switch (options.part) {
|
|
233
|
+
case "body":
|
|
234
|
+
case "querystring":
|
|
235
|
+
case "params":
|
|
236
|
+
case "headers":
|
|
237
|
+
return options.part;
|
|
238
|
+
default:
|
|
239
|
+
throw new TypeError("Fastify schema part is invalid");
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* @brief read fastify json schema options.
|
|
244
|
+
* @details Copies only JSON Schema exporter options out of the adapter options object.
|
|
245
|
+
* @returns Partial JSON Schema options without exact-optional undefined slots.
|
|
246
|
+
*/
|
|
247
|
+
function readFastifyJsonSchemaOptions(options) {
|
|
248
|
+
const output = {};
|
|
249
|
+
if (options?.dialect !== undefined) {
|
|
250
|
+
output.dialect = options.dialect;
|
|
251
|
+
}
|
|
252
|
+
if (options?.schemaId !== undefined) {
|
|
253
|
+
output.schemaId = options.schemaId;
|
|
254
|
+
}
|
|
255
|
+
return output;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* @brief read react hook form options.
|
|
259
|
+
*/
|
|
260
|
+
function readReactHookFormOptions(options) {
|
|
261
|
+
if (options === undefined) {
|
|
262
|
+
return {
|
|
263
|
+
messages: undefined
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
if (!isRecord(options)) {
|
|
267
|
+
throw new TypeError("React Hook Form resolver options must be an object");
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
messages: options.messages
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* @brief issues to react hook form errors.
|
|
275
|
+
*/
|
|
276
|
+
function issuesToReactHookFormErrors(issues, options) {
|
|
277
|
+
const errors = makeReactHookFormErrorBranch();
|
|
278
|
+
for (let index = 0; index < issues.length; index += 1) {
|
|
279
|
+
const issue = issues[index];
|
|
280
|
+
if (issue === undefined) {
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
insertReactHookFormIssue(errors, issue, options);
|
|
284
|
+
}
|
|
285
|
+
return freezeReactHookFormErrors(errors);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* @brief make react hook form error branch.
|
|
289
|
+
* @details Allocates one null-prototype object so form field names cannot collide with inherited keys.
|
|
290
|
+
* @returns Mutable branch owned by the caller.
|
|
291
|
+
*/
|
|
292
|
+
function makeReactHookFormErrorBranch() {
|
|
293
|
+
return Object.create(null);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* @brief insert react hook form issue.
|
|
297
|
+
* @details Inserts one TypeSea issue into a nested React Hook Form error tree.
|
|
298
|
+
* @param errors Borrowed output branch that receives the leaf error when absent.
|
|
299
|
+
* @param issue Borrowed input issue; its path is converted into string map keys.
|
|
300
|
+
* @param options Borrowed input message options used only for new leaf formatting.
|
|
301
|
+
* @post Existing first errors are preserved to match React Hook Form resolver behavior.
|
|
302
|
+
*/
|
|
303
|
+
function insertReactHookFormIssue(errors, issue, options) {
|
|
304
|
+
const path = issue.path.length === 0 ? rootIssuePath : issue.path;
|
|
305
|
+
let cursor = errors;
|
|
306
|
+
for (let index = 0; index < path.length - 1; index += 1) {
|
|
307
|
+
const segment = path[index];
|
|
308
|
+
if (segment === undefined) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const key = reactHookFormPathKey(segment);
|
|
312
|
+
const child = cursor[key];
|
|
313
|
+
if (child === undefined) {
|
|
314
|
+
const branch = makeReactHookFormErrorBranch();
|
|
315
|
+
cursor[key] = branch;
|
|
316
|
+
cursor = branch;
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
if (!isReactHookFormErrorBranch(child)) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
cursor = child;
|
|
323
|
+
}
|
|
324
|
+
const last = path[path.length - 1];
|
|
325
|
+
if (last === undefined) {
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
const key = reactHookFormPathKey(last);
|
|
329
|
+
if (Object.prototype.hasOwnProperty.call(cursor, key)) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
cursor[key] = Object.freeze({
|
|
333
|
+
type: issue.code,
|
|
334
|
+
message: issue.message ?? formatIssue(issue, options)
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* @brief react hook form path key.
|
|
339
|
+
* @details Converts one TypeSea path segment into the object key React Hook Form traverses.
|
|
340
|
+
* @param segment Borrowed input path segment.
|
|
341
|
+
* @returns Stable string key for object or array-index lookup.
|
|
342
|
+
*/
|
|
343
|
+
function reactHookFormPathKey(segment) {
|
|
344
|
+
return typeof segment === "number" ? String(segment) : segment;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* @brief freeze react hook form errors.
|
|
348
|
+
* @details Recursively freezes the null-prototype tree after all issues have been inserted.
|
|
349
|
+
* @param errors Borrowed mutable branch that becomes immutable before publication.
|
|
350
|
+
* @returns Frozen React Hook Form error tree.
|
|
351
|
+
*/
|
|
352
|
+
function freezeReactHookFormErrors(errors) {
|
|
353
|
+
const keys = Object.keys(errors);
|
|
354
|
+
for (let index = 0; index < keys.length; index += 1) {
|
|
355
|
+
const key = keys[index];
|
|
356
|
+
if (key === undefined) {
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
const child = errors[key];
|
|
360
|
+
if (isReactHookFormErrorBranch(child)) {
|
|
361
|
+
errors[key] = freezeReactHookFormErrors(child);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return Object.freeze(errors);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* @brief is react hook form error branch.
|
|
368
|
+
* @details Distinguishes internal branch objects from frozen leaf error objects.
|
|
369
|
+
* @param value Candidate branch or leaf error value.
|
|
370
|
+
* @returns True when value is a null-prototype branch object.
|
|
371
|
+
*/
|
|
372
|
+
function isReactHookFormErrorBranch(value) {
|
|
373
|
+
return typeof value === "object" &&
|
|
374
|
+
value !== null &&
|
|
375
|
+
Object.getPrototypeOf(value) === null;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* @brief root issue path.
|
|
379
|
+
* @details Module-scope path used when a TypeSea issue points at the submitted root value.
|
|
380
|
+
* @invariant The singleton is never mutated after module initialization.
|
|
381
|
+
*/
|
|
382
|
+
const rootIssuePath = Object.freeze(["root"]);
|
|
383
|
+
/**
|
|
384
|
+
* @brief is record.
|
|
385
|
+
*/
|
|
386
|
+
function isRecord(value) {
|
|
387
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* @brief empty errors.
|
|
391
|
+
*/
|
|
392
|
+
const emptyErrors = Object.freeze({});
|
|
393
|
+
/**
|
|
394
|
+
* @brief empty values.
|
|
395
|
+
*/
|
|
396
|
+
const emptyValues = Object.freeze({});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Guard, Presence } from "../guard/index.js";
|
|
2
|
+
import type { PathSegment } from "../issue/index.js";
|
|
3
|
+
import { type Result } from "../result/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* @brief aot issue code.
|
|
6
|
+
*/
|
|
7
|
+
export type AotIssueCode = "unsupported_aot_lazy" | "unsupported_aot_refine" | "unsupported_aot_symbol_literal";
|
|
8
|
+
/**
|
|
9
|
+
* @brief aot issue.
|
|
10
|
+
*/
|
|
11
|
+
export interface AotIssue {
|
|
12
|
+
readonly path: readonly PathSegment[];
|
|
13
|
+
readonly code: AotIssueCode;
|
|
14
|
+
readonly message: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* @brief aot compile options.
|
|
18
|
+
*/
|
|
19
|
+
export interface AotCompileOptions {
|
|
20
|
+
readonly name: string | undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* @brief aot module.
|
|
24
|
+
*/
|
|
25
|
+
export interface AotModule {
|
|
26
|
+
readonly source: string;
|
|
27
|
+
readonly declarationSource: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @brief emit aot module.
|
|
31
|
+
*/
|
|
32
|
+
export declare function emitAotModule(guard: Guard<unknown, Presence>, options?: Partial<AotCompileOptions>): Result<AotModule, readonly AotIssue[]>;
|
|
33
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/aot/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAQ1D;;GAEG;AACH,MAAM,MAAM,YAAY,GACpB,sBAAsB,GACtB,wBAAwB,GACxB,gCAAgC,CAAC;AAErC;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE,CAAC;IACtC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;CACnC;AASD;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC;CACpC;AAED;;GAEG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,KAAK,CAAC,OAAO,EAAE,QAAQ,CAAC,EAC/B,OAAO,CAAC,EAAE,OAAO,CAAC,iBAAiB,CAAC,GACnC,MAAM,CAAC,SAAS,EAAE,SAAS,QAAQ,EAAE,CAAC,CAsBxC"}
|