@atproto/lex-schema 0.0.10 → 0.0.12
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 +26 -0
- package/dist/core/$type.d.ts +149 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +44 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +44 -0
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js +30 -0
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/result.d.ts +85 -4
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +60 -4
- package/dist/core/result.js.map +1 -1
- package/dist/core/schema.d.ts +229 -2
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +197 -4
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +244 -11
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +150 -0
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.d.ts +90 -3
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/validation-error.d.ts +61 -1
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +60 -0
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.d.ts +61 -0
- package/dist/core/validation-issue.d.ts.map +1 -1
- package/dist/core/validation-issue.js +51 -0
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +347 -10
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +184 -3
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +13 -25
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +2 -2
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +45 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +14 -0
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +46 -0
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +39 -0
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +28 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +28 -0
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +38 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +32 -0
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +38 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +33 -0
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +66 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +54 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +44 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +44 -0
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +58 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +45 -0
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +48 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +48 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +42 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +36 -0
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +54 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +49 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +44 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +44 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +42 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +42 -0
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +29 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +29 -0
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +41 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +41 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +56 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +51 -0
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +42 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +42 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +89 -7
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +84 -10
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +111 -15
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +70 -0
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +58 -0
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +50 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +42 -0
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +39 -0
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +64 -0
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +64 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +55 -0
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +55 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +63 -8
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +20 -0
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +50 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +17 -0
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +58 -9
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +44 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +44 -0
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +50 -0
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +41 -0
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +72 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +59 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +47 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +47 -0
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +62 -8
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +18 -0
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +53 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +15 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +50 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +50 -1
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +45 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +40 -0
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +34 -0
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +31 -0
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +33 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +33 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +44 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +44 -0
- package/dist/schema/with-default.js.map +1 -1
- package/package.json +4 -4
- package/src/core/$type.ts +150 -18
- package/src/core/record-key.ts +44 -0
- package/src/core/result.ts +86 -4
- package/src/core/schema.ts +236 -7
- package/src/core/string-format.ts +259 -13
- package/src/core/types.ts +91 -3
- package/src/core/validation-error.ts +60 -0
- package/src/core/validation-issue.ts +65 -0
- package/src/core/validator.ts +351 -10
- package/src/helpers.test.ts +110 -29
- package/src/helpers.ts +14 -14
- package/src/schema/array.test.ts +94 -79
- package/src/schema/array.ts +45 -0
- package/src/schema/blob.ts +46 -0
- package/src/schema/boolean.ts +28 -0
- package/src/schema/bytes.ts +38 -0
- package/src/schema/cid.ts +38 -0
- package/src/schema/custom.ts +66 -1
- package/src/schema/dict.ts +44 -0
- package/src/schema/discriminated-union.ts +58 -0
- package/src/schema/enum.ts +48 -0
- package/src/schema/integer.ts +42 -0
- package/src/schema/intersection.ts +54 -0
- package/src/schema/literal.ts +44 -0
- package/src/schema/never.ts +42 -0
- package/src/schema/null.ts +29 -0
- package/src/schema/nullable.ts +41 -0
- package/src/schema/object.ts +56 -0
- package/src/schema/optional.ts +42 -0
- package/src/schema/params.test.ts +58 -2
- package/src/schema/params.ts +124 -16
- package/src/schema/payload.test.ts +3 -3
- package/src/schema/payload.ts +142 -38
- package/src/schema/permission-set.ts +58 -0
- package/src/schema/permission.ts +42 -0
- package/src/schema/procedure.ts +64 -0
- package/src/schema/query.ts +55 -0
- package/src/schema/record.ts +63 -8
- package/src/schema/ref.ts +50 -0
- package/src/schema/refine.ts +58 -9
- package/src/schema/regexp.ts +44 -0
- package/src/schema/string.ts +50 -0
- package/src/schema/subscription.ts +72 -2
- package/src/schema/token.ts +47 -0
- package/src/schema/typed-object.ts +62 -8
- package/src/schema/typed-ref.ts +53 -0
- package/src/schema/typed-union.ts +55 -2
- package/src/schema/union.ts +45 -0
- package/src/schema/unknown-object.ts +34 -0
- package/src/schema/unknown.ts +33 -0
- package/src/schema/with-default.ts +44 -0
|
@@ -3,6 +3,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.IntersectionSchema = void 0;
|
|
4
4
|
exports.intersection = intersection;
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema for combining an object schema with a dictionary schema.
|
|
8
|
+
*
|
|
9
|
+
* Validates that the input matches both the fixed object shape and allows
|
|
10
|
+
* additional properties that match the dictionary schema. Properties defined
|
|
11
|
+
* in the object schema are validated by the object, and remaining properties
|
|
12
|
+
* are validated by the dictionary.
|
|
13
|
+
*
|
|
14
|
+
* @template Left - The ObjectSchema type for fixed properties
|
|
15
|
+
* @template Right - The DictSchema type for additional properties
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const schema = new IntersectionSchema(
|
|
20
|
+
* l.object({ name: l.string() }),
|
|
21
|
+
* l.dict(l.string(), l.integer())
|
|
22
|
+
* )
|
|
23
|
+
* // Validates: { name: 'test', score: 100, count: 5 }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
6
26
|
class IntersectionSchema extends core_js_1.Schema {
|
|
7
27
|
left;
|
|
8
28
|
right;
|
|
@@ -21,6 +41,35 @@ class IntersectionSchema extends core_js_1.Schema {
|
|
|
21
41
|
}
|
|
22
42
|
}
|
|
23
43
|
exports.IntersectionSchema = IntersectionSchema;
|
|
44
|
+
/**
|
|
45
|
+
* Creates an intersection schema combining fixed object properties with dynamic dictionary properties.
|
|
46
|
+
*
|
|
47
|
+
* Useful for objects that have a known set of properties plus additional
|
|
48
|
+
* arbitrary properties that follow a pattern.
|
|
49
|
+
*
|
|
50
|
+
* @param left - Object schema defining the fixed, known properties
|
|
51
|
+
* @param right - Dictionary schema for validating additional properties
|
|
52
|
+
* @returns A new {@link IntersectionSchema} instance
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* // Object with fixed and dynamic properties
|
|
57
|
+
* const configSchema = l.intersection(
|
|
58
|
+
* l.object({
|
|
59
|
+
* version: l.integer(),
|
|
60
|
+
* name: l.string(),
|
|
61
|
+
* }),
|
|
62
|
+
* l.dict(l.string(), l.string()) // Additional string properties
|
|
63
|
+
* )
|
|
64
|
+
*
|
|
65
|
+
* configSchema.parse({
|
|
66
|
+
* version: 1,
|
|
67
|
+
* name: 'my-config',
|
|
68
|
+
* customField: 'value',
|
|
69
|
+
* anotherField: 'another',
|
|
70
|
+
* })
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
24
73
|
/*@__NO_SIDE_EFFECTS__*/
|
|
25
74
|
function intersection(left, right) {
|
|
26
75
|
return new IntersectionSchema(left, right);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intersection.js","sourceRoot":"","sources":["../../src/schema/intersection.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"intersection.js","sourceRoot":"","sources":["../../src/schema/intersection.ts"],"names":[],"mappings":";;;AAyGA,oCAKC;AA9GD,wCAMmB;AAyBnB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAa,kBAGX,SAAQ,gBAGT;IAEsB;IACA;IAFrB,YACqB,IAAU,EACV,KAAY;QAE/B,KAAK,EAAE,CAAA;QAHY,SAAI,GAAJ,IAAI,CAAM;QACV,UAAK,GAAL,KAAK,CAAO;IAGjC,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,MAAM,UAAU,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;QACjD,IAAI,CAAC,UAAU,CAAC,OAAO;YAAE,OAAO,UAAU,CAAA;QAE1C,OAAO,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,UAAU,CAAC,KAAK,EAAE,GAAG,EAAE;YACzD,WAAW,EAAE,IAAI,CAAC,IAAI,CAAC,aAAa;SACrC,CAAC,CAAA;IACJ,CAAC;CACF;AAtBD,gDAsBC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAwB;AACxB,SAAgB,YAAY,CAG1B,IAAU,EAAE,KAAY;IACxB,OAAO,IAAI,kBAAkB,CAAc,IAAI,EAAE,KAAK,CAAC,CAAA;AACzD,CAAC","sourcesContent":["import {\n InferInput,\n InferOutput,\n Schema,\n Simplify,\n ValidationContext,\n} from '../core.js'\nimport { DictSchema } from './dict.js'\nimport { ObjectSchema } from './object.js'\n\n/**\n * Type utility for computing the intersection of two object types.\n *\n * Allows to more accurately represent the intersection of two object types\n * where both types may share some keys, and one of them uses an index\n * signature.\n *\n * @template A - First object type (typically from ObjectSchema)\n * @template B - Second object type (typically from DictSchema)\n *\n * @see {@link https://www.typescriptlang.org/play/?#code/C4TwDgpgBAglC8UDeUBmB7dAuKByARgIYBOuUAvlAGTJQDaA+lAJYB2UAzsMWwOYC6OVgFcAtvgjEKAKGkATCAGMANiWiL0rLlEI4YsjVuBQA1hBA4uPVrwRQARBnT2Dm7QDdCy4dESE6ZiD8UAD0IVAi4pJQABQcABbowspyUBIORMT2AJSyEAAeYOjExqCQUACSrMCSHErAzJoAPNJQsFAFNaxyHFAASkrFck1WfAA0UMKsJqzoAO6sAHxjrVAAQh35XT39g8TDozYTUzPzSyuLdqtwVKttMYHoqO00j88bnRDdvawQ7pJ3NpQAD860BbRwSHBQLadAA0ix2G91oJ1vDggAfWABcxPF5QOH8aFtci5aRlaAwVDMfIQVKIKo1Yh1RQNZq0Jw4AgkMjkCYoRiIzjcPioyISKTkRayBQqNRQQzaQgAMRpdL01NpclcRignm8EFVWrsKrVchxQVC4XF0SxmSAA Playground link}\n */\nexport type Intersect<A, B> = B[keyof B] extends never\n ? A\n : keyof A & keyof B extends never\n ? // If A and B don't overlap, just return A & B\n A & B\n : // Otherwise, properly represent the fact that accessing using an\n // index signature could return a value from either A or B\n A & { [K in keyof B]: B[K] | A[keyof A & K] }\n\n/**\n * Schema for combining an object schema with a dictionary schema.\n *\n * Validates that the input matches both the fixed object shape and allows\n * additional properties that match the dictionary schema. Properties defined\n * in the object schema are validated by the object, and remaining properties\n * are validated by the dictionary.\n *\n * @template Left - The ObjectSchema type for fixed properties\n * @template Right - The DictSchema type for additional properties\n *\n * @example\n * ```ts\n * const schema = new IntersectionSchema(\n * l.object({ name: l.string() }),\n * l.dict(l.string(), l.integer())\n * )\n * // Validates: { name: 'test', score: 100, count: 5 }\n * ```\n */\nexport class IntersectionSchema<\n const Left extends ObjectSchema = any,\n const Right extends DictSchema = any,\n> extends Schema<\n Simplify<Intersect<InferInput<Left>, InferInput<Right>>>,\n Simplify<Intersect<InferOutput<Left>, InferOutput<Right>>>\n> {\n constructor(\n protected readonly left: Left,\n protected readonly right: Right,\n ) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n const leftResult = ctx.validate(input, this.left)\n if (!leftResult.success) return leftResult\n\n return this.right.validateInContext(leftResult.value, ctx, {\n ignoredKeys: this.left.validatorsMap,\n })\n }\n}\n\n/**\n * Creates an intersection schema combining fixed object properties with dynamic dictionary properties.\n *\n * Useful for objects that have a known set of properties plus additional\n * arbitrary properties that follow a pattern.\n *\n * @param left - Object schema defining the fixed, known properties\n * @param right - Dictionary schema for validating additional properties\n * @returns A new {@link IntersectionSchema} instance\n *\n * @example\n * ```ts\n * // Object with fixed and dynamic properties\n * const configSchema = l.intersection(\n * l.object({\n * version: l.integer(),\n * name: l.string(),\n * }),\n * l.dict(l.string(), l.string()) // Additional string properties\n * )\n *\n * configSchema.parse({\n * version: 1,\n * name: 'my-config',\n * customField: 'value',\n * anotherField: 'another',\n * })\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function intersection<\n const Left extends ObjectSchema,\n const Right extends DictSchema,\n>(left: Left, right: Right) {\n return new IntersectionSchema<Left, Right>(left, right)\n}\n"]}
|
package/dist/schema/literal.d.ts
CHANGED
|
@@ -1,8 +1,52 @@
|
|
|
1
1
|
import { Schema, ValidationContext } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Schema that only accepts a specific literal value.
|
|
4
|
+
*
|
|
5
|
+
* Validates that the input is exactly equal to the specified value using
|
|
6
|
+
* strict equality (===).
|
|
7
|
+
*
|
|
8
|
+
* @template TValue - The literal type (null, string, number, or boolean)
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const schema = new LiteralSchema('admin')
|
|
13
|
+
* schema.validate('admin') // success
|
|
14
|
+
* schema.validate('user') // fails
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
2
17
|
export declare class LiteralSchema<const TValue extends null | string | number | boolean> extends Schema<TValue> {
|
|
3
18
|
readonly value: TValue;
|
|
4
19
|
constructor(value: TValue);
|
|
5
20
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<TValue>;
|
|
6
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a literal schema that only accepts the exact specified value.
|
|
24
|
+
*
|
|
25
|
+
* Useful for discriminator fields in unions, constant values, or type narrowing.
|
|
26
|
+
*
|
|
27
|
+
* @param value - The exact value that must be matched
|
|
28
|
+
* @returns A new {@link LiteralSchema} instance
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* // String literal
|
|
33
|
+
* const roleSchema = l.literal('admin')
|
|
34
|
+
*
|
|
35
|
+
* // Number literal
|
|
36
|
+
* const versionSchema = l.literal(1)
|
|
37
|
+
*
|
|
38
|
+
* // Boolean literal
|
|
39
|
+
* const enabledSchema = l.literal(true)
|
|
40
|
+
*
|
|
41
|
+
* // Null literal
|
|
42
|
+
* const nullSchema = l.literal(null)
|
|
43
|
+
*
|
|
44
|
+
* // In discriminated unions
|
|
45
|
+
* const actionSchema = l.discriminatedUnion('type', [
|
|
46
|
+
* l.object({ type: l.literal('create'), data: l.unknown() }),
|
|
47
|
+
* l.object({ type: l.literal('delete'), id: l.string() }),
|
|
48
|
+
* ])
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
7
51
|
export declare function literal<const V extends null | string | number | boolean>(value: V): LiteralSchema<V>;
|
|
8
52
|
//# sourceMappingURL=literal.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"literal.d.ts","sourceRoot":"","sources":["../../src/schema/literal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEtD,qBAAa,aAAa,CACxB,KAAK,CAAC,MAAM,SAAS,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CACrD,SAAQ,MAAM,CAAC,MAAM,CAAC;IACV,QAAQ,CAAC,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;IAIlC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;
|
|
1
|
+
{"version":3,"file":"literal.d.ts","sourceRoot":"","sources":["../../src/schema/literal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEtD;;;;;;;;;;;;;;GAcG;AACH,qBAAa,aAAa,CACxB,KAAK,CAAC,MAAM,SAAS,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CACrD,SAAQ,MAAM,CAAC,MAAM,CAAC;IACV,QAAQ,CAAC,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;IAIlC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,wBAAgB,OAAO,CAAC,KAAK,CAAC,CAAC,SAAS,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,EACtE,KAAK,EAAE,CAAC,oBAGT"}
|
package/dist/schema/literal.js
CHANGED
|
@@ -3,6 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.LiteralSchema = void 0;
|
|
4
4
|
exports.literal = literal;
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema that only accepts a specific literal value.
|
|
8
|
+
*
|
|
9
|
+
* Validates that the input is exactly equal to the specified value using
|
|
10
|
+
* strict equality (===).
|
|
11
|
+
*
|
|
12
|
+
* @template TValue - The literal type (null, string, number, or boolean)
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const schema = new LiteralSchema('admin')
|
|
17
|
+
* schema.validate('admin') // success
|
|
18
|
+
* schema.validate('user') // fails
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
6
21
|
class LiteralSchema extends core_js_1.Schema {
|
|
7
22
|
value;
|
|
8
23
|
constructor(value) {
|
|
@@ -17,6 +32,35 @@ class LiteralSchema extends core_js_1.Schema {
|
|
|
17
32
|
}
|
|
18
33
|
}
|
|
19
34
|
exports.LiteralSchema = LiteralSchema;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a literal schema that only accepts the exact specified value.
|
|
37
|
+
*
|
|
38
|
+
* Useful for discriminator fields in unions, constant values, or type narrowing.
|
|
39
|
+
*
|
|
40
|
+
* @param value - The exact value that must be matched
|
|
41
|
+
* @returns A new {@link LiteralSchema} instance
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* // String literal
|
|
46
|
+
* const roleSchema = l.literal('admin')
|
|
47
|
+
*
|
|
48
|
+
* // Number literal
|
|
49
|
+
* const versionSchema = l.literal(1)
|
|
50
|
+
*
|
|
51
|
+
* // Boolean literal
|
|
52
|
+
* const enabledSchema = l.literal(true)
|
|
53
|
+
*
|
|
54
|
+
* // Null literal
|
|
55
|
+
* const nullSchema = l.literal(null)
|
|
56
|
+
*
|
|
57
|
+
* // In discriminated unions
|
|
58
|
+
* const actionSchema = l.discriminatedUnion('type', [
|
|
59
|
+
* l.object({ type: l.literal('create'), data: l.unknown() }),
|
|
60
|
+
* l.object({ type: l.literal('delete'), id: l.string() }),
|
|
61
|
+
* ])
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
20
64
|
/*@__NO_SIDE_EFFECTS__*/
|
|
21
65
|
function literal(value) {
|
|
22
66
|
return new LiteralSchema(value);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"literal.js","sourceRoot":"","sources":["../../src/schema/literal.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"literal.js","sourceRoot":"","sources":["../../src/schema/literal.ts"],"names":[],"mappings":";;;AA+DA,0BAIC;AAnED,wCAAsD;AAEtD;;;;;;;;;;;;;;GAcG;AACH,MAAa,aAEX,SAAQ,gBAAc;IACD;IAArB,YAAqB,KAAa;QAChC,KAAK,EAAE,CAAA;QADY,UAAK,GAAL,KAAK,CAAQ;IAElC,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,KAAK,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YACzB,OAAO,GAAG,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;QACnD,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAChC,CAAC;CACF;AAdD,sCAcC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAwB;AACxB,SAAgB,OAAO,CACrB,KAAQ;IAER,OAAO,IAAI,aAAa,CAAI,KAAK,CAAC,CAAA;AACpC,CAAC","sourcesContent":["import { Schema, ValidationContext } from '../core.js'\n\n/**\n * Schema that only accepts a specific literal value.\n *\n * Validates that the input is exactly equal to the specified value using\n * strict equality (===).\n *\n * @template TValue - The literal type (null, string, number, or boolean)\n *\n * @example\n * ```ts\n * const schema = new LiteralSchema('admin')\n * schema.validate('admin') // success\n * schema.validate('user') // fails\n * ```\n */\nexport class LiteralSchema<\n const TValue extends null | string | number | boolean,\n> extends Schema<TValue> {\n constructor(readonly value: TValue) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (input !== this.value) {\n return ctx.issueInvalidValue(input, [this.value])\n }\n\n return ctx.success(this.value)\n }\n}\n\n/**\n * Creates a literal schema that only accepts the exact specified value.\n *\n * Useful for discriminator fields in unions, constant values, or type narrowing.\n *\n * @param value - The exact value that must be matched\n * @returns A new {@link LiteralSchema} instance\n *\n * @example\n * ```ts\n * // String literal\n * const roleSchema = l.literal('admin')\n *\n * // Number literal\n * const versionSchema = l.literal(1)\n *\n * // Boolean literal\n * const enabledSchema = l.literal(true)\n *\n * // Null literal\n * const nullSchema = l.literal(null)\n *\n * // In discriminated unions\n * const actionSchema = l.discriminatedUnion('type', [\n * l.object({ type: l.literal('create'), data: l.unknown() }),\n * l.object({ type: l.literal('delete'), id: l.string() }),\n * ])\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function literal<const V extends null | string | number | boolean>(\n value: V,\n) {\n return new LiteralSchema<V>(value)\n}\n"]}
|
package/dist/schema/never.d.ts
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
import { Schema, ValidationContext } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Schema that always fails validation.
|
|
4
|
+
*
|
|
5
|
+
* Represents an impossible type - no value can satisfy this schema.
|
|
6
|
+
* Useful for exhaustiveness checking or marking impossible branches.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const schema = new NeverSchema()
|
|
11
|
+
* schema.validate(anything) // always fails
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
2
14
|
export declare class NeverSchema extends Schema<never> {
|
|
3
15
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationFailure;
|
|
4
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates a never schema that always fails validation.
|
|
19
|
+
*
|
|
20
|
+
* Useful for exhaustiveness checking in TypeScript or marking impossible
|
|
21
|
+
* code paths.
|
|
22
|
+
*
|
|
23
|
+
* @returns A new {@link NeverSchema} instance
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* // Exhaustiveness checking
|
|
28
|
+
* type Status = 'active' | 'inactive'
|
|
29
|
+
*
|
|
30
|
+
* function handleStatus(status: Status) {
|
|
31
|
+
* switch (status) {
|
|
32
|
+
* case 'active': return 'Active'
|
|
33
|
+
* case 'inactive': return 'Inactive'
|
|
34
|
+
* default:
|
|
35
|
+
* // TypeScript will error if we miss a case
|
|
36
|
+
* l.never().parse(status)
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* // In impossible union branches
|
|
41
|
+
* const schema = l.object({
|
|
42
|
+
* type: l.literal('fixed'),
|
|
43
|
+
* dynamic: l.never(), // This property can never exist
|
|
44
|
+
* })
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
5
47
|
export declare const never: () => NeverSchema;
|
|
6
48
|
//# sourceMappingURL=never.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"never.d.ts","sourceRoot":"","sources":["../../src/schema/never.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD,qBAAa,WAAY,SAAQ,MAAM,CAAC,KAAK,CAAC;IAC5C,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAGzD;AAED,eAAO,MAAM,KAAK,mBAEhB,CAAA"}
|
|
1
|
+
{"version":3,"file":"never.d.ts","sourceRoot":"","sources":["../../src/schema/never.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD;;;;;;;;;;;GAWG;AACH,qBAAa,WAAY,SAAQ,MAAM,CAAC,KAAK,CAAC;IAC5C,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAGzD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,eAAO,MAAM,KAAK,mBAEhB,CAAA"}
|
package/dist/schema/never.js
CHANGED
|
@@ -3,12 +3,54 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.never = exports.NeverSchema = void 0;
|
|
4
4
|
const core_js_1 = require("../core.js");
|
|
5
5
|
const memoize_js_1 = require("../util/memoize.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema that always fails validation.
|
|
8
|
+
*
|
|
9
|
+
* Represents an impossible type - no value can satisfy this schema.
|
|
10
|
+
* Useful for exhaustiveness checking or marking impossible branches.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const schema = new NeverSchema()
|
|
15
|
+
* schema.validate(anything) // always fails
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
6
18
|
class NeverSchema extends core_js_1.Schema {
|
|
7
19
|
validateInContext(input, ctx) {
|
|
8
20
|
return ctx.issueInvalidType(input, 'never');
|
|
9
21
|
}
|
|
10
22
|
}
|
|
11
23
|
exports.NeverSchema = NeverSchema;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a never schema that always fails validation.
|
|
26
|
+
*
|
|
27
|
+
* Useful for exhaustiveness checking in TypeScript or marking impossible
|
|
28
|
+
* code paths.
|
|
29
|
+
*
|
|
30
|
+
* @returns A new {@link NeverSchema} instance
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* // Exhaustiveness checking
|
|
35
|
+
* type Status = 'active' | 'inactive'
|
|
36
|
+
*
|
|
37
|
+
* function handleStatus(status: Status) {
|
|
38
|
+
* switch (status) {
|
|
39
|
+
* case 'active': return 'Active'
|
|
40
|
+
* case 'inactive': return 'Inactive'
|
|
41
|
+
* default:
|
|
42
|
+
* // TypeScript will error if we miss a case
|
|
43
|
+
* l.never().parse(status)
|
|
44
|
+
* }
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* // In impossible union branches
|
|
48
|
+
* const schema = l.object({
|
|
49
|
+
* type: l.literal('fixed'),
|
|
50
|
+
* dynamic: l.never(), // This property can never exist
|
|
51
|
+
* })
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
12
54
|
exports.never = (0, memoize_js_1.memoizedOptions)(function () {
|
|
13
55
|
return new NeverSchema();
|
|
14
56
|
});
|
package/dist/schema/never.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"never.js","sourceRoot":"","sources":["../../src/schema/never.ts"],"names":[],"mappings":";;;AAAA,wCAAsD;AACtD,mDAAoD;AAEpD,MAAa,WAAY,SAAQ,gBAAa;IAC5C,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAC7C,CAAC;CACF;AAJD,kCAIC;
|
|
1
|
+
{"version":3,"file":"never.js","sourceRoot":"","sources":["../../src/schema/never.ts"],"names":[],"mappings":";;;AAAA,wCAAsD;AACtD,mDAAoD;AAEpD;;;;;;;;;;;GAWG;AACH,MAAa,WAAY,SAAQ,gBAAa;IAC5C,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAC7C,CAAC;CACF;AAJD,kCAIC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACU,QAAA,KAAK,GAAiB,IAAA,4BAAe,EAAC;IACjD,OAAO,IAAI,WAAW,EAAE,CAAA;AAC1B,CAAC,CAAC,CAAA","sourcesContent":["import { Schema, ValidationContext } from '../core.js'\nimport { memoizedOptions } from '../util/memoize.js'\n\n/**\n * Schema that always fails validation.\n *\n * Represents an impossible type - no value can satisfy this schema.\n * Useful for exhaustiveness checking or marking impossible branches.\n *\n * @example\n * ```ts\n * const schema = new NeverSchema()\n * schema.validate(anything) // always fails\n * ```\n */\nexport class NeverSchema extends Schema<never> {\n validateInContext(input: unknown, ctx: ValidationContext) {\n return ctx.issueInvalidType(input, 'never')\n }\n}\n\n/**\n * Creates a never schema that always fails validation.\n *\n * Useful for exhaustiveness checking in TypeScript or marking impossible\n * code paths.\n *\n * @returns A new {@link NeverSchema} instance\n *\n * @example\n * ```ts\n * // Exhaustiveness checking\n * type Status = 'active' | 'inactive'\n *\n * function handleStatus(status: Status) {\n * switch (status) {\n * case 'active': return 'Active'\n * case 'inactive': return 'Inactive'\n * default:\n * // TypeScript will error if we miss a case\n * l.never().parse(status)\n * }\n * }\n *\n * // In impossible union branches\n * const schema = l.object({\n * type: l.literal('fixed'),\n * dynamic: l.never(), // This property can never exist\n * })\n * ```\n */\nexport const never = /*#__PURE__*/ memoizedOptions(function () {\n return new NeverSchema()\n})\n"]}
|
package/dist/schema/null.d.ts
CHANGED
|
@@ -1,7 +1,36 @@
|
|
|
1
1
|
import { Schema, ValidationContext } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Schema for validating null values.
|
|
4
|
+
*
|
|
5
|
+
* Only accepts the JavaScript `null` value. Rejects `undefined` and all
|
|
6
|
+
* other values.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const schema = new NullSchema()
|
|
11
|
+
* schema.validate(null) // success
|
|
12
|
+
* schema.validate(undefined) // fails
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
2
15
|
export declare class NullSchema extends Schema<null> {
|
|
3
16
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<null>;
|
|
4
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a null schema that only accepts the null value.
|
|
20
|
+
*
|
|
21
|
+
* Useful for explicitly representing null in union types or optional fields.
|
|
22
|
+
*
|
|
23
|
+
* @returns A new {@link NullSchema} instance
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* // Explicit null
|
|
28
|
+
* const nullOnlySchema = l.null()
|
|
29
|
+
*
|
|
30
|
+
* // Nullable string (string or null)
|
|
31
|
+
* const nullableStringSchema = l.union([l.string(), l.null()])
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
5
34
|
export declare const nullSchema: () => NullSchema;
|
|
6
35
|
export { nullSchema as null };
|
|
7
36
|
//# sourceMappingURL=null.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"null.d.ts","sourceRoot":"","sources":["../../src/schema/null.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD,qBAAa,UAAW,SAAQ,MAAM,CAAC,IAAI,CAAC;IAC1C,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED,eAAO,MAAM,UAAU,kBAErB,CAAA;AAEF,OAAO,EAAE,UAAU,IAAI,IAAI,EAAE,CAAA"}
|
|
1
|
+
{"version":3,"file":"null.d.ts","sourceRoot":"","sources":["../../src/schema/null.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD;;;;;;;;;;;;GAYG;AACH,qBAAa,UAAW,SAAQ,MAAM,CAAC,IAAI,CAAC;IAC1C,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,UAAU,kBAErB,CAAA;AAEF,OAAO,EAAE,UAAU,IAAI,IAAI,EAAE,CAAA"}
|
package/dist/schema/null.js
CHANGED
|
@@ -3,6 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.null = exports.nullSchema = exports.NullSchema = void 0;
|
|
4
4
|
const core_js_1 = require("../core.js");
|
|
5
5
|
const memoize_js_1 = require("../util/memoize.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema for validating null values.
|
|
8
|
+
*
|
|
9
|
+
* Only accepts the JavaScript `null` value. Rejects `undefined` and all
|
|
10
|
+
* other values.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const schema = new NullSchema()
|
|
15
|
+
* schema.validate(null) // success
|
|
16
|
+
* schema.validate(undefined) // fails
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
6
19
|
class NullSchema extends core_js_1.Schema {
|
|
7
20
|
validateInContext(input, ctx) {
|
|
8
21
|
if (input !== null) {
|
|
@@ -12,6 +25,22 @@ class NullSchema extends core_js_1.Schema {
|
|
|
12
25
|
}
|
|
13
26
|
}
|
|
14
27
|
exports.NullSchema = NullSchema;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a null schema that only accepts the null value.
|
|
30
|
+
*
|
|
31
|
+
* Useful for explicitly representing null in union types or optional fields.
|
|
32
|
+
*
|
|
33
|
+
* @returns A new {@link NullSchema} instance
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* // Explicit null
|
|
38
|
+
* const nullOnlySchema = l.null()
|
|
39
|
+
*
|
|
40
|
+
* // Nullable string (string or null)
|
|
41
|
+
* const nullableStringSchema = l.union([l.string(), l.null()])
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
15
44
|
exports.nullSchema = (0, memoize_js_1.memoizedOptions)(function () {
|
|
16
45
|
return new NullSchema();
|
|
17
46
|
});
|
package/dist/schema/null.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"null.js","sourceRoot":"","sources":["../../src/schema/null.ts"],"names":[],"mappings":";;;AAAA,wCAAsD;AACtD,mDAAoD;AAEpD,MAAa,UAAW,SAAQ,gBAAY;IAC1C,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QAC5C,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;CACF;AARD,gCAQC;
|
|
1
|
+
{"version":3,"file":"null.js","sourceRoot":"","sources":["../../src/schema/null.ts"],"names":[],"mappings":";;;AAAA,wCAAsD;AACtD,mDAAoD;AAEpD;;;;;;;;;;;;GAYG;AACH,MAAa,UAAW,SAAQ,gBAAY;IAC1C,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QAC5C,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;CACF;AARD,gCAQC;AAED;;;;;;;;;;;;;;;GAeG;AACU,QAAA,UAAU,GAAiB,IAAA,4BAAe,EAAC;IACtD,OAAO,IAAI,UAAU,EAAE,CAAA;AACzB,CAAC,CAAC,CAAA;AAEqB,eAJV,kBAAU,CAII","sourcesContent":["import { Schema, ValidationContext } from '../core.js'\nimport { memoizedOptions } from '../util/memoize.js'\n\n/**\n * Schema for validating null values.\n *\n * Only accepts the JavaScript `null` value. Rejects `undefined` and all\n * other values.\n *\n * @example\n * ```ts\n * const schema = new NullSchema()\n * schema.validate(null) // success\n * schema.validate(undefined) // fails\n * ```\n */\nexport class NullSchema extends Schema<null> {\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (input !== null) {\n return ctx.issueInvalidType(input, 'null')\n }\n\n return ctx.success(null)\n }\n}\n\n/**\n * Creates a null schema that only accepts the null value.\n *\n * Useful for explicitly representing null in union types or optional fields.\n *\n * @returns A new {@link NullSchema} instance\n *\n * @example\n * ```ts\n * // Explicit null\n * const nullOnlySchema = l.null()\n *\n * // Nullable string (string or null)\n * const nullableStringSchema = l.union([l.string(), l.null()])\n * ```\n */\nexport const nullSchema = /*#__PURE__*/ memoizedOptions(function () {\n return new NullSchema()\n})\n\nexport { nullSchema as null }\n"]}
|
|
@@ -1,8 +1,49 @@
|
|
|
1
1
|
import { InferInput, InferOutput, Schema, ValidationContext, Validator } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Schema wrapper that allows null values in addition to the wrapped schema.
|
|
4
|
+
*
|
|
5
|
+
* When the input is `null`, validation succeeds immediately. Otherwise,
|
|
6
|
+
* the input is validated against the wrapped schema.
|
|
7
|
+
*
|
|
8
|
+
* @template TValidator - The wrapped validator type
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const schema = new NullableSchema(l.string())
|
|
13
|
+
* schema.validate(null) // success
|
|
14
|
+
* schema.validate('hello') // success
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
2
17
|
export declare class NullableSchema<const TValidator extends Validator> extends Schema<InferInput<TValidator> | null, InferOutput<TValidator> | null> {
|
|
3
18
|
readonly validator: TValidator;
|
|
4
19
|
constructor(validator: TValidator);
|
|
5
20
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationFailure | import("../core.js").ValidationSuccess<null> | import("../core.js").ValidationSuccess<InferInput<TValidator>>;
|
|
6
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a nullable schema that accepts null in addition to the wrapped type.
|
|
24
|
+
*
|
|
25
|
+
* Wraps another schema to allow null values. Different from `optional()` which
|
|
26
|
+
* allows undefined.
|
|
27
|
+
*
|
|
28
|
+
* @param validator - The validator to make nullable
|
|
29
|
+
* @returns A new {@link NullableSchema} instance
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* // Nullable string
|
|
34
|
+
* const nullableString = l.nullable(l.string())
|
|
35
|
+
* nullableString.parse(null) // null
|
|
36
|
+
* nullableString.parse('hello') // 'hello'
|
|
37
|
+
*
|
|
38
|
+
* // In an object
|
|
39
|
+
* const userSchema = l.object({
|
|
40
|
+
* name: l.string(),
|
|
41
|
+
* deletedAt: l.nullable(l.string({ format: 'datetime' })),
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
44
|
+
* // Combine with optional for null or undefined
|
|
45
|
+
* const maybeString = l.optional(l.nullable(l.string()))
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
7
48
|
export declare const nullable: <const TValidator extends Validator>(validator: TValidator) => NullableSchema<TValidator>;
|
|
8
49
|
//# sourceMappingURL=nullable.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nullable.d.ts","sourceRoot":"","sources":["../../src/schema/nullable.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,iBAAiB,EACjB,SAAS,EACV,MAAM,YAAY,CAAA;AAGnB,qBAAa,cAAc,CAAC,KAAK,CAAC,UAAU,SAAS,SAAS,CAAE,SAAQ,MAAM,CAC5E,UAAU,CAAC,UAAU,CAAC,GAAG,IAAI,EAC7B,WAAW,CAAC,UAAU,CAAC,GAAG,IAAI,CAC/B;IACa,QAAQ,CAAC,SAAS,EAAE,UAAU;gBAArB,SAAS,EAAE,UAAU;IAI1C,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED,eAAO,MAAM,QAAQ,SACb,UAAU,SAAS,SAAS,aACvB,UAAU,+BAErB,CAAA"}
|
|
1
|
+
{"version":3,"file":"nullable.d.ts","sourceRoot":"","sources":["../../src/schema/nullable.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,iBAAiB,EACjB,SAAS,EACV,MAAM,YAAY,CAAA;AAGnB;;;;;;;;;;;;;;GAcG;AACH,qBAAa,cAAc,CAAC,KAAK,CAAC,UAAU,SAAS,SAAS,CAAE,SAAQ,MAAM,CAC5E,UAAU,CAAC,UAAU,CAAC,GAAG,IAAI,EAC7B,WAAW,CAAC,UAAU,CAAC,GAAG,IAAI,CAC/B;IACa,QAAQ,CAAC,SAAS,EAAE,UAAU;gBAArB,SAAS,EAAE,UAAU;IAI1C,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,QAAQ,SACb,UAAU,SAAS,SAAS,aACvB,UAAU,+BAErB,CAAA"}
|
package/dist/schema/nullable.js
CHANGED
|
@@ -3,6 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.nullable = exports.NullableSchema = void 0;
|
|
4
4
|
const core_js_1 = require("../core.js");
|
|
5
5
|
const memoize_js_1 = require("../util/memoize.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema wrapper that allows null values in addition to the wrapped schema.
|
|
8
|
+
*
|
|
9
|
+
* When the input is `null`, validation succeeds immediately. Otherwise,
|
|
10
|
+
* the input is validated against the wrapped schema.
|
|
11
|
+
*
|
|
12
|
+
* @template TValidator - The wrapped validator type
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const schema = new NullableSchema(l.string())
|
|
17
|
+
* schema.validate(null) // success
|
|
18
|
+
* schema.validate('hello') // success
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
6
21
|
class NullableSchema extends core_js_1.Schema {
|
|
7
22
|
validator;
|
|
8
23
|
constructor(validator) {
|
|
@@ -17,6 +32,32 @@ class NullableSchema extends core_js_1.Schema {
|
|
|
17
32
|
}
|
|
18
33
|
}
|
|
19
34
|
exports.NullableSchema = NullableSchema;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a nullable schema that accepts null in addition to the wrapped type.
|
|
37
|
+
*
|
|
38
|
+
* Wraps another schema to allow null values. Different from `optional()` which
|
|
39
|
+
* allows undefined.
|
|
40
|
+
*
|
|
41
|
+
* @param validator - The validator to make nullable
|
|
42
|
+
* @returns A new {@link NullableSchema} instance
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* // Nullable string
|
|
47
|
+
* const nullableString = l.nullable(l.string())
|
|
48
|
+
* nullableString.parse(null) // null
|
|
49
|
+
* nullableString.parse('hello') // 'hello'
|
|
50
|
+
*
|
|
51
|
+
* // In an object
|
|
52
|
+
* const userSchema = l.object({
|
|
53
|
+
* name: l.string(),
|
|
54
|
+
* deletedAt: l.nullable(l.string({ format: 'datetime' })),
|
|
55
|
+
* })
|
|
56
|
+
*
|
|
57
|
+
* // Combine with optional for null or undefined
|
|
58
|
+
* const maybeString = l.optional(l.nullable(l.string()))
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
20
61
|
exports.nullable = (0, memoize_js_1.memoizedTransformer)(function (validator) {
|
|
21
62
|
return new NullableSchema(validator);
|
|
22
63
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nullable.js","sourceRoot":"","sources":["../../src/schema/nullable.ts"],"names":[],"mappings":";;;AAAA,wCAMmB;AACnB,mDAAwD;AAExD,MAAa,cAAmD,SAAQ,gBAGvE;IACsB;IAArB,YAAqB,SAAqB;QACxC,KAAK,EAAE,CAAA;QADY,cAAS,GAAT,SAAS,CAAY;IAE1C,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAC1B,CAAC;QAED,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;IAC5C,CAAC;CACF;AAfD,wCAeC;
|
|
1
|
+
{"version":3,"file":"nullable.js","sourceRoot":"","sources":["../../src/schema/nullable.ts"],"names":[],"mappings":";;;AAAA,wCAMmB;AACnB,mDAAwD;AAExD;;;;;;;;;;;;;;GAcG;AACH,MAAa,cAAmD,SAAQ,gBAGvE;IACsB;IAArB,YAAqB,SAAqB;QACxC,KAAK,EAAE,CAAA;QADY,cAAS,GAAT,SAAS,CAAY;IAE1C,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAC1B,CAAC;QAED,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;IAC5C,CAAC;CACF;AAfD,wCAeC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACU,QAAA,QAAQ,GAAiB,IAAA,gCAAmB,EAAC,UAExD,SAAqB;IACrB,OAAO,IAAI,cAAc,CAAa,SAAS,CAAC,CAAA;AAClD,CAAC,CAAC,CAAA","sourcesContent":["import {\n InferInput,\n InferOutput,\n Schema,\n ValidationContext,\n Validator,\n} from '../core.js'\nimport { memoizedTransformer } from '../util/memoize.js'\n\n/**\n * Schema wrapper that allows null values in addition to the wrapped schema.\n *\n * When the input is `null`, validation succeeds immediately. Otherwise,\n * the input is validated against the wrapped schema.\n *\n * @template TValidator - The wrapped validator type\n *\n * @example\n * ```ts\n * const schema = new NullableSchema(l.string())\n * schema.validate(null) // success\n * schema.validate('hello') // success\n * ```\n */\nexport class NullableSchema<const TValidator extends Validator> extends Schema<\n InferInput<TValidator> | null,\n InferOutput<TValidator> | null\n> {\n constructor(readonly validator: TValidator) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (input === null) {\n return ctx.success(null)\n }\n\n return ctx.validate(input, this.validator)\n }\n}\n\n/**\n * Creates a nullable schema that accepts null in addition to the wrapped type.\n *\n * Wraps another schema to allow null values. Different from `optional()` which\n * allows undefined.\n *\n * @param validator - The validator to make nullable\n * @returns A new {@link NullableSchema} instance\n *\n * @example\n * ```ts\n * // Nullable string\n * const nullableString = l.nullable(l.string())\n * nullableString.parse(null) // null\n * nullableString.parse('hello') // 'hello'\n *\n * // In an object\n * const userSchema = l.object({\n * name: l.string(),\n * deletedAt: l.nullable(l.string({ format: 'datetime' })),\n * })\n *\n * // Combine with optional for null or undefined\n * const maybeString = l.optional(l.nullable(l.string()))\n * ```\n */\nexport const nullable = /*#__PURE__*/ memoizedTransformer(function <\n const TValidator extends Validator,\n>(validator: TValidator) {\n return new NullableSchema<TValidator>(validator)\n})\n"]}
|
package/dist/schema/object.d.ts
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import { InferInput, InferOutput, Schema, ValidationContext, Validator, WithOptionalProperties } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Type representing the shape of an object schema.
|
|
4
|
+
*
|
|
5
|
+
* Maps property names to their corresponding validators.
|
|
6
|
+
*/
|
|
2
7
|
export type ObjectSchemaShape = Record<string, Validator>;
|
|
8
|
+
/**
|
|
9
|
+
* Schema for validating objects with a defined shape.
|
|
10
|
+
*
|
|
11
|
+
* Each property in the shape is validated against its corresponding schema.
|
|
12
|
+
* Properties wrapped in `optional()` are not required.
|
|
13
|
+
*
|
|
14
|
+
* @template TShape - The object shape type mapping property names to validators
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const schema = new ObjectSchema({
|
|
19
|
+
* name: l.string(),
|
|
20
|
+
* age: l.optional(l.integer()),
|
|
21
|
+
* })
|
|
22
|
+
* const result = schema.validate({ name: 'Alice' })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
3
25
|
export declare class ObjectSchema<const TShape extends ObjectSchemaShape = any> extends Schema<WithOptionalProperties<{
|
|
4
26
|
[K in keyof TShape]: InferInput<TShape[K]>;
|
|
5
27
|
}>, WithOptionalProperties<{
|
|
@@ -10,5 +32,39 @@ export declare class ObjectSchema<const TShape extends ObjectSchemaShape = any>
|
|
|
10
32
|
get validatorsMap(): Map<string, Validator>;
|
|
11
33
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<Record<string, unknown>>;
|
|
12
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates an object schema with the specified property validators.
|
|
37
|
+
*
|
|
38
|
+
* Validates that the input is a plain object and each property matches
|
|
39
|
+
* its corresponding schema. Properties wrapped in `optional()` are not required.
|
|
40
|
+
*
|
|
41
|
+
* @param properties - Object mapping property names to their validators
|
|
42
|
+
* @returns A new {@link ObjectSchema} instance
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* // Basic object
|
|
47
|
+
* const userSchema = l.object({
|
|
48
|
+
* name: l.string(),
|
|
49
|
+
* email: l.string({ format: 'uri' }),
|
|
50
|
+
* })
|
|
51
|
+
*
|
|
52
|
+
* // With optional properties
|
|
53
|
+
* const profileSchema = l.object({
|
|
54
|
+
* displayName: l.string(),
|
|
55
|
+
* bio: l.optional(l.string({ maxLength: 256 })),
|
|
56
|
+
* avatar: l.optional(l.blob({ accept: ['image/*'] })),
|
|
57
|
+
* })
|
|
58
|
+
*
|
|
59
|
+
* // Nested objects
|
|
60
|
+
* const postSchema = l.object({
|
|
61
|
+
* text: l.string(),
|
|
62
|
+
* author: l.object({
|
|
63
|
+
* did: l.string({ format: 'did' }),
|
|
64
|
+
* handle: l.string({ format: 'handle' }),
|
|
65
|
+
* }),
|
|
66
|
+
* })
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
13
69
|
export declare function object<const TShape extends ObjectSchemaShape>(properties: TShape): ObjectSchema<TShape>;
|
|
14
70
|
//# sourceMappingURL=object.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/schema/object.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,iBAAiB,EACjB,SAAS,EACT,sBAAsB,EACvB,MAAM,YAAY,CAAA;AAGnB,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;AAEzD,qBAAa,YAAY,CACvB,KAAK,CAAC,MAAM,SAAS,iBAAiB,GAAG,GAAG,CAC5C,SAAQ,MAAM,CACd,sBAAsB,CAAC;KACpB,CAAC,IAAI,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;CAC3C,CAAC,EACF,sBAAsB,CAAC;KACpB,CAAC,IAAI,MAAM,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;CAC5C,CAAC,CACH;IACa,QAAQ,CAAC,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;IAIlC,IAAI,aAAa,IAAI,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAI1C;IAED,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAqCzD;
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/schema/object.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,iBAAiB,EACjB,SAAS,EACT,sBAAsB,EACvB,MAAM,YAAY,CAAA;AAGnB;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;AAEzD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,YAAY,CACvB,KAAK,CAAC,MAAM,SAAS,iBAAiB,GAAG,GAAG,CAC5C,SAAQ,MAAM,CACd,sBAAsB,CAAC;KACpB,CAAC,IAAI,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;CAC3C,CAAC,EACF,sBAAsB,CAAC;KACpB,CAAC,IAAI,MAAM,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;CAC5C,CAAC,CACH;IACa,QAAQ,CAAC,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;IAIlC,IAAI,aAAa,IAAI,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAI1C;IAED,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAqCzD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,wBAAgB,MAAM,CAAC,KAAK,CAAC,MAAM,SAAS,iBAAiB,EAC3D,UAAU,EAAE,MAAM,wBAGnB"}
|