@livon/schema 0.27.0-rc.3 → 0.28.0-rc.4
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/LICENSE.md +21 -0
- package/README.md +171 -5
- package/THIRD_PARTY_NOTICES.md +28 -0
- package/dist/SchemaValidationError.cjs +41 -1
- package/dist/SchemaValidationError.d.ts +1 -1
- package/dist/SchemaValidationError.js +7 -1
- package/dist/after.cjs +36 -1
- package/dist/after.d.ts +1 -1
- package/dist/after.js +2 -1
- package/dist/and.cjs +59 -1
- package/dist/and.d.ts +48 -11
- package/dist/and.js +25 -1
- package/dist/api.cjs +317 -1
- package/dist/api.d.ts +3 -3
- package/dist/api.js +277 -1
- package/dist/array.cjs +74 -1
- package/dist/array.d.ts +1 -1
- package/dist/array.js +40 -1
- package/dist/before.cjs +36 -1
- package/dist/before.d.ts +1 -1
- package/dist/before.js +2 -1
- package/dist/binary.cjs +53 -1
- package/dist/binary.d.ts +1 -1
- package/dist/binary.js +19 -1
- package/dist/boolean.cjs +53 -1
- package/dist/boolean.d.ts +1 -1
- package/dist/boolean.js +19 -1
- package/dist/combinatorName.cjs +46 -0
- package/dist/combinatorName.d.ts +8 -0
- package/dist/combinatorName.js +12 -0
- package/dist/context.cjs +125 -1
- package/dist/context.d.ts +6 -6
- package/dist/context.js +76 -1
- package/dist/date.cjs +53 -1
- package/dist/date.d.ts +1 -1
- package/dist/date.js +19 -1
- package/dist/doc.cjs +54 -1
- package/dist/doc.d.ts +2 -2
- package/dist/doc.js +17 -1
- package/dist/enumeration.cjs +74 -1
- package/dist/enumeration.d.ts +4 -4
- package/dist/enumeration.js +40 -1
- package/dist/hydrate.cjs +18 -1
- package/dist/index.cjs +142 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +24 -1
- package/dist/literal.cjs +55 -1
- package/dist/literal.d.ts +1 -1
- package/dist/literal.js +21 -1
- package/dist/number.cjs +89 -1
- package/dist/number.d.ts +6 -6
- package/dist/number.js +55 -1
- package/dist/object.cjs +66 -1
- package/dist/object.d.ts +1 -1
- package/dist/object.js +32 -1
- package/dist/operation.cjs +182 -1
- package/dist/operation.d.ts +6 -6
- package/dist/operation.js +133 -1
- package/dist/or.cjs +93 -1
- package/dist/or.d.ts +2 -3
- package/dist/or.js +59 -1
- package/dist/schema.cjs +292 -1
- package/dist/schema.d.ts +7 -7
- package/dist/schema.js +240 -1
- package/dist/schemaFactory.cjs +125 -1
- package/dist/schemaFactory.d.ts +6 -6
- package/dist/schemaFactory.js +88 -1
- package/dist/schemaModule.cjs +278 -1
- package/dist/schemaModule.d.ts +3 -20
- package/dist/schemaModule.js +244 -1
- package/dist/string.cjs +93 -1
- package/dist/string.d.ts +6 -6
- package/dist/string.js +59 -1
- package/dist/tuple.cjs +58 -1
- package/dist/tuple.d.ts +1 -1
- package/dist/tuple.js +24 -1
- package/dist/typeGuards.cjs +60 -1
- package/dist/typeGuards.d.ts +7 -7
- package/dist/typeGuards.js +8 -1
- package/dist/types.cjs +18 -1
- package/dist/types.d.ts +12 -8
- package/dist/union.cjs +37 -1
- package/dist/union.d.ts +6 -9
- package/dist/union.js +3 -1
- package/package.json +28 -28
- package/PROMPT.md +0 -21
- package/SCHEMA.md +0 -13
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LIVON contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,8 +1,174 @@
|
|
|
1
|
+
<!-- Generated from website/docs/packages/*.md. Do not edit directly. -->
|
|
2
|
+
|
|
1
3
|
# @livon/schema
|
|
2
4
|
|
|
3
|
-
Package docs (GitHub Pages):
|
|
4
|
-
- https://live-input-vector-output-node.github.io/livon-ts/docs/packages/schema
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
[](https://www.npmjs.com/package/@livon/schema)
|
|
7
|
+
[](https://libraries.io/npm/%40livon%2Fschema)
|
|
8
|
+
[](https://github.com/live-input-vector-output-node/livon-ts/actions/workflows/code-quality.yml)
|
|
9
|
+
[](https://www.npmjs.com/package/@livon/schema)
|
|
10
|
+
[](https://github.com/live-input-vector-output-node/livon-ts)
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm add @livon/schema
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Purpose
|
|
19
|
+
|
|
20
|
+
[@livon/schema](https://livon.tech/docs/packages/schema) defines:
|
|
21
|
+
|
|
22
|
+
- [value schemas](https://livon.tech/docs/schema) (`string`, `number`, `object`, `array`, `or`, `union`, ...)
|
|
23
|
+
- operation schemas
|
|
24
|
+
- subscription schemas
|
|
25
|
+
- [schema module](https://livon.tech/docs/technical/runtime-design) (`schemaModule`)
|
|
26
|
+
|
|
27
|
+
## Best for
|
|
28
|
+
|
|
29
|
+
Use this package when you want a single schema source for validation, typing, and generated client APIs.
|
|
30
|
+
|
|
31
|
+
## Schema API docs
|
|
32
|
+
|
|
33
|
+
Each schema/combinator has its own usage page:
|
|
34
|
+
|
|
35
|
+
- [Schema APIs overview](https://livon.tech/docs/schema)
|
|
36
|
+
- [Schema Type Safety](https://livon.tech/docs/schema/type-safety)
|
|
37
|
+
- [Schema Context](https://livon.tech/docs/schema/context)
|
|
38
|
+
- [string](https://livon.tech/docs/schema/string)
|
|
39
|
+
- [number](https://livon.tech/docs/schema/number)
|
|
40
|
+
- [boolean](https://livon.tech/docs/schema/boolean)
|
|
41
|
+
- [date](https://livon.tech/docs/schema/date)
|
|
42
|
+
- [enumeration](https://livon.tech/docs/schema/enumeration)
|
|
43
|
+
- [object](https://livon.tech/docs/schema/object)
|
|
44
|
+
- [array](https://livon.tech/docs/schema/array)
|
|
45
|
+
- [tuple](https://livon.tech/docs/schema/tuple)
|
|
46
|
+
- [literal](https://livon.tech/docs/schema/literal)
|
|
47
|
+
- [union](https://livon.tech/docs/schema/union)
|
|
48
|
+
- [or](https://livon.tech/docs/schema/or)
|
|
49
|
+
- [binary](https://livon.tech/docs/schema/binary)
|
|
50
|
+
- [before](https://livon.tech/docs/schema/before)
|
|
51
|
+
- [after](https://livon.tech/docs/schema/after)
|
|
52
|
+
- [and](https://livon.tech/docs/schema/and)
|
|
53
|
+
- [api](https://livon.tech/docs/schema/api)
|
|
54
|
+
- [operation](https://livon.tech/docs/schema/operation)
|
|
55
|
+
- [subscription](https://livon.tech/docs/schema/subscription)
|
|
56
|
+
- [fieldResolver](https://livon.tech/docs/schema/field-resolver)
|
|
57
|
+
- [schemaFactory](https://livon.tech/docs/schema/schema-factory)
|
|
58
|
+
- [typeGuards](https://livon.tech/docs/schema/type-guards)
|
|
59
|
+
|
|
60
|
+
## Type safety model
|
|
61
|
+
|
|
62
|
+
LIVON [schemas](https://livon.tech/docs/schema) are both runtime validators and type sources.
|
|
63
|
+
Primitive schema names are optional, so `string()` and `number()` are valid defaults.
|
|
64
|
+
|
|
65
|
+
1. Define payload shape once in [schema](https://livon.tech/docs/schema).
|
|
66
|
+
2. Validate unknown input with `parse()`.
|
|
67
|
+
3. Reuse schema entrypoint with `typed()`.
|
|
68
|
+
4. Derive types with `Infer` instead of hand-written payload interfaces.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import {object, string, number, type Infer} from '@livon/schema';
|
|
72
|
+
|
|
73
|
+
const User = object({
|
|
74
|
+
name: 'User',
|
|
75
|
+
shape: {
|
|
76
|
+
id: string(),
|
|
77
|
+
age: number().int().min(0),
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
type UserType = Infer<typeof User>;
|
|
82
|
+
|
|
83
|
+
const input: unknown = JSON.parse(raw);
|
|
84
|
+
const parsed = User.parse(input);
|
|
85
|
+
const typedInput: UserType = {id: 'u-1', age: 21};
|
|
86
|
+
const typed = User.typed(typedInput);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Parameters in this example
|
|
90
|
+
|
|
91
|
+
`object({...})`:
|
|
92
|
+
|
|
93
|
+
- `name` (`string`): schema node name.
|
|
94
|
+
- `shape` (`Record<string, Schema>`): field schema map.
|
|
95
|
+
|
|
96
|
+
`number().int().min(0)`:
|
|
97
|
+
|
|
98
|
+
- `min` (`number`): minimum allowed numeric value.
|
|
99
|
+
|
|
100
|
+
`User.parse(input)`:
|
|
101
|
+
|
|
102
|
+
- `input` (`unknown`): runtime value to validate/parse.
|
|
103
|
+
|
|
104
|
+
`User.typed(typedInput)`:
|
|
105
|
+
|
|
106
|
+
- `typedInput` (`UserType`): pretyped value using same schema entrypoint.
|
|
107
|
+
|
|
108
|
+
## Compose API schema
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import {api} from '@livon/schema';
|
|
112
|
+
|
|
113
|
+
const ApiSchema = api({
|
|
114
|
+
type: User,
|
|
115
|
+
operations: {
|
|
116
|
+
sendMessage,
|
|
117
|
+
},
|
|
118
|
+
subscriptions: {
|
|
119
|
+
onMessage,
|
|
120
|
+
},
|
|
121
|
+
fieldOperations: {
|
|
122
|
+
greeting: userGreetingResolver,
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
export const serverSchema = ApiSchema;
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Use the `api(...)` (or `composeApi(...)`) result directly in `schemaModule(...)`.
|
|
130
|
+
No additional schema-module input factory is required.
|
|
131
|
+
|
|
132
|
+
### Parameters in this example
|
|
133
|
+
|
|
134
|
+
`api({...})`:
|
|
135
|
+
|
|
136
|
+
- `type` (`Schema`, optional): entity schema for field operations.
|
|
137
|
+
- `operations` (`Record<string, Operation>`, optional): request/response operations.
|
|
138
|
+
- `subscriptions` (`Record<string, Subscription | Schema>`, optional): publish topic schemas.
|
|
139
|
+
- `fieldOperations` (`Record<string, FieldOperation>`, optional): field-level resolvers.
|
|
140
|
+
|
|
141
|
+
For focused usage patterns:
|
|
142
|
+
|
|
143
|
+
- [api](https://livon.tech/docs/schema/api)
|
|
144
|
+
- [operation](https://livon.tech/docs/schema/operation)
|
|
145
|
+
- [subscription](https://livon.tech/docs/schema/subscription)
|
|
146
|
+
- [fieldResolver](https://livon.tech/docs/schema/field-resolver)
|
|
147
|
+
|
|
148
|
+
## Mount schema module into runtime
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import {runtime} from '@livon/runtime';
|
|
152
|
+
import {schemaModule} from '@livon/schema';
|
|
153
|
+
|
|
154
|
+
runtime(schemaModule(serverSchema, {explain: true}));
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Parameters in this example
|
|
158
|
+
|
|
159
|
+
`schemaModule(serverSchema, options?)`:
|
|
160
|
+
|
|
161
|
+
- `serverSchema` (`Api | ComposedApi`): fully composed schema from `api(...)` or `composeApi(...)`.
|
|
162
|
+
- `options.explain` (`boolean`, optional): enables `$explain` endpoint for AST/checksum metadata.
|
|
163
|
+
|
|
164
|
+
## Explain endpoint
|
|
165
|
+
|
|
166
|
+
If `explain: true`, [schema module](https://livon.tech/docs/technical/runtime-design) responds to `$explain` with AST/checksum metadata.
|
|
167
|
+
|
|
168
|
+
## Related pages
|
|
169
|
+
|
|
170
|
+
- [Validated by Default](https://livon.tech/docs/core/validated-by-default)
|
|
171
|
+
- [parse vs typed](https://livon.tech/docs/core/parse-vs-typed)
|
|
172
|
+
- [Schema APIs overview](https://livon.tech/docs/schema)
|
|
173
|
+
- [Runtime Design](https://livon.tech/docs/technical/runtime-design)
|
|
174
|
+
- [Architecture](https://livon.tech/docs/technical/architecture)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Third-Party Notices
|
|
2
|
+
|
|
3
|
+
This package includes third-party software. The following licenses apply:
|
|
4
|
+
|
|
5
|
+
------------------------------------------------------------------------------
|
|
6
|
+
msgpackr
|
|
7
|
+
------------------------------------------------------------------------------
|
|
8
|
+
License: MIT
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2020 Kris Zyp
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
@@ -1 +1,41 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
createSchemaValidationError: ()=>createSchemaValidationError
|
|
28
|
+
});
|
|
29
|
+
const createSchemaValidationError = ({ issues, meta })=>{
|
|
30
|
+
const error = new Error('Schema validation failed');
|
|
31
|
+
error.issues = issues;
|
|
32
|
+
error.meta = meta;
|
|
33
|
+
return error;
|
|
34
|
+
};
|
|
35
|
+
exports.createSchemaValidationError = __webpack_exports__.createSchemaValidationError;
|
|
36
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
37
|
+
"createSchemaValidationError"
|
|
38
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
39
|
+
Object.defineProperty(exports, '__esModule', {
|
|
40
|
+
value: true
|
|
41
|
+
});
|
|
@@ -9,7 +9,7 @@ export type SchemaValidationError = Error & {
|
|
|
9
9
|
* @remarks
|
|
10
10
|
* Parameter and return types are defined in the TypeScript signature.
|
|
11
11
|
*
|
|
12
|
-
* @see https://
|
|
12
|
+
* @see https://livon.tech/docs/schema/type-safety
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* const result = createSchemaValidationError(undefined as never);
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
const createSchemaValidationError = ({ issues, meta })=>{
|
|
2
|
+
const error = new Error('Schema validation failed');
|
|
3
|
+
error.issues = issues;
|
|
4
|
+
error.meta = meta;
|
|
5
|
+
return error;
|
|
6
|
+
};
|
|
7
|
+
export { createSchemaValidationError };
|
package/dist/after.cjs
CHANGED
|
@@ -1 +1,36 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
after: ()=>after
|
|
28
|
+
});
|
|
29
|
+
const after = ({ schema, hook })=>schema.after(hook);
|
|
30
|
+
exports.after = __webpack_exports__.after;
|
|
31
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
32
|
+
"after"
|
|
33
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
34
|
+
Object.defineProperty(exports, '__esModule', {
|
|
35
|
+
value: true
|
|
36
|
+
});
|
package/dist/after.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export interface AfterInput<T> {
|
|
|
9
9
|
* @remarks
|
|
10
10
|
* Parameter and return types are defined in the TypeScript signature.
|
|
11
11
|
*
|
|
12
|
-
* @see https://
|
|
12
|
+
* @see https://livon.tech/docs/schema/after
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* // Postprocesses validated string output by converting it to uppercase.
|
package/dist/after.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
const after = ({ schema, hook })=>schema.after(hook);
|
|
2
|
+
export { after };
|
package/dist/and.cjs
CHANGED
|
@@ -1 +1,59 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
and: ()=>and
|
|
28
|
+
});
|
|
29
|
+
const isLegacyInput = (input)=>'left' in input && 'right' in input;
|
|
30
|
+
function and(input) {
|
|
31
|
+
if (isLegacyInput(input)) {
|
|
32
|
+
const { left, right, name } = input;
|
|
33
|
+
return void 0 === name ? left.and(right) : left.and(right, {
|
|
34
|
+
name
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const { schemas, name } = input;
|
|
38
|
+
if (schemas.length < 2) throw new Error('and() requires at least 2 schemas in the schemas array');
|
|
39
|
+
const [first, ...rest] = schemas;
|
|
40
|
+
const initial = first;
|
|
41
|
+
const lastIndex = rest.length - 1;
|
|
42
|
+
const reduced = rest.reduce((state, schema)=>({
|
|
43
|
+
schema: void 0 !== name && state.index === lastIndex ? state.schema.and(schema, {
|
|
44
|
+
name
|
|
45
|
+
}) : state.schema.and(schema),
|
|
46
|
+
index: state.index + 1
|
|
47
|
+
}), {
|
|
48
|
+
schema: initial,
|
|
49
|
+
index: 0
|
|
50
|
+
});
|
|
51
|
+
return reduced.schema;
|
|
52
|
+
}
|
|
53
|
+
exports.and = __webpack_exports__.and;
|
|
54
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
55
|
+
"and"
|
|
56
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
57
|
+
Object.defineProperty(exports, '__esModule', {
|
|
58
|
+
value: true
|
|
59
|
+
});
|
package/dist/and.d.ts
CHANGED
|
@@ -1,26 +1,63 @@
|
|
|
1
1
|
import { Schema } from './types.js';
|
|
2
|
-
|
|
2
|
+
type InferSchemasTuple<T extends readonly Schema<unknown>[]> = {
|
|
3
|
+
[K in keyof T]: T[K] extends Schema<infer U> ? U : never;
|
|
4
|
+
};
|
|
5
|
+
type IntersectTuple<T extends readonly unknown[]> = T extends readonly [infer Head, ...infer Tail] ? Head & IntersectTuple<Tail> : unknown;
|
|
6
|
+
export interface AndSchemaInput<TSchemas extends readonly Schema<unknown>[]> {
|
|
7
|
+
name?: string;
|
|
8
|
+
schemas: TSchemas;
|
|
9
|
+
}
|
|
10
|
+
export interface AndLegacyInput<T, U> {
|
|
3
11
|
left: Schema<T>;
|
|
4
12
|
right: Schema<U>;
|
|
13
|
+
name?: string;
|
|
5
14
|
}
|
|
15
|
+
type VariadicSchemaTuple = readonly [Schema<any>, Schema<any>, ...Schema<any>[]];
|
|
6
16
|
/**
|
|
7
17
|
* and is part of the public LIVON API.
|
|
8
18
|
*
|
|
9
19
|
* @remarks
|
|
10
20
|
* Parameter and return types are defined in the TypeScript signature.
|
|
11
21
|
*
|
|
12
|
-
* @see https://
|
|
22
|
+
* @see https://livon.tech/docs/schema/and
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Extends a base message input schema with an id field (legacy API).
|
|
26
|
+
* const messageInput = object({
|
|
27
|
+
* name: 'MessageInput',
|
|
28
|
+
* shape: { text: string() },
|
|
29
|
+
* });
|
|
30
|
+
* const withId = object({
|
|
31
|
+
* name: 'WithId',
|
|
32
|
+
* shape: { id: string() },
|
|
33
|
+
* });
|
|
34
|
+
* const MessageWithId = and({ left: messageInput, right: withId });
|
|
35
|
+
* MessageWithId.parse({ text: 'Hello', id: 'm-1' });
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Uses explicit naming for generated type surfaces (legacy API).
|
|
39
|
+
* const MessageWithId = and({
|
|
40
|
+
* left: messageInput,
|
|
41
|
+
* right: withId,
|
|
42
|
+
* name: 'MessageWithId',
|
|
43
|
+
* });
|
|
44
|
+
* MessageWithId.parse({ text: 'Hello', id: 'm-1' });
|
|
13
45
|
*
|
|
14
46
|
* @example
|
|
15
|
-
* //
|
|
16
|
-
* const
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
47
|
+
* // Composes multiple schemas using the schemas array (new API).
|
|
48
|
+
* const MessageWithId = and({
|
|
49
|
+
* name: 'MessageWithId',
|
|
50
|
+
* schemas: [messageInput, withId],
|
|
51
|
+
* });
|
|
52
|
+
* MessageWithId.parse({ text: 'Hello', id: 'm-1' });
|
|
20
53
|
*
|
|
21
54
|
* @example
|
|
22
|
-
* //
|
|
23
|
-
* const
|
|
24
|
-
*
|
|
55
|
+
* // Composes three schemas using the schemas array.
|
|
56
|
+
* const MessageComplete = and({
|
|
57
|
+
* schemas: [messageInput, withId, withTimestamp],
|
|
58
|
+
* });
|
|
59
|
+
* MessageComplete.parse({ text: 'Hello', id: 'm-1', timestamp: Date.now() });
|
|
25
60
|
*/
|
|
26
|
-
export declare
|
|
61
|
+
export declare function and<TSchemas extends VariadicSchemaTuple>(input: AndSchemaInput<TSchemas>): Schema<IntersectTuple<InferSchemasTuple<TSchemas>>>;
|
|
62
|
+
export declare function and<T, U>(input: AndLegacyInput<T, U>): Schema<T & U>;
|
|
63
|
+
export {};
|
package/dist/and.js
CHANGED
|
@@ -1 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
const isLegacyInput = (input)=>'left' in input && 'right' in input;
|
|
2
|
+
function and(input) {
|
|
3
|
+
if (isLegacyInput(input)) {
|
|
4
|
+
const { left, right, name } = input;
|
|
5
|
+
return void 0 === name ? left.and(right) : left.and(right, {
|
|
6
|
+
name
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
const { schemas, name } = input;
|
|
10
|
+
if (schemas.length < 2) throw new Error('and() requires at least 2 schemas in the schemas array');
|
|
11
|
+
const [first, ...rest] = schemas;
|
|
12
|
+
const initial = first;
|
|
13
|
+
const lastIndex = rest.length - 1;
|
|
14
|
+
const reduced = rest.reduce((state, schema)=>({
|
|
15
|
+
schema: void 0 !== name && state.index === lastIndex ? state.schema.and(schema, {
|
|
16
|
+
name
|
|
17
|
+
}) : state.schema.and(schema),
|
|
18
|
+
index: state.index + 1
|
|
19
|
+
}), {
|
|
20
|
+
schema: initial,
|
|
21
|
+
index: 0
|
|
22
|
+
});
|
|
23
|
+
return reduced.schema;
|
|
24
|
+
}
|
|
25
|
+
export { and };
|