@livon/schema 0.27.0-rc.1
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/PROMPT.md +21 -0
- package/README.md +13 -0
- package/SCHEMA.md +13 -0
- package/dist/SchemaValidationError.cjs +41 -0
- package/dist/SchemaValidationError.d.ts +20 -0
- package/dist/SchemaValidationError.js +7 -0
- package/dist/SchemaValidationError.spec.cjs +65 -0
- package/dist/SchemaValidationError.spec.d.ts +1 -0
- package/dist/SchemaValidationError.spec.js +59 -0
- package/dist/after.cjs +36 -0
- package/dist/after.d.ts +30 -0
- package/dist/after.js +2 -0
- package/dist/after.spec.cjs +54 -0
- package/dist/after.spec.d.ts +1 -0
- package/dist/after.spec.js +48 -0
- package/dist/and.cjs +36 -0
- package/dist/and.d.ts +26 -0
- package/dist/and.js +2 -0
- package/dist/and.spec.cjs +57 -0
- package/dist/and.spec.d.ts +1 -0
- package/dist/and.spec.js +51 -0
- package/dist/api.cjs +317 -0
- package/dist/api.d.ts +107 -0
- package/dist/api.js +277 -0
- package/dist/api.spec.cjs +512 -0
- package/dist/api.spec.d.ts +1 -0
- package/dist/api.spec.js +506 -0
- package/dist/array.cjs +74 -0
- package/dist/array.d.ts +25 -0
- package/dist/array.js +40 -0
- package/dist/array.spec.cjs +167 -0
- package/dist/array.spec.d.ts +1 -0
- package/dist/array.spec.js +161 -0
- package/dist/before.cjs +36 -0
- package/dist/before.d.ts +30 -0
- package/dist/before.js +2 -0
- package/dist/before.spec.cjs +54 -0
- package/dist/before.spec.d.ts +1 -0
- package/dist/before.spec.js +48 -0
- package/dist/binary.cjs +53 -0
- package/dist/binary.d.ts +24 -0
- package/dist/binary.js +19 -0
- package/dist/binary.spec.cjs +107 -0
- package/dist/binary.spec.d.ts +1 -0
- package/dist/binary.spec.js +101 -0
- package/dist/boolean.cjs +53 -0
- package/dist/boolean.d.ts +24 -0
- package/dist/boolean.js +19 -0
- package/dist/boolean.spec.cjs +96 -0
- package/dist/boolean.spec.d.ts +1 -0
- package/dist/boolean.spec.js +90 -0
- package/dist/context.cjs +125 -0
- package/dist/context.d.ts +101 -0
- package/dist/context.js +76 -0
- package/dist/context.spec.cjs +244 -0
- package/dist/context.spec.d.ts +1 -0
- package/dist/context.spec.js +238 -0
- package/dist/date.cjs +53 -0
- package/dist/date.d.ts +24 -0
- package/dist/date.js +19 -0
- package/dist/date.spec.cjs +97 -0
- package/dist/date.spec.d.ts +1 -0
- package/dist/date.spec.js +91 -0
- package/dist/doc.cjs +54 -0
- package/dist/doc.d.ts +25 -0
- package/dist/doc.js +17 -0
- package/dist/doc.spec.cjs +99 -0
- package/dist/doc.spec.d.ts +1 -0
- package/dist/doc.spec.js +93 -0
- package/dist/enumeration.cjs +74 -0
- package/dist/enumeration.d.ts +50 -0
- package/dist/enumeration.js +40 -0
- package/dist/enumeration.spec.cjs +110 -0
- package/dist/enumeration.spec.d.ts +1 -0
- package/dist/enumeration.spec.js +104 -0
- package/dist/hydrate.cjs +18 -0
- package/dist/hydrate.d.ts +1 -0
- package/dist/hydrate.js +0 -0
- package/dist/index.cjs +145 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +24 -0
- package/dist/index.spec.cjs +43 -0
- package/dist/index.spec.d.ts +1 -0
- package/dist/index.spec.js +37 -0
- package/dist/literal.cjs +55 -0
- package/dist/literal.d.ts +25 -0
- package/dist/literal.js +21 -0
- package/dist/literal.spec.cjs +93 -0
- package/dist/literal.spec.d.ts +1 -0
- package/dist/literal.spec.js +87 -0
- package/dist/number.cjs +89 -0
- package/dist/number.d.ts +84 -0
- package/dist/number.js +55 -0
- package/dist/number.spec.cjs +155 -0
- package/dist/number.spec.d.ts +1 -0
- package/dist/number.spec.js +149 -0
- package/dist/object.cjs +66 -0
- package/dist/object.d.ts +37 -0
- package/dist/object.js +32 -0
- package/dist/object.spec.cjs +171 -0
- package/dist/object.spec.d.ts +1 -0
- package/dist/object.spec.js +165 -0
- package/dist/operation.cjs +182 -0
- package/dist/operation.d.ts +197 -0
- package/dist/operation.js +133 -0
- package/dist/operation.spec.cjs +454 -0
- package/dist/operation.spec.d.ts +1 -0
- package/dist/operation.spec.js +448 -0
- package/dist/or.cjs +85 -0
- package/dist/or.d.ts +37 -0
- package/dist/or.js +51 -0
- package/dist/or.spec.cjs +204 -0
- package/dist/or.spec.d.ts +1 -0
- package/dist/or.spec.js +198 -0
- package/dist/schema.cjs +285 -0
- package/dist/schema.d.ts +132 -0
- package/dist/schema.js +233 -0
- package/dist/schema.spec.cjs +587 -0
- package/dist/schema.spec.d.ts +1 -0
- package/dist/schema.spec.js +581 -0
- package/dist/schemaFactory.cjs +125 -0
- package/dist/schemaFactory.d.ts +97 -0
- package/dist/schemaFactory.js +88 -0
- package/dist/schemaFactory.spec.cjs +197 -0
- package/dist/schemaFactory.spec.d.ts +1 -0
- package/dist/schemaFactory.spec.js +191 -0
- package/dist/schemaModule.cjs +280 -0
- package/dist/schemaModule.d.ts +97 -0
- package/dist/schemaModule.js +243 -0
- package/dist/schemaModule.spec.cjs +355 -0
- package/dist/schemaModule.spec.d.ts +1 -0
- package/dist/schemaModule.spec.js +349 -0
- package/dist/string.cjs +93 -0
- package/dist/string.d.ts +85 -0
- package/dist/string.js +59 -0
- package/dist/string.spec.cjs +158 -0
- package/dist/string.spec.d.ts +1 -0
- package/dist/string.spec.js +152 -0
- package/dist/testing/mocks/assertions.mock.cjs +48 -0
- package/dist/testing/mocks/assertions.mock.d.ts +5 -0
- package/dist/testing/mocks/assertions.mock.js +14 -0
- package/dist/testing/mocks/index.cjs +52 -0
- package/dist/testing/mocks/index.d.ts +4 -0
- package/dist/testing/mocks/index.js +3 -0
- package/dist/testing/mocks/schema.mock.cjs +120 -0
- package/dist/testing/mocks/schema.mock.d.ts +37 -0
- package/dist/testing/mocks/schema.mock.js +74 -0
- package/dist/tuple.cjs +58 -0
- package/dist/tuple.d.ts +33 -0
- package/dist/tuple.js +24 -0
- package/dist/tuple.spec.cjs +162 -0
- package/dist/tuple.spec.d.ts +1 -0
- package/dist/tuple.spec.js +156 -0
- package/dist/typeGuards.cjs +60 -0
- package/dist/typeGuards.d.ts +93 -0
- package/dist/typeGuards.js +8 -0
- package/dist/typeGuards.spec.cjs +101 -0
- package/dist/typeGuards.spec.d.ts +1 -0
- package/dist/typeGuards.spec.js +95 -0
- package/dist/types.cjs +18 -0
- package/dist/types.d.ts +289 -0
- package/dist/types.js +0 -0
- package/dist/union.cjs +74 -0
- package/dist/union.d.ts +33 -0
- package/dist/union.js +40 -0
- package/dist/union.spec.cjs +159 -0
- package/dist/union.spec.d.ts +1 -0
- package/dist/union.spec.js +153 -0
- package/package.json +47 -0
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { Schema, SchemaContext, SchemaIssue, SchemaResult, SchemaErrorMeta, SchemaValidate, AstNode } from './types.js';
|
|
2
|
+
import { TypeGuard } from './typeGuards.js';
|
|
3
|
+
export interface CreateIssueInput {
|
|
4
|
+
path: readonly (string | number)[];
|
|
5
|
+
message: string;
|
|
6
|
+
code?: string;
|
|
7
|
+
context?: Readonly<Record<string, unknown>>;
|
|
8
|
+
}
|
|
9
|
+
export interface OkInput<T> {
|
|
10
|
+
value: T;
|
|
11
|
+
}
|
|
12
|
+
export interface FailInput {
|
|
13
|
+
issues: readonly SchemaIssue[];
|
|
14
|
+
meta?: SchemaErrorMeta;
|
|
15
|
+
}
|
|
16
|
+
export interface SchemaAstInternal {
|
|
17
|
+
(ctx: SchemaContext): AstNode;
|
|
18
|
+
}
|
|
19
|
+
export interface CreateSchemaInput<T> {
|
|
20
|
+
name: string;
|
|
21
|
+
type: string;
|
|
22
|
+
ast: SchemaAstInternal;
|
|
23
|
+
validate: SchemaValidate<T>;
|
|
24
|
+
}
|
|
25
|
+
export interface CreateGuardSchemaInput<T> {
|
|
26
|
+
name: string;
|
|
27
|
+
type: string;
|
|
28
|
+
guard: TypeGuard<T>;
|
|
29
|
+
message: string;
|
|
30
|
+
code?: string;
|
|
31
|
+
}
|
|
32
|
+
export interface SchemaGuard<T> {
|
|
33
|
+
(input: unknown): input is T;
|
|
34
|
+
}
|
|
35
|
+
export interface CreatePrimitiveSchemaInput<T> {
|
|
36
|
+
name: string;
|
|
37
|
+
type: string;
|
|
38
|
+
guard: SchemaGuard<T>;
|
|
39
|
+
message: string;
|
|
40
|
+
code?: string;
|
|
41
|
+
}
|
|
42
|
+
export interface EnsureSchemaContextInput {
|
|
43
|
+
context?: SchemaContext;
|
|
44
|
+
request?: SchemaContext['getRequestContext'];
|
|
45
|
+
}
|
|
46
|
+
export interface EnsureSchemaContext {
|
|
47
|
+
(ctx?: SchemaContext): SchemaContext;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* createIssueForPath is part of the public LIVON API.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
54
|
+
*
|
|
55
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* const result = createIssueForPath(undefined as never);
|
|
59
|
+
*/
|
|
60
|
+
export declare const createIssueForPath: ({ path, message, code, context, }: CreateIssueInput) => SchemaIssue;
|
|
61
|
+
/**
|
|
62
|
+
* ok is part of the public LIVON API.
|
|
63
|
+
*
|
|
64
|
+
* @remarks
|
|
65
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
66
|
+
*
|
|
67
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* const result = ok(undefined as never);
|
|
71
|
+
*/
|
|
72
|
+
export declare const ok: <T>({ value }: OkInput<T>) => SchemaResult<T>;
|
|
73
|
+
/**
|
|
74
|
+
* fail is part of the public LIVON API.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
77
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
78
|
+
*
|
|
79
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* const result = fail(undefined as never);
|
|
83
|
+
*/
|
|
84
|
+
export declare const fail: ({ issues, meta }: FailInput) => SchemaResult<never>;
|
|
85
|
+
/**
|
|
86
|
+
* ensureSchemaContext is part of the public LIVON API.
|
|
87
|
+
*
|
|
88
|
+
* @remarks
|
|
89
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
90
|
+
*
|
|
91
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* const result = ensureSchemaContext(undefined as never);
|
|
95
|
+
*/
|
|
96
|
+
export declare const ensureSchemaContext: EnsureSchemaContext;
|
|
97
|
+
/**
|
|
98
|
+
* createSchema is part of the public LIVON API.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
102
|
+
*
|
|
103
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* const result = createSchema(undefined as never);
|
|
107
|
+
*/
|
|
108
|
+
export declare const createSchema: <T>({ name, type, ast, validate }: CreateSchemaInput<T>) => Schema<T>;
|
|
109
|
+
/**
|
|
110
|
+
* createGuardSchema is part of the public LIVON API.
|
|
111
|
+
*
|
|
112
|
+
* @remarks
|
|
113
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
114
|
+
*
|
|
115
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* const result = createGuardSchema(undefined as never);
|
|
119
|
+
*/
|
|
120
|
+
export declare const createGuardSchema: <T>({ name, type, guard, message, code }: CreateGuardSchemaInput<T>) => Schema<T>;
|
|
121
|
+
/**
|
|
122
|
+
* createPrimitiveSchema is part of the public LIVON API.
|
|
123
|
+
*
|
|
124
|
+
* @remarks
|
|
125
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
126
|
+
*
|
|
127
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* const result = createPrimitiveSchema(undefined as never);
|
|
131
|
+
*/
|
|
132
|
+
export declare const createPrimitiveSchema: <T>({ name, type, guard, message, code, }: CreatePrimitiveSchemaInput<T>) => Schema<T>;
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { createSchemaValidationError } from "./SchemaValidationError.js";
|
|
2
|
+
import { createSchemaContext, normalizeBuildContext, normalizeRequestContext } from "./context.js";
|
|
3
|
+
import { mergeDoc, normalizeDoc } from "./doc.js";
|
|
4
|
+
const createIssueForPath = ({ path, message, code, context })=>({
|
|
5
|
+
path,
|
|
6
|
+
message,
|
|
7
|
+
code,
|
|
8
|
+
context
|
|
9
|
+
});
|
|
10
|
+
const ok = ({ value })=>({
|
|
11
|
+
ok: true,
|
|
12
|
+
value
|
|
13
|
+
});
|
|
14
|
+
const fail = ({ issues, meta })=>({
|
|
15
|
+
ok: false,
|
|
16
|
+
issues,
|
|
17
|
+
meta
|
|
18
|
+
});
|
|
19
|
+
const ensureSchemaContext = (ctx)=>{
|
|
20
|
+
if (ctx) return ctx;
|
|
21
|
+
const request = normalizeRequestContext();
|
|
22
|
+
return createSchemaContext({
|
|
23
|
+
request
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
const createSchema = ({ name, type, ast, validate })=>{
|
|
27
|
+
const validateWithContext = (input, ctx)=>{
|
|
28
|
+
const context = ensureSchemaContext(ctx);
|
|
29
|
+
if (!context.getRequestContext()) context.setRequestContext(normalizeRequestContext());
|
|
30
|
+
const build = context.getBuildContext();
|
|
31
|
+
const request = context.getRequestContext();
|
|
32
|
+
const meta = {
|
|
33
|
+
build,
|
|
34
|
+
request,
|
|
35
|
+
type,
|
|
36
|
+
name
|
|
37
|
+
};
|
|
38
|
+
const result = validate(input, context);
|
|
39
|
+
return result.ok ? result : fail({
|
|
40
|
+
issues: result.issues,
|
|
41
|
+
meta
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
const parse = (input, ctx)=>{
|
|
45
|
+
const result = validateWithContext(input, ctx);
|
|
46
|
+
if (result.ok) return result.value;
|
|
47
|
+
throw createSchemaValidationError({
|
|
48
|
+
issues: result.issues,
|
|
49
|
+
meta: result.meta
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const typed = (input, ctx)=>parse(input, ctx);
|
|
53
|
+
const optional = ()=>createSchema({
|
|
54
|
+
name: `${name}.optional`,
|
|
55
|
+
type: `${type}.optional`,
|
|
56
|
+
ast,
|
|
57
|
+
validate: (input, ctx)=>void 0 === input ? ok({
|
|
58
|
+
value: void 0
|
|
59
|
+
}) : validate(input, ctx)
|
|
60
|
+
});
|
|
61
|
+
const nullable = ()=>createSchema({
|
|
62
|
+
name: `${name}.nullable`,
|
|
63
|
+
type: `${type}.nullable`,
|
|
64
|
+
ast,
|
|
65
|
+
validate: (input, ctx)=>null === input ? ok({
|
|
66
|
+
value: null
|
|
67
|
+
}) : validate(input, ctx)
|
|
68
|
+
});
|
|
69
|
+
const describe = (doc)=>{
|
|
70
|
+
const nextDoc = normalizeDoc(doc);
|
|
71
|
+
return createSchema({
|
|
72
|
+
name,
|
|
73
|
+
type,
|
|
74
|
+
ast: (ctx)=>{
|
|
75
|
+
const node = ast(ctx);
|
|
76
|
+
const merged = mergeDoc(node.doc, nextDoc);
|
|
77
|
+
return merged ? {
|
|
78
|
+
...node,
|
|
79
|
+
doc: merged
|
|
80
|
+
} : node;
|
|
81
|
+
},
|
|
82
|
+
validate
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
const refine = ({ predicate, message, code })=>createSchema({
|
|
86
|
+
name: `${name}.refine`,
|
|
87
|
+
type: `${type}.refine`,
|
|
88
|
+
ast,
|
|
89
|
+
validate: (input, ctx)=>{
|
|
90
|
+
const context = ensureSchemaContext(ctx);
|
|
91
|
+
const result = validate(input, context);
|
|
92
|
+
if (!result.ok) return result;
|
|
93
|
+
return predicate(result.value, context) ? result : fail({
|
|
94
|
+
issues: [
|
|
95
|
+
createIssueForPath({
|
|
96
|
+
path: [],
|
|
97
|
+
message,
|
|
98
|
+
code
|
|
99
|
+
})
|
|
100
|
+
]
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
const before = (hook)=>createSchema({
|
|
105
|
+
name: `${name}.before`,
|
|
106
|
+
type: `${type}.before`,
|
|
107
|
+
ast,
|
|
108
|
+
validate: (input, ctx)=>{
|
|
109
|
+
const context = ensureSchemaContext(ctx);
|
|
110
|
+
const result = hook(input, context);
|
|
111
|
+
const issues = 'object' == typeof result && null !== result && 'issues' in result ? result.issues ?? [] : [];
|
|
112
|
+
if (issues.length > 0) return fail({
|
|
113
|
+
issues
|
|
114
|
+
});
|
|
115
|
+
const nextInput = 'object' == typeof result && null !== result && 'input' in result ? result.input : result;
|
|
116
|
+
return validate(nextInput, context);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
const after = (hook)=>createSchema({
|
|
120
|
+
name: `${name}.after`,
|
|
121
|
+
type: `${type}.after`,
|
|
122
|
+
ast,
|
|
123
|
+
validate: (input, ctx)=>{
|
|
124
|
+
const context = ensureSchemaContext(ctx);
|
|
125
|
+
const result = validate(input, context);
|
|
126
|
+
if (!result.ok) return result;
|
|
127
|
+
const hookResult = hook(result.value, context);
|
|
128
|
+
const issues = 'object' == typeof hookResult && null !== hookResult && 'issues' in hookResult ? hookResult.issues ?? [] : [];
|
|
129
|
+
if (issues.length > 0) return fail({
|
|
130
|
+
issues
|
|
131
|
+
});
|
|
132
|
+
const nextValue = 'object' == typeof hookResult && null !== hookResult && 'value' in hookResult ? hookResult.value : hookResult;
|
|
133
|
+
return ok({
|
|
134
|
+
value: nextValue
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
const and = (other)=>createSchema({
|
|
139
|
+
name: `${name}.and`,
|
|
140
|
+
type: `${type}.and`,
|
|
141
|
+
ast: (ctx)=>({
|
|
142
|
+
type: 'and',
|
|
143
|
+
children: [
|
|
144
|
+
ast(ctx),
|
|
145
|
+
other.ast(ctx.getBuildContext())
|
|
146
|
+
]
|
|
147
|
+
}),
|
|
148
|
+
validate: (input, ctx)=>{
|
|
149
|
+
const context = ensureSchemaContext(ctx);
|
|
150
|
+
const left = validate(input, context);
|
|
151
|
+
const right = other.validate(input, context);
|
|
152
|
+
return left.ok && right.ok ? ok({
|
|
153
|
+
value: left.value
|
|
154
|
+
}) : fail({
|
|
155
|
+
issues: [
|
|
156
|
+
...left.ok ? [] : left.issues,
|
|
157
|
+
...right.ok ? [] : right.issues
|
|
158
|
+
]
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
const resolveAst = (ctx)=>{
|
|
163
|
+
const node = ast(ctx);
|
|
164
|
+
return node.name ? node : {
|
|
165
|
+
...node,
|
|
166
|
+
name
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
return {
|
|
170
|
+
name,
|
|
171
|
+
type,
|
|
172
|
+
ast: (input)=>{
|
|
173
|
+
const build = normalizeBuildContext(input ?? {});
|
|
174
|
+
const context = createSchemaContext({
|
|
175
|
+
build
|
|
176
|
+
});
|
|
177
|
+
return resolveAst(context);
|
|
178
|
+
},
|
|
179
|
+
validate: validateWithContext,
|
|
180
|
+
parse,
|
|
181
|
+
typed,
|
|
182
|
+
optional,
|
|
183
|
+
nullable,
|
|
184
|
+
describe,
|
|
185
|
+
refine,
|
|
186
|
+
before,
|
|
187
|
+
after,
|
|
188
|
+
and
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
const createGuardSchema = ({ name, type, guard, message, code })=>createSchema({
|
|
192
|
+
name,
|
|
193
|
+
type,
|
|
194
|
+
ast: ()=>({
|
|
195
|
+
type,
|
|
196
|
+
constraints: {
|
|
197
|
+
kind: 'guard'
|
|
198
|
+
}
|
|
199
|
+
}),
|
|
200
|
+
validate: (input)=>guard(input) ? ok({
|
|
201
|
+
value: input
|
|
202
|
+
}) : fail({
|
|
203
|
+
issues: [
|
|
204
|
+
createIssueForPath({
|
|
205
|
+
path: [],
|
|
206
|
+
message,
|
|
207
|
+
code
|
|
208
|
+
})
|
|
209
|
+
]
|
|
210
|
+
})
|
|
211
|
+
});
|
|
212
|
+
const createPrimitiveSchema = ({ name, type, guard, message, code })=>createSchema({
|
|
213
|
+
name,
|
|
214
|
+
type,
|
|
215
|
+
ast: ()=>({
|
|
216
|
+
type,
|
|
217
|
+
constraints: {
|
|
218
|
+
kind: 'primitive'
|
|
219
|
+
}
|
|
220
|
+
}),
|
|
221
|
+
validate: (input)=>guard(input) ? ok({
|
|
222
|
+
value: input
|
|
223
|
+
}) : fail({
|
|
224
|
+
issues: [
|
|
225
|
+
createIssueForPath({
|
|
226
|
+
path: [],
|
|
227
|
+
message,
|
|
228
|
+
code
|
|
229
|
+
})
|
|
230
|
+
]
|
|
231
|
+
})
|
|
232
|
+
});
|
|
233
|
+
export { createGuardSchema, createIssueForPath, createPrimitiveSchema, createSchema, ensureSchemaContext, fail, ok };
|