@oyinlola141/lattice-schema 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/README.md +19 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/schemaBase/index.d.ts +10 -0
- package/dist/schemaBase/index.d.ts.map +1 -0
- package/dist/schemaBase/index.js +9 -0
- package/dist/schemaBase/index.js.map +1 -0
- package/dist/schemaBase/schemaBase.context.d.ts +17 -0
- package/dist/schemaBase/schemaBase.context.d.ts.map +1 -0
- package/dist/schemaBase/schemaBase.context.js +44 -0
- package/dist/schemaBase/schemaBase.context.js.map +1 -0
- package/dist/schemaBase/schemaBase.core.d.ts +52 -0
- package/dist/schemaBase/schemaBase.core.d.ts.map +1 -0
- package/dist/schemaBase/schemaBase.core.js +99 -0
- package/dist/schemaBase/schemaBase.core.js.map +1 -0
- package/dist/schemaBase/schemaBase.result.d.ts +17 -0
- package/dist/schemaBase/schemaBase.result.d.ts.map +1 -0
- package/dist/schemaBase/schemaBase.result.js +29 -0
- package/dist/schemaBase/schemaBase.result.js.map +1 -0
- package/dist/schemaBase/schemaBase.type.d.ts +57 -0
- package/dist/schemaBase/schemaBase.type.d.ts.map +1 -0
- package/dist/schemaBase/schemaBase.type.js +7 -0
- package/dist/schemaBase/schemaBase.type.js.map +1 -0
- package/dist/schemaCoerce/index.d.ts +7 -0
- package/dist/schemaCoerce/index.d.ts.map +1 -0
- package/dist/schemaCoerce/index.js +7 -0
- package/dist/schemaCoerce/index.js.map +1 -0
- package/dist/schemaCoerce/schemaCoerce.core.d.ts +48 -0
- package/dist/schemaCoerce/schemaCoerce.core.d.ts.map +1 -0
- package/dist/schemaCoerce/schemaCoerce.core.js +126 -0
- package/dist/schemaCoerce/schemaCoerce.core.js.map +1 -0
- package/dist/schemaComposition/index.d.ts +10 -0
- package/dist/schemaComposition/index.d.ts.map +1 -0
- package/dist/schemaComposition/index.js +10 -0
- package/dist/schemaComposition/index.js.map +1 -0
- package/dist/schemaComposition/schemaEnum.core.d.ts +21 -0
- package/dist/schemaComposition/schemaEnum.core.d.ts.map +1 -0
- package/dist/schemaComposition/schemaEnum.core.js +41 -0
- package/dist/schemaComposition/schemaEnum.core.js.map +1 -0
- package/dist/schemaComposition/schemaIntersection.core.d.ts +21 -0
- package/dist/schemaComposition/schemaIntersection.core.d.ts.map +1 -0
- package/dist/schemaComposition/schemaIntersection.core.js +54 -0
- package/dist/schemaComposition/schemaIntersection.core.js.map +1 -0
- package/dist/schemaComposition/schemaLazy.core.d.ts +22 -0
- package/dist/schemaComposition/schemaLazy.core.d.ts.map +1 -0
- package/dist/schemaComposition/schemaLazy.core.js +33 -0
- package/dist/schemaComposition/schemaLazy.core.js.map +1 -0
- package/dist/schemaComposition/schemaUnion.core.d.ts +32 -0
- package/dist/schemaComposition/schemaUnion.core.d.ts.map +1 -0
- package/dist/schemaComposition/schemaUnion.core.js +89 -0
- package/dist/schemaComposition/schemaUnion.core.js.map +1 -0
- package/dist/schemaInference/index.d.ts +7 -0
- package/dist/schemaInference/index.d.ts.map +1 -0
- package/dist/schemaInference/index.js +7 -0
- package/dist/schemaInference/index.js.map +1 -0
- package/dist/schemaInference/schemaInfer.type.d.ts +27 -0
- package/dist/schemaInference/schemaInfer.type.d.ts.map +1 -0
- package/dist/schemaInference/schemaInfer.type.js +7 -0
- package/dist/schemaInference/schemaInfer.type.js.map +1 -0
- package/dist/schemaModifiers/index.d.ts +10 -0
- package/dist/schemaModifiers/index.d.ts.map +1 -0
- package/dist/schemaModifiers/index.js +10 -0
- package/dist/schemaModifiers/index.js.map +1 -0
- package/dist/schemaModifiers/schemaDefault.core.d.ts +20 -0
- package/dist/schemaModifiers/schemaDefault.core.d.ts.map +1 -0
- package/dist/schemaModifiers/schemaDefault.core.js +33 -0
- package/dist/schemaModifiers/schemaDefault.core.js.map +1 -0
- package/dist/schemaModifiers/schemaOptionalNullable.core.d.ts +30 -0
- package/dist/schemaModifiers/schemaOptionalNullable.core.d.ts.map +1 -0
- package/dist/schemaModifiers/schemaOptionalNullable.core.js +47 -0
- package/dist/schemaModifiers/schemaOptionalNullable.core.js.map +1 -0
- package/dist/schemaModifiers/schemaRefine.core.d.ts +21 -0
- package/dist/schemaModifiers/schemaRefine.core.d.ts.map +1 -0
- package/dist/schemaModifiers/schemaRefine.core.js +40 -0
- package/dist/schemaModifiers/schemaRefine.core.js.map +1 -0
- package/dist/schemaModifiers/schemaTransform.core.d.ts +20 -0
- package/dist/schemaModifiers/schemaTransform.core.d.ts.map +1 -0
- package/dist/schemaModifiers/schemaTransform.core.js +40 -0
- package/dist/schemaModifiers/schemaTransform.core.js.map +1 -0
- package/dist/schemaPrimitives/index.d.ts +12 -0
- package/dist/schemaPrimitives/index.d.ts.map +1 -0
- package/dist/schemaPrimitives/index.js +12 -0
- package/dist/schemaPrimitives/index.js.map +1 -0
- package/dist/schemaPrimitives/schemaBoolean.core.d.ts +26 -0
- package/dist/schemaPrimitives/schemaBoolean.core.d.ts.map +1 -0
- package/dist/schemaPrimitives/schemaBoolean.core.js +47 -0
- package/dist/schemaPrimitives/schemaBoolean.core.js.map +1 -0
- package/dist/schemaPrimitives/schemaLiteral.core.d.ts +19 -0
- package/dist/schemaPrimitives/schemaLiteral.core.d.ts.map +1 -0
- package/dist/schemaPrimitives/schemaLiteral.core.js +37 -0
- package/dist/schemaPrimitives/schemaLiteral.core.js.map +1 -0
- package/dist/schemaPrimitives/schemaNumber.core.d.ts +62 -0
- package/dist/schemaPrimitives/schemaNumber.core.d.ts.map +1 -0
- package/dist/schemaPrimitives/schemaNumber.core.js +191 -0
- package/dist/schemaPrimitives/schemaNumber.core.js.map +1 -0
- package/dist/schemaPrimitives/schemaSentinel.core.d.ts +53 -0
- package/dist/schemaPrimitives/schemaSentinel.core.d.ts.map +1 -0
- package/dist/schemaPrimitives/schemaSentinel.core.js +101 -0
- package/dist/schemaPrimitives/schemaSentinel.core.js.map +1 -0
- package/dist/schemaPrimitives/schemaString.core.d.ts +74 -0
- package/dist/schemaPrimitives/schemaString.core.d.ts.map +1 -0
- package/dist/schemaPrimitives/schemaString.core.js +208 -0
- package/dist/schemaPrimitives/schemaString.core.js.map +1 -0
- package/dist/schemaPrimitives/schemaTransform.core.d.ts +13 -0
- package/dist/schemaPrimitives/schemaTransform.core.d.ts.map +1 -0
- package/dist/schemaPrimitives/schemaTransform.core.js +31 -0
- package/dist/schemaPrimitives/schemaTransform.core.js.map +1 -0
- package/dist/schemaRoot/index.d.ts +7 -0
- package/dist/schemaRoot/index.d.ts.map +1 -0
- package/dist/schemaRoot/index.js +7 -0
- package/dist/schemaRoot/index.js.map +1 -0
- package/dist/schemaRoot/schemaRoot.namespace.d.ts +72 -0
- package/dist/schemaRoot/schemaRoot.namespace.d.ts.map +1 -0
- package/dist/schemaRoot/schemaRoot.namespace.js +79 -0
- package/dist/schemaRoot/schemaRoot.namespace.js.map +1 -0
- package/dist/schemaStructures/index.d.ts +12 -0
- package/dist/schemaStructures/index.d.ts.map +1 -0
- package/dist/schemaStructures/index.js +11 -0
- package/dist/schemaStructures/index.js.map +1 -0
- package/dist/schemaStructures/schemaArray.core.d.ts +36 -0
- package/dist/schemaStructures/schemaArray.core.d.ts.map +1 -0
- package/dist/schemaStructures/schemaArray.core.js +105 -0
- package/dist/schemaStructures/schemaArray.core.js.map +1 -0
- package/dist/schemaStructures/schemaMapSet.core.d.ts +31 -0
- package/dist/schemaStructures/schemaMapSet.core.d.ts.map +1 -0
- package/dist/schemaStructures/schemaMapSet.core.js +77 -0
- package/dist/schemaStructures/schemaMapSet.core.js.map +1 -0
- package/dist/schemaStructures/schemaObject.core.d.ts +66 -0
- package/dist/schemaStructures/schemaObject.core.d.ts.map +1 -0
- package/dist/schemaStructures/schemaObject.core.js +184 -0
- package/dist/schemaStructures/schemaObject.core.js.map +1 -0
- package/dist/schemaStructures/schemaRecord.core.d.ts +20 -0
- package/dist/schemaStructures/schemaRecord.core.d.ts.map +1 -0
- package/dist/schemaStructures/schemaRecord.core.js +66 -0
- package/dist/schemaStructures/schemaRecord.core.js.map +1 -0
- package/dist/schemaStructures/schemaTuple.core.d.ts +24 -0
- package/dist/schemaStructures/schemaTuple.core.d.ts.map +1 -0
- package/dist/schemaStructures/schemaTuple.core.js +60 -0
- package/dist/schemaStructures/schemaTuple.core.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @latticejs/schema/composition/intersection
|
|
3
|
+
*
|
|
4
|
+
* Intersection schema — validates against all schemas, merging results.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
import { addIssue } from "../schemaBase/index.js";
|
|
8
|
+
import { SchemaIssueCode } from "@latticejs/constants";
|
|
9
|
+
/**
|
|
10
|
+
* Schema that accepts values matching all provided schemas.
|
|
11
|
+
* Results are merged left to right.
|
|
12
|
+
*/
|
|
13
|
+
export class IntersectionSchema extends Schema {
|
|
14
|
+
_left;
|
|
15
|
+
_right;
|
|
16
|
+
_type = "intersection";
|
|
17
|
+
constructor(_left, _right) {
|
|
18
|
+
super();
|
|
19
|
+
this._left = _left;
|
|
20
|
+
this._right = _right;
|
|
21
|
+
}
|
|
22
|
+
_parse(ctx, input) {
|
|
23
|
+
let leftResult;
|
|
24
|
+
try {
|
|
25
|
+
leftResult = this._left._parse(ctx, input);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
addIssue(ctx, {
|
|
29
|
+
code: SchemaIssueCode.INVALID_UNION,
|
|
30
|
+
path: [...ctx.path],
|
|
31
|
+
message: "Intersection left schema failed",
|
|
32
|
+
});
|
|
33
|
+
throw new Error("Validation failed");
|
|
34
|
+
}
|
|
35
|
+
let rightResult;
|
|
36
|
+
try {
|
|
37
|
+
rightResult = this._right._parse(ctx, input);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
addIssue(ctx, {
|
|
41
|
+
code: SchemaIssueCode.INVALID_UNION,
|
|
42
|
+
path: [...ctx.path],
|
|
43
|
+
message: "Intersection right schema failed",
|
|
44
|
+
});
|
|
45
|
+
throw new Error("Validation failed");
|
|
46
|
+
}
|
|
47
|
+
return { ...leftResult, ...rightResult };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Creates an intersection schema from two schemas. */
|
|
51
|
+
export function intersectionSchema(left, right) {
|
|
52
|
+
return new IntersectionSchema(left, right);
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=schemaIntersection.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaIntersection.core.js","sourceRoot":"","sources":["../../src/schemaComposition/schemaIntersection.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD;;;GAGG;AACH,MAAM,OAAO,kBAAkC,SAAQ,MAAsB;IAIxD,KAAK;IACL,MAAM;IAJT,KAAK,GAAG,cAAc,CAAC;IAEvC,YACmB,KAAoB,EACpB,MAAsB;QAEvC,KAAK,EAAE,CAAC;qBAHS,KAAK;sBACL,MAAM;IAGzB,CAAC;IAEM,MAAM,CAAC,GAAuB,EAAE,KAAc;QACnD,IAAI,UAAiB,CAAC;QACtB,IAAI,CAAC;YACH,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC7C,CAAC;QAAC,MAAM,CAAC;YACP,QAAQ,CAAC,GAAG,EAAE;gBACZ,IAAI,EAAE,eAAe,CAAC,aAAa;gBACnC,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC;gBACnB,OAAO,EAAE,iCAAiC;aAC3C,CAAC,CAAC;YACH,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACvC,CAAC;QAED,IAAI,WAAmB,CAAC;QACxB,IAAI,CAAC;YACH,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC/C,CAAC;QAAC,MAAM,CAAC;YACP,QAAQ,CAAC,GAAG,EAAE;gBACZ,IAAI,EAAE,eAAe,CAAC,aAAa;gBACnC,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC;gBACnB,OAAO,EAAE,kCAAkC;aAC5C,CAAC,CAAC;YACH,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACvC,CAAC;QAED,OAAO,EAAE,GAAG,UAAU,EAAE,GAAG,WAAW,EAAoB,CAAC;IAC7D,CAAC;CACF;AAED,uDAAuD;AACvD,MAAM,UAAU,kBAAkB,CAChC,IAAmB,EACnB,KAAqB;IAErB,OAAO,IAAI,kBAAkB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oyinlola141/lattice-schema/composition/lazy
|
|
3
|
+
*
|
|
4
|
+
* Lazy schema for recursive and self-referencing data structures.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
import type { SchemaParseContext } from "../schemaBase/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Schema that defers creation of the inner schema until parse time.
|
|
10
|
+
* Essential for recursive data structures like trees and nested comments.
|
|
11
|
+
*/
|
|
12
|
+
export declare class LazySchema<TOutput, TInput = TOutput> extends Schema<TOutput, TInput> {
|
|
13
|
+
readonly _type = "lazy";
|
|
14
|
+
private readonly _factory;
|
|
15
|
+
private _inner;
|
|
16
|
+
constructor(factory: () => Schema<TOutput, TInput>);
|
|
17
|
+
private _resolve;
|
|
18
|
+
_parse(ctx: SchemaParseContext, input: TInput): TOutput;
|
|
19
|
+
}
|
|
20
|
+
/** Creates a lazy schema from a factory function. */
|
|
21
|
+
export declare function lazySchema<TOutput, TInput = TOutput>(factory: () => Schema<TOutput, TInput>): LazySchema<TOutput, TInput>;
|
|
22
|
+
//# sourceMappingURL=schemaLazy.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaLazy.core.d.ts","sourceRoot":"","sources":["../../src/schemaComposition/schemaLazy.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAEjE;;;GAGG;AACH,qBAAa,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAE,SAAQ,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC;IAChF,SAAgB,KAAK,UAAU;IAC/B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgC;IACzD,OAAO,CAAC,MAAM,CAAsC;IAEpD,YAAY,OAAO,EAAE,MAAM,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,EAGjD;IAED,OAAO,CAAC,QAAQ;IAOT,MAAM,CAAC,GAAG,EAAE,kBAAkB,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAE7D;CACF;AAED,qDAAqD;AACrD,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,EAClD,OAAO,EAAE,MAAM,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,GACrC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAE7B"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @latticejs/schema/composition/lazy
|
|
3
|
+
*
|
|
4
|
+
* Lazy schema for recursive and self-referencing data structures.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
/**
|
|
8
|
+
* Schema that defers creation of the inner schema until parse time.
|
|
9
|
+
* Essential for recursive data structures like trees and nested comments.
|
|
10
|
+
*/
|
|
11
|
+
export class LazySchema extends Schema {
|
|
12
|
+
_type = "lazy";
|
|
13
|
+
_factory;
|
|
14
|
+
_inner;
|
|
15
|
+
constructor(factory) {
|
|
16
|
+
super();
|
|
17
|
+
this._factory = factory;
|
|
18
|
+
}
|
|
19
|
+
_resolve() {
|
|
20
|
+
if (!this._inner) {
|
|
21
|
+
this._inner = this._factory();
|
|
22
|
+
}
|
|
23
|
+
return this._inner;
|
|
24
|
+
}
|
|
25
|
+
_parse(ctx, input) {
|
|
26
|
+
return this._resolve()._parse(ctx, input);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Creates a lazy schema from a factory function. */
|
|
30
|
+
export function lazySchema(factory) {
|
|
31
|
+
return new LazySchema(factory);
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=schemaLazy.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaLazy.core.js","sourceRoot":"","sources":["../../src/schemaComposition/schemaLazy.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAGhD;;;GAGG;AACH,MAAM,OAAO,UAAsC,SAAQ,MAAuB;IAChE,KAAK,GAAG,MAAM,CAAC;IACd,QAAQ,CAAgC;IACjD,MAAM,CAAsC;IAEpD,YAAY,OAAsC;QAChD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAEO,QAAQ;QACd,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;QAChC,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAEM,MAAM,CAAC,GAAuB,EAAE,KAAa;QAClD,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAC5C,CAAC;CACF;AAED,qDAAqD;AACrD,MAAM,UAAU,UAAU,CACxB,OAAsC;IAEtC,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC,CAAC;AACjC,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oyinlola141/lattice-schema/composition/union
|
|
3
|
+
*
|
|
4
|
+
* Union schema — validates against multiple schemas, returning the first match.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
import type { SchemaParseContext } from "../schemaBase/index.js";
|
|
8
|
+
/** Helper type to infer union output from schema array. */
|
|
9
|
+
type UnionOutput<TSchemas extends readonly Schema<unknown>[]> = TSchemas[number] extends Schema<infer U> ? U : never;
|
|
10
|
+
/**
|
|
11
|
+
* Schema that accepts values matching any of the provided schemas.
|
|
12
|
+
*/
|
|
13
|
+
export declare class UnionSchema<TSchemas extends readonly Schema<unknown>[]> extends Schema<UnionOutput<TSchemas>> {
|
|
14
|
+
private readonly _schemas;
|
|
15
|
+
readonly _type = "union";
|
|
16
|
+
constructor(_schemas: TSchemas);
|
|
17
|
+
_parse(ctx: SchemaParseContext, input: unknown): UnionOutput<TSchemas>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Discriminated union schema — uses a discriminator key to select the right schema.
|
|
21
|
+
*/
|
|
22
|
+
export declare class DiscriminatedUnionSchema<K extends string, TSchemas extends readonly Schema<Record<string, unknown>>[]> extends Schema<UnionOutput<TSchemas>> {
|
|
23
|
+
private readonly _discriminator;
|
|
24
|
+
readonly _type = "discriminatedUnion";
|
|
25
|
+
private readonly _schemaMap;
|
|
26
|
+
constructor(_discriminator: K, schemas: TSchemas);
|
|
27
|
+
_parse(ctx: SchemaParseContext, input: unknown): UnionOutput<TSchemas>;
|
|
28
|
+
}
|
|
29
|
+
/** Creates a union schema. */
|
|
30
|
+
export declare function unionSchema<T extends readonly Schema<unknown>[]>(schemas: T): UnionSchema<T>;
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=schemaUnion.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaUnion.core.d.ts","sourceRoot":"","sources":["../../src/schemaComposition/schemaUnion.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAIjE,2DAA2D;AAC3D,KAAK,WAAW,CAAC,QAAQ,SAAS,SAAS,MAAM,CAAC,OAAO,CAAC,EAAE,IAC1D,QAAQ,CAAC,MAAM,CAAC,SAAS,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAEvD;;GAEG;AACH,qBAAa,WAAW,CAAC,QAAQ,SAAS,SAAS,MAAM,CAAC,OAAO,CAAC,EAAE,CAAE,SAAQ,MAAM,CAClF,WAAW,CAAC,QAAQ,CAAC,CACtB;IAGa,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAFrC,SAAgB,KAAK,WAAW;IAEhC,YAA6B,QAAQ,EAAE,QAAQ,EAE9C;IAEM,MAAM,CAAC,GAAG,EAAE,kBAAkB,EAAE,KAAK,EAAE,OAAO,GAAG,WAAW,CAAC,QAAQ,CAAC,CAsB5E;CACF;AAED;;GAEG;AACH,qBAAa,wBAAwB,CACnC,CAAC,SAAS,MAAM,EAChB,QAAQ,SAAS,SAAS,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EAAE,CAC3D,SAAQ,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;IAKnC,OAAO,CAAC,QAAQ,CAAC,cAAc;IAJjC,SAAgB,KAAK,wBAAwB;IAC7C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA+B;IAE1D,YACmB,cAAc,EAAE,CAAC,EAClC,OAAO,EAAE,QAAQ,EAOlB;IAEM,MAAM,CAAC,GAAG,EAAE,kBAAkB,EAAE,KAAK,EAAE,OAAO,GAAG,WAAW,CAAC,QAAQ,CAAC,CA4B5E;CACF;AAED,8BAA8B;AAC9B,wBAAgB,WAAW,CAAC,CAAC,SAAS,SAAS,MAAM,CAAC,OAAO,CAAC,EAAE,EAC9D,OAAO,EAAE,CAAC,GACT,WAAW,CAAC,CAAC,CAAC,CAEhB"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @latticejs/schema/composition/union
|
|
3
|
+
*
|
|
4
|
+
* Union schema — validates against multiple schemas, returning the first match.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
import { addIssue, createParseContext } from "../schemaBase/index.js";
|
|
8
|
+
import { SchemaIssueCode } from "@latticejs/constants";
|
|
9
|
+
/**
|
|
10
|
+
* Schema that accepts values matching any of the provided schemas.
|
|
11
|
+
*/
|
|
12
|
+
export class UnionSchema extends Schema {
|
|
13
|
+
_schemas;
|
|
14
|
+
_type = "union";
|
|
15
|
+
constructor(_schemas) {
|
|
16
|
+
super();
|
|
17
|
+
this._schemas = _schemas;
|
|
18
|
+
}
|
|
19
|
+
_parse(ctx, input) {
|
|
20
|
+
for (const schema of this._schemas) {
|
|
21
|
+
const childCtx = createParseContext({
|
|
22
|
+
maxDepth: ctx.options.maxDepth,
|
|
23
|
+
path: [...ctx.path],
|
|
24
|
+
});
|
|
25
|
+
try {
|
|
26
|
+
const result = schema._parse(childCtx, input);
|
|
27
|
+
if (childCtx.issues.length === 0) {
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// Continue to next schema
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
addIssue(ctx, {
|
|
36
|
+
code: SchemaIssueCode.INVALID_UNION,
|
|
37
|
+
path: [...ctx.path],
|
|
38
|
+
message: `Invalid union: no matching schema found`,
|
|
39
|
+
});
|
|
40
|
+
throw new Error("Validation failed");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Discriminated union schema — uses a discriminator key to select the right schema.
|
|
45
|
+
*/
|
|
46
|
+
export class DiscriminatedUnionSchema extends Schema {
|
|
47
|
+
_discriminator;
|
|
48
|
+
_type = "discriminatedUnion";
|
|
49
|
+
_schemaMap;
|
|
50
|
+
constructor(_discriminator, schemas) {
|
|
51
|
+
super();
|
|
52
|
+
this._discriminator = _discriminator;
|
|
53
|
+
this._schemaMap = new Map();
|
|
54
|
+
for (const schema of schemas) {
|
|
55
|
+
this._schemaMap.set(String(schema._type), schema);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
_parse(ctx, input) {
|
|
59
|
+
if (typeof input !== "object" || input === null) {
|
|
60
|
+
addIssue(ctx, {
|
|
61
|
+
code: SchemaIssueCode.INVALID_TYPE,
|
|
62
|
+
path: [...ctx.path],
|
|
63
|
+
message: `Expected object for discriminated union, received ${typeof input}`,
|
|
64
|
+
expected: "object",
|
|
65
|
+
received: typeof input,
|
|
66
|
+
});
|
|
67
|
+
throw new Error("Validation failed");
|
|
68
|
+
}
|
|
69
|
+
const obj = input;
|
|
70
|
+
const discriminatorValue = String(obj[this._discriminator]);
|
|
71
|
+
const schema = this._schemaMap.get(discriminatorValue);
|
|
72
|
+
if (schema) {
|
|
73
|
+
return schema._parse(ctx, input);
|
|
74
|
+
}
|
|
75
|
+
addIssue(ctx, {
|
|
76
|
+
code: SchemaIssueCode.INVALID_UNION,
|
|
77
|
+
path: [...ctx.path],
|
|
78
|
+
message: `No matching variant for discriminator "${this._discriminator}" = "${discriminatorValue}"`,
|
|
79
|
+
expected: `one of [${[...this._schemaMap.keys()].join(", ")}]`,
|
|
80
|
+
received: discriminatorValue,
|
|
81
|
+
});
|
|
82
|
+
throw new Error("Validation failed");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Creates a union schema. */
|
|
86
|
+
export function unionSchema(schemas) {
|
|
87
|
+
return new UnionSchema(schemas);
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=schemaUnion.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaUnion.core.js","sourceRoot":"","sources":["../../src/schemaComposition/schemaUnion.core.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/schemaInference/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EAAE,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/schemaInference/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oyinlola141/lattice-schema/inference
|
|
3
|
+
*
|
|
4
|
+
* Type inference utilities for extracting TypeScript types from schemas.
|
|
5
|
+
*/
|
|
6
|
+
import type { Schema } from "../schemaBase/index.js";
|
|
7
|
+
/**
|
|
8
|
+
* Infers the output type of a schema.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const UserSchema = schema.object({ name: schema.string() });
|
|
12
|
+
* type User = Infer<typeof UserSchema>; // { name: string }
|
|
13
|
+
*/
|
|
14
|
+
export type Infer<TSchema> = TSchema extends Schema<infer TOutput, unknown> ? TOutput : TSchema extends Schema<infer TOutput> ? TOutput : never;
|
|
15
|
+
/**
|
|
16
|
+
* Infers the input type of a schema.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const schema = schema.string().transform(Number);
|
|
20
|
+
* type Input = Input<typeof schema>; // string
|
|
21
|
+
*/
|
|
22
|
+
export type SchemaInput<TSchema> = TSchema extends Schema<unknown, infer TInput> ? TInput : TSchema extends Schema<infer TOutput> ? TOutput : never;
|
|
23
|
+
/**
|
|
24
|
+
* Infers the output type of a schema (alias for Infer).
|
|
25
|
+
*/
|
|
26
|
+
export type SchemaOutput<TSchema> = Infer<TSchema>;
|
|
27
|
+
//# sourceMappingURL=schemaInfer.type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaInfer.type.d.ts","sourceRoot":"","sources":["../../src/schemaInference/schemaInfer.type.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAErD;;;;;;GAMG;AACH,MAAM,MAAM,KAAK,CAAC,OAAO,IAAI,OAAO,SAAS,MAAM,CAAC,MAAM,OAAO,EAAE,OAAO,CAAC,GACvE,OAAO,GACP,OAAO,SAAS,MAAM,CAAC,MAAM,OAAO,CAAC,GACnC,OAAO,GACP,KAAK,CAAC;AAEZ;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,CAAC,OAAO,IAAI,OAAO,SAAS,MAAM,CAAC,OAAO,EAAE,MAAM,MAAM,CAAC,GAC5E,MAAM,GACN,OAAO,SAAS,MAAM,CAAC,MAAM,OAAO,CAAC,GACnC,OAAO,GACP,KAAK,CAAC;AAEZ;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaInfer.type.js","sourceRoot":"","sources":["../../src/schemaInference/schemaInfer.type.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oyinlola141/lattice-schema/modifiers
|
|
3
|
+
*
|
|
4
|
+
* Schema modifiers: optional, nullable, default, refine, transform.
|
|
5
|
+
*/
|
|
6
|
+
export { OptionalModifierSchema, NullableModifierSchema, optionalSchema, nullableSchema, } from "./schemaOptionalNullable.core.js";
|
|
7
|
+
export { DefaultSchema, defaultSchema } from "./schemaDefault.core.js";
|
|
8
|
+
export { RefineSchema, refineSchema } from "./schemaRefine.core.js";
|
|
9
|
+
export { TransformModifierSchema, transformSchema } from "./schemaTransform.core.js";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/schemaModifiers/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EACtB,cAAc,EACd,cAAc,GACf,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACvE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACpE,OAAO,EAAE,uBAAuB,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @latticejs/schema/modifiers
|
|
3
|
+
*
|
|
4
|
+
* Schema modifiers: optional, nullable, default, refine, transform.
|
|
5
|
+
*/
|
|
6
|
+
export { OptionalModifierSchema, NullableModifierSchema, optionalSchema, nullableSchema, } from "./schemaOptionalNullable.core.js";
|
|
7
|
+
export { DefaultSchema, defaultSchema } from "./schemaDefault.core.js";
|
|
8
|
+
export { RefineSchema, refineSchema } from "./schemaRefine.core.js";
|
|
9
|
+
export { TransformModifierSchema, transformSchema } from "./schemaTransform.core.js";
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/schemaModifiers/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EACtB,cAAc,EACd,cAAc,GACf,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACvE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACpE,OAAO,EAAE,uBAAuB,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oyinlola141/lattice-schema/modifiers/default
|
|
3
|
+
*
|
|
4
|
+
* Default value schema — applies a default when input is undefined.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
import type { SchemaParseContext } from "../schemaBase/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Schema that applies a default value when the input is undefined.
|
|
10
|
+
*/
|
|
11
|
+
export declare class DefaultSchema<TOutput> extends Schema<TOutput> {
|
|
12
|
+
private readonly _inner;
|
|
13
|
+
private readonly _defaultValue;
|
|
14
|
+
readonly _type = "default";
|
|
15
|
+
constructor(_inner: Schema<TOutput>, _defaultValue: TOutput | (() => TOutput));
|
|
16
|
+
_parse(ctx: SchemaParseContext, input: unknown): TOutput;
|
|
17
|
+
}
|
|
18
|
+
/** Applies a default value when input is undefined. */
|
|
19
|
+
export declare function defaultSchema<T>(schema: Schema<T>, defaultValue: T | (() => T)): DefaultSchema<T>;
|
|
20
|
+
//# sourceMappingURL=schemaDefault.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaDefault.core.d.ts","sourceRoot":"","sources":["../../src/schemaModifiers/schemaDefault.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAEjE;;GAEG;AACH,qBAAa,aAAa,CAAC,OAAO,CAAE,SAAQ,MAAM,CAAC,OAAO,CAAC;IAIvD,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,aAAa;IAJhC,SAAgB,KAAK,aAAa;IAElC,YACmB,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,EACvB,aAAa,EAAE,OAAO,GAAG,CAAC,MAAM,OAAO,CAAC,EAG1D;IAEM,MAAM,CAAC,GAAG,EAAE,kBAAkB,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAS9D;CACF;AAED,uDAAuD;AACvD,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAEjG"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @latticejs/schema/modifiers/default
|
|
3
|
+
*
|
|
4
|
+
* Default value schema — applies a default when input is undefined.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
/**
|
|
8
|
+
* Schema that applies a default value when the input is undefined.
|
|
9
|
+
*/
|
|
10
|
+
export class DefaultSchema extends Schema {
|
|
11
|
+
_inner;
|
|
12
|
+
_defaultValue;
|
|
13
|
+
_type = "default";
|
|
14
|
+
constructor(_inner, _defaultValue) {
|
|
15
|
+
super();
|
|
16
|
+
this._inner = _inner;
|
|
17
|
+
this._defaultValue = _defaultValue;
|
|
18
|
+
}
|
|
19
|
+
_parse(ctx, input) {
|
|
20
|
+
if (input === undefined) {
|
|
21
|
+
const defaultVal = typeof this._defaultValue === "function"
|
|
22
|
+
? this._defaultValue()
|
|
23
|
+
: this._defaultValue;
|
|
24
|
+
return this._inner._parse(ctx, defaultVal);
|
|
25
|
+
}
|
|
26
|
+
return this._inner._parse(ctx, input);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Applies a default value when input is undefined. */
|
|
30
|
+
export function defaultSchema(schema, defaultValue) {
|
|
31
|
+
return new DefaultSchema(schema, defaultValue);
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=schemaDefault.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaDefault.core.js","sourceRoot":"","sources":["../../src/schemaModifiers/schemaDefault.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAGhD;;GAEG;AACH,MAAM,OAAO,aAAuB,SAAQ,MAAe;IAItC,MAAM;IACN,aAAa;IAJhB,KAAK,GAAG,SAAS,CAAC;IAElC,YACmB,MAAuB,EACvB,aAAwC;QAEzD,KAAK,EAAE,CAAC;sBAHS,MAAM;6BACN,aAAa;IAGhC,CAAC;IAEM,MAAM,CAAC,GAAuB,EAAE,KAAc;QACnD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,MAAM,UAAU,GACd,OAAO,IAAI,CAAC,aAAa,KAAK,UAAU;gBACtC,CAAC,CAAE,IAAI,CAAC,aAA+B,EAAE;gBACzC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC;YACzB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;CACF;AAED,uDAAuD;AACvD,MAAM,UAAU,aAAa,CAAI,MAAiB,EAAE,YAA2B;IAC7E,OAAO,IAAI,aAAa,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oyinlola141/lattice-schema/modifiers/optional-nullable
|
|
3
|
+
*
|
|
4
|
+
* Optional and nullable schema wrappers.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
import type { SchemaParseContext } from "../schemaBase/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Marks a schema as optional (accepts undefined).
|
|
10
|
+
*/
|
|
11
|
+
export declare class OptionalModifierSchema<T> extends Schema<T | undefined> {
|
|
12
|
+
private readonly _inner;
|
|
13
|
+
readonly _type = "optional";
|
|
14
|
+
constructor(_inner: Schema<T>);
|
|
15
|
+
_parse(ctx: SchemaParseContext, input: unknown): T | undefined;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Marks a schema as nullable (accepts null).
|
|
19
|
+
*/
|
|
20
|
+
export declare class NullableModifierSchema<T> extends Schema<T | null> {
|
|
21
|
+
private readonly _inner;
|
|
22
|
+
readonly _type = "nullable";
|
|
23
|
+
constructor(_inner: Schema<T>);
|
|
24
|
+
_parse(ctx: SchemaParseContext, input: unknown): T | null;
|
|
25
|
+
}
|
|
26
|
+
/** Makes a schema optional. */
|
|
27
|
+
export declare function optionalSchema<T>(schema: Schema<T>): OptionalModifierSchema<T>;
|
|
28
|
+
/** Makes a schema nullable. */
|
|
29
|
+
export declare function nullableSchema<T>(schema: Schema<T>): NullableModifierSchema<T>;
|
|
30
|
+
//# sourceMappingURL=schemaOptionalNullable.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaOptionalNullable.core.d.ts","sourceRoot":"","sources":["../../src/schemaModifiers/schemaOptionalNullable.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAEjE;;GAEG;AACH,qBAAa,sBAAsB,CAAC,CAAC,CAAE,SAAQ,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC;IAGtD,OAAO,CAAC,QAAQ,CAAC,MAAM;IAFnC,SAAgB,KAAK,cAAc;IAEnC,YAA6B,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAE7C;IAEM,MAAM,CAAC,GAAG,EAAE,kBAAkB,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,GAAG,SAAS,CAGpE;CACF;AAED;;GAEG;AACH,qBAAa,sBAAsB,CAAC,CAAC,CAAE,SAAQ,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC;IAGjD,OAAO,CAAC,QAAQ,CAAC,MAAM;IAFnC,SAAgB,KAAK,cAAc;IAEnC,YAA6B,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAE7C;IAEM,MAAM,CAAC,GAAG,EAAE,kBAAkB,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,GAAG,IAAI,CAG/D;CACF;AAED,+BAA+B;AAC/B,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,sBAAsB,CAAC,CAAC,CAAC,CAE9E;AAED,+BAA+B;AAC/B,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,sBAAsB,CAAC,CAAC,CAAC,CAE9E"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @latticejs/schema/modifiers/optional-nullable
|
|
3
|
+
*
|
|
4
|
+
* Optional and nullable schema wrappers.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
/**
|
|
8
|
+
* Marks a schema as optional (accepts undefined).
|
|
9
|
+
*/
|
|
10
|
+
export class OptionalModifierSchema extends Schema {
|
|
11
|
+
_inner;
|
|
12
|
+
_type = "optional";
|
|
13
|
+
constructor(_inner) {
|
|
14
|
+
super();
|
|
15
|
+
this._inner = _inner;
|
|
16
|
+
}
|
|
17
|
+
_parse(ctx, input) {
|
|
18
|
+
if (input === undefined)
|
|
19
|
+
return undefined;
|
|
20
|
+
return this._inner._parse(ctx, input);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Marks a schema as nullable (accepts null).
|
|
25
|
+
*/
|
|
26
|
+
export class NullableModifierSchema extends Schema {
|
|
27
|
+
_inner;
|
|
28
|
+
_type = "nullable";
|
|
29
|
+
constructor(_inner) {
|
|
30
|
+
super();
|
|
31
|
+
this._inner = _inner;
|
|
32
|
+
}
|
|
33
|
+
_parse(ctx, input) {
|
|
34
|
+
if (input === null)
|
|
35
|
+
return null;
|
|
36
|
+
return this._inner._parse(ctx, input);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Makes a schema optional. */
|
|
40
|
+
export function optionalSchema(schema) {
|
|
41
|
+
return new OptionalModifierSchema(schema);
|
|
42
|
+
}
|
|
43
|
+
/** Makes a schema nullable. */
|
|
44
|
+
export function nullableSchema(schema) {
|
|
45
|
+
return new NullableModifierSchema(schema);
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=schemaOptionalNullable.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaOptionalNullable.core.js","sourceRoot":"","sources":["../../src/schemaModifiers/schemaOptionalNullable.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAGhD;;GAEG;AACH,MAAM,OAAO,sBAA0B,SAAQ,MAAqB;IAGrC,MAAM;IAFnB,KAAK,GAAG,UAAU,CAAC;IAEnC,YAA6B,MAAiB;QAC5C,KAAK,EAAE,CAAC;sBADmB,MAAM;IAEnC,CAAC;IAEM,MAAM,CAAC,GAAuB,EAAE,KAAc;QACnD,IAAI,KAAK,KAAK,SAAS;YAAE,OAAO,SAAS,CAAC;QAC1C,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,sBAA0B,SAAQ,MAAgB;IAGhC,MAAM;IAFnB,KAAK,GAAG,UAAU,CAAC;IAEnC,YAA6B,MAAiB;QAC5C,KAAK,EAAE,CAAC;sBADmB,MAAM;IAEnC,CAAC;IAEM,MAAM,CAAC,GAAuB,EAAE,KAAc;QACnD,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO,IAAI,CAAC;QAChC,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;CACF;AAED,+BAA+B;AAC/B,MAAM,UAAU,cAAc,CAAI,MAAiB;IACjD,OAAO,IAAI,sBAAsB,CAAC,MAAM,CAAC,CAAC;AAC5C,CAAC;AAED,+BAA+B;AAC/B,MAAM,UAAU,cAAc,CAAI,MAAiB;IACjD,OAAO,IAAI,sBAAsB,CAAC,MAAM,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oyinlola141/lattice-schema/modifiers/refine
|
|
3
|
+
*
|
|
4
|
+
* Refine schema — adds custom synchronous validation logic.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
import type { SchemaParseContext } from "../schemaBase/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Schema that adds a custom refinement check to another schema.
|
|
10
|
+
*/
|
|
11
|
+
export declare class RefineSchema<T> extends Schema<T> {
|
|
12
|
+
private readonly _inner;
|
|
13
|
+
private readonly _check;
|
|
14
|
+
private readonly _message;
|
|
15
|
+
readonly _type = "refine";
|
|
16
|
+
constructor(_inner: Schema<T>, _check: (value: T) => boolean, _message: string);
|
|
17
|
+
_parse(ctx: SchemaParseContext, input: unknown): T;
|
|
18
|
+
}
|
|
19
|
+
/** Adds a custom refinement check to a schema. */
|
|
20
|
+
export declare function refineSchema<T>(schema: Schema<T>, check: (value: T) => boolean, message: string): RefineSchema<T>;
|
|
21
|
+
//# sourceMappingURL=schemaRefine.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaRefine.core.d.ts","sourceRoot":"","sources":["../../src/schemaModifiers/schemaRefine.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAIjE;;GAEG;AACH,qBAAa,YAAY,CAAC,CAAC,CAAE,SAAQ,MAAM,CAAC,CAAC,CAAC;IAI1C,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAL3B,SAAgB,KAAK,YAAY;IAEjC,YACmB,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EACjB,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,EAC7B,QAAQ,EAAE,MAAM,EAGlC;IAEM,MAAM,CAAC,GAAG,EAAE,kBAAkB,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,CAaxD;CACF;AAED,kDAAkD;AAClD,wBAAgB,YAAY,CAAC,CAAC,EAC5B,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EACjB,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,EAC5B,OAAO,EAAE,MAAM,GACd,YAAY,CAAC,CAAC,CAAC,CAEjB"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @latticejs/schema/modifiers/refine
|
|
3
|
+
*
|
|
4
|
+
* Refine schema — adds custom synchronous validation logic.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
import { addIssue } from "../schemaBase/index.js";
|
|
8
|
+
import { SchemaIssueCode } from "@latticejs/constants";
|
|
9
|
+
/**
|
|
10
|
+
* Schema that adds a custom refinement check to another schema.
|
|
11
|
+
*/
|
|
12
|
+
export class RefineSchema extends Schema {
|
|
13
|
+
_inner;
|
|
14
|
+
_check;
|
|
15
|
+
_message;
|
|
16
|
+
_type = "refine";
|
|
17
|
+
constructor(_inner, _check, _message) {
|
|
18
|
+
super();
|
|
19
|
+
this._inner = _inner;
|
|
20
|
+
this._check = _check;
|
|
21
|
+
this._message = _message;
|
|
22
|
+
}
|
|
23
|
+
_parse(ctx, input) {
|
|
24
|
+
const value = this._inner._parse(ctx, input);
|
|
25
|
+
if (!this._check(value)) {
|
|
26
|
+
addIssue(ctx, {
|
|
27
|
+
code: SchemaIssueCode.CUSTOM,
|
|
28
|
+
path: [...ctx.path],
|
|
29
|
+
message: this._message,
|
|
30
|
+
});
|
|
31
|
+
throw new Error("Validation failed");
|
|
32
|
+
}
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Adds a custom refinement check to a schema. */
|
|
37
|
+
export function refineSchema(schema, check, message) {
|
|
38
|
+
return new RefineSchema(schema, check, message);
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=schemaRefine.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaRefine.core.js","sourceRoot":"","sources":["../../src/schemaModifiers/schemaRefine.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD;;GAEG;AACH,MAAM,OAAO,YAAgB,SAAQ,MAAS;IAIzB,MAAM;IACN,MAAM;IACN,QAAQ;IALX,KAAK,GAAG,QAAQ,CAAC;IAEjC,YACmB,MAAiB,EACjB,MAA6B,EAC7B,QAAgB;QAEjC,KAAK,EAAE,CAAC;sBAJS,MAAM;sBACN,MAAM;wBACN,QAAQ;IAG3B,CAAC;IAEM,MAAM,CAAC,GAAuB,EAAE,KAAc;QACnD,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAE7C,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACxB,QAAQ,CAAC,GAAG,EAAE;gBACZ,IAAI,EAAE,eAAe,CAAC,MAAM;gBAC5B,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC;gBACnB,OAAO,EAAE,IAAI,CAAC,QAAQ;aACvB,CAAC,CAAC;YACH,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACvC,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAED,kDAAkD;AAClD,MAAM,UAAU,YAAY,CAC1B,MAAiB,EACjB,KAA4B,EAC5B,OAAe;IAEf,OAAO,IAAI,YAAY,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;AAClD,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oyinlola141/lattice-schema/modifiers/transform
|
|
3
|
+
*
|
|
4
|
+
* Transform modifier — applies a transformation function to validated output.
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "../schemaBase/index.js";
|
|
7
|
+
import type { SchemaParseContext } from "../schemaBase/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Schema that transforms the output of an inner schema.
|
|
10
|
+
*/
|
|
11
|
+
export declare class TransformModifierSchema<TInput, TOutput> extends Schema<TOutput, TInput> {
|
|
12
|
+
private readonly _inner;
|
|
13
|
+
private readonly _fn;
|
|
14
|
+
readonly _type = "transform";
|
|
15
|
+
constructor(_inner: Schema<TInput>, _fn: (value: TInput) => TOutput);
|
|
16
|
+
_parse(ctx: SchemaParseContext, input: TInput): TOutput;
|
|
17
|
+
}
|
|
18
|
+
/** Adds a transformation to a schema. */
|
|
19
|
+
export declare function transformSchema<TInput, TOutput>(schema: Schema<TInput>, fn: (value: TInput) => TOutput): TransformModifierSchema<TInput, TOutput>;
|
|
20
|
+
//# sourceMappingURL=schemaTransform.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schemaTransform.core.d.ts","sourceRoot":"","sources":["../../src/schemaModifiers/schemaTransform.core.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAIjE;;GAEG;AACH,qBAAa,uBAAuB,CAAC,MAAM,EAAE,OAAO,CAAE,SAAQ,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC;IAIjF,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,GAAG;IAJtB,SAAgB,KAAK,eAAe;IAEpC,YACmB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,EACtB,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,EAGjD;IAEM,MAAM,CAAC,GAAG,EAAE,kBAAkB,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAY7D;CACF;AAED,yCAAyC;AACzC,wBAAgB,eAAe,CAAC,MAAM,EAAE,OAAO,EAC7C,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,EACtB,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,GAC7B,uBAAuB,CAAC,MAAM,EAAE,OAAO,CAAC,CAE1C"}
|