@atproto/lex-schema 0.0.11 → 0.0.13
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 +54 -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 +232 -5
- 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 +60 -0
- 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 +54 -1
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +356 -11
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +203 -4
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +12 -28
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +46 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +16 -1
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +50 -2
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +44 -2
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +29 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +30 -1
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +39 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +34 -1
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +39 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +35 -1
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +67 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +55 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +45 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +46 -1
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +59 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +47 -1
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +49 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +49 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +43 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +38 -1
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +55 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +50 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/lex-map.d.ts +37 -0
- package/dist/schema/lex-map.d.ts.map +1 -0
- package/dist/schema/lex-map.js +60 -0
- package/dist/schema/lex-map.js.map +1 -0
- package/dist/schema/lex-value.d.ts +35 -0
- package/dist/schema/lex-value.d.ts.map +1 -0
- package/dist/schema/lex-value.js +87 -0
- package/dist/schema/lex-value.js.map +1 -0
- package/dist/schema/literal.d.ts +45 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +45 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +43 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +44 -1
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +30 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +31 -1
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +42 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +42 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +57 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +53 -1
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +43 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +43 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +96 -12
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +155 -21
- 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 +73 -3
- 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 +76 -25
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +21 -0
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +51 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +18 -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 +45 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +46 -1
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +72 -6
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +56 -8
- 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 +48 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +49 -1
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +73 -23
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +20 -1
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +54 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +16 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +51 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +52 -2
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +46 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +41 -0
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown.d.ts +34 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +34 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +45 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +45 -0
- package/dist/schema/with-default.js.map +1 -1
- package/dist/schema.d.ts +2 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +2 -1
- package/dist/schema.js.map +1 -1
- package/dist/util/if-any.d.ts +2 -0
- package/dist/util/if-any.d.ts.map +1 -0
- package/dist/util/if-any.js +3 -0
- package/dist/util/if-any.js.map +1 -0
- package/package.json +3 -3
- 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 +244 -9
- 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 +68 -2
- package/src/core/validator.ts +373 -12
- package/src/helpers.test.ts +110 -29
- package/src/helpers.ts +54 -25
- package/src/schema/array.test.ts +94 -79
- package/src/schema/array.ts +48 -1
- package/src/schema/blob.ts +50 -1
- package/src/schema/boolean.ts +31 -1
- package/src/schema/bytes.ts +41 -1
- package/src/schema/cid.ts +41 -1
- package/src/schema/custom.ts +68 -1
- package/src/schema/dict.ts +47 -1
- package/src/schema/discriminated-union.ts +61 -1
- package/src/schema/enum.ts +50 -0
- package/src/schema/integer.ts +45 -1
- package/src/schema/intersection.ts +56 -0
- package/src/schema/{unknown-object.test.ts → lex-map.test.ts} +9 -9
- package/src/schema/lex-map.ts +63 -0
- package/src/schema/lex-value.test.ts +81 -0
- package/src/schema/lex-value.ts +86 -0
- package/src/schema/literal.ts +46 -0
- package/src/schema/never.ts +45 -1
- package/src/schema/null.ts +32 -1
- package/src/schema/nullable.ts +43 -0
- package/src/schema/object.ts +59 -1
- package/src/schema/optional.ts +44 -0
- package/src/schema/params.test.ts +133 -38
- package/src/schema/params.ts +237 -37
- package/src/schema/payload.test.ts +3 -3
- package/src/schema/payload.ts +145 -42
- 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 +82 -16
- package/src/schema/ref.ts +52 -0
- package/src/schema/refine.ts +58 -9
- package/src/schema/regexp.ts +47 -1
- package/src/schema/string.test.ts +99 -2
- package/src/schema/string.ts +108 -15
- package/src/schema/subscription.ts +72 -2
- package/src/schema/token.ts +50 -1
- package/src/schema/typed-object.ts +81 -16
- package/src/schema/typed-ref.ts +55 -0
- package/src/schema/typed-union.ts +58 -3
- package/src/schema/union.ts +47 -0
- package/src/schema/unknown.ts +35 -0
- package/src/schema/with-default.ts +46 -0
- package/src/schema.ts +2 -1
- package/src/util/if-any.ts +3 -0
- package/dist/schema/unknown-object.d.ts +0 -8
- package/dist/schema/unknown-object.d.ts.map +0 -1
- package/dist/schema/unknown-object.js +0 -19
- package/dist/schema/unknown-object.js.map +0 -1
- package/src/schema/unknown-object.ts +0 -19
package/dist/schema/literal.js
CHANGED
|
@@ -3,8 +3,24 @@ 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;
|
|
23
|
+
type = 'literal';
|
|
8
24
|
constructor(value) {
|
|
9
25
|
super();
|
|
10
26
|
this.value = value;
|
|
@@ -17,6 +33,35 @@ class LiteralSchema extends core_js_1.Schema {
|
|
|
17
33
|
}
|
|
18
34
|
}
|
|
19
35
|
exports.LiteralSchema = LiteralSchema;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a literal schema that only accepts the exact specified value.
|
|
38
|
+
*
|
|
39
|
+
* Useful for discriminator fields in unions, constant values, or type narrowing.
|
|
40
|
+
*
|
|
41
|
+
* @param value - The exact value that must be matched
|
|
42
|
+
* @returns A new {@link LiteralSchema} instance
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* // String literal
|
|
47
|
+
* const roleSchema = l.literal('admin')
|
|
48
|
+
*
|
|
49
|
+
* // Number literal
|
|
50
|
+
* const versionSchema = l.literal(1)
|
|
51
|
+
*
|
|
52
|
+
* // Boolean literal
|
|
53
|
+
* const enabledSchema = l.literal(true)
|
|
54
|
+
*
|
|
55
|
+
* // Null literal
|
|
56
|
+
* const nullSchema = l.literal(null)
|
|
57
|
+
*
|
|
58
|
+
* // In discriminated unions
|
|
59
|
+
* const actionSchema = l.discriminatedUnion('type', [
|
|
60
|
+
* l.object({ type: l.literal('create'), data: l.unknown() }),
|
|
61
|
+
* l.object({ type: l.literal('delete'), id: l.string() }),
|
|
62
|
+
* ])
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
20
65
|
/*@__NO_SIDE_EFFECTS__*/
|
|
21
66
|
function literal(value) {
|
|
22
67
|
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":";;;AAiEA,0BAIC;AArED,wCAAsD;AAEtD;;;;;;;;;;;;;;GAcG;AACH,MAAa,aAEX,SAAQ,gBAAc;IAGD;IAFZ,IAAI,GAAG,SAAkB,CAAA;IAElC,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;AAhBD,sCAgBC;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 readonly type = 'literal' as const\n\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,49 @@
|
|
|
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> {
|
|
15
|
+
readonly type: "never";
|
|
3
16
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationFailure;
|
|
4
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a never schema that always fails validation.
|
|
20
|
+
*
|
|
21
|
+
* Useful for exhaustiveness checking in TypeScript or marking impossible
|
|
22
|
+
* code paths.
|
|
23
|
+
*
|
|
24
|
+
* @returns A new {@link NeverSchema} instance
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* // Exhaustiveness checking
|
|
29
|
+
* type Status = 'active' | 'inactive'
|
|
30
|
+
*
|
|
31
|
+
* function handleStatus(status: Status) {
|
|
32
|
+
* switch (status) {
|
|
33
|
+
* case 'active': return 'Active'
|
|
34
|
+
* case 'inactive': return 'Inactive'
|
|
35
|
+
* default:
|
|
36
|
+
* // TypeScript will error if we miss a case
|
|
37
|
+
* l.never().parse(status)
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* // In impossible union branches
|
|
42
|
+
* const schema = l.object({
|
|
43
|
+
* type: l.literal('fixed'),
|
|
44
|
+
* dynamic: l.never(), // This property can never exist
|
|
45
|
+
* })
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
5
48
|
export declare const never: () => NeverSchema;
|
|
6
49
|
//# 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,QAAQ,CAAC,IAAI,EAAG,OAAO,CAAS;IAEhC,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,55 @@ 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 {
|
|
19
|
+
type = 'never';
|
|
7
20
|
validateInContext(input, ctx) {
|
|
8
|
-
return ctx.
|
|
21
|
+
return ctx.issueUnexpectedType(input, 'never');
|
|
9
22
|
}
|
|
10
23
|
}
|
|
11
24
|
exports.NeverSchema = NeverSchema;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a never schema that always fails validation.
|
|
27
|
+
*
|
|
28
|
+
* Useful for exhaustiveness checking in TypeScript or marking impossible
|
|
29
|
+
* code paths.
|
|
30
|
+
*
|
|
31
|
+
* @returns A new {@link NeverSchema} instance
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* // Exhaustiveness checking
|
|
36
|
+
* type Status = 'active' | 'inactive'
|
|
37
|
+
*
|
|
38
|
+
* function handleStatus(status: Status) {
|
|
39
|
+
* switch (status) {
|
|
40
|
+
* case 'active': return 'Active'
|
|
41
|
+
* case 'inactive': return 'Inactive'
|
|
42
|
+
* default:
|
|
43
|
+
* // TypeScript will error if we miss a case
|
|
44
|
+
* l.never().parse(status)
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* // In impossible union branches
|
|
49
|
+
* const schema = l.object({
|
|
50
|
+
* type: l.literal('fixed'),
|
|
51
|
+
* dynamic: l.never(), // This property can never exist
|
|
52
|
+
* })
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
12
55
|
exports.never = (0, memoize_js_1.memoizedOptions)(function () {
|
|
13
56
|
return new NeverSchema();
|
|
14
57
|
});
|
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;
|
|
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;IACnC,IAAI,GAAG,OAAgB,CAAA;IAEhC,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,OAAO,GAAG,CAAC,mBAAmB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAChD,CAAC;CACF;AAND,kCAMC;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 readonly type = 'never' as const\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n return ctx.issueUnexpectedType(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,37 @@
|
|
|
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> {
|
|
16
|
+
readonly type: "null";
|
|
3
17
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<null>;
|
|
4
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a null schema that only accepts the null value.
|
|
21
|
+
*
|
|
22
|
+
* Useful for explicitly representing null in union types or optional fields.
|
|
23
|
+
*
|
|
24
|
+
* @returns A new {@link NullSchema} instance
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* // Explicit null
|
|
29
|
+
* const nullOnlySchema = l.null()
|
|
30
|
+
*
|
|
31
|
+
* // Nullable string (string or null)
|
|
32
|
+
* const nullableStringSchema = l.union([l.string(), l.null()])
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
5
35
|
export declare const nullSchema: () => NullSchema;
|
|
6
36
|
export { nullSchema as null };
|
|
7
37
|
//# 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,QAAQ,CAAC,IAAI,EAAG,MAAM,CAAS;IAE/B,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,15 +3,45 @@ 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 {
|
|
20
|
+
type = 'null';
|
|
7
21
|
validateInContext(input, ctx) {
|
|
8
22
|
if (input !== null) {
|
|
9
|
-
return ctx.
|
|
23
|
+
return ctx.issueUnexpectedType(input, 'null');
|
|
10
24
|
}
|
|
11
25
|
return ctx.success(null);
|
|
12
26
|
}
|
|
13
27
|
}
|
|
14
28
|
exports.NullSchema = NullSchema;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a null schema that only accepts the null value.
|
|
31
|
+
*
|
|
32
|
+
* Useful for explicitly representing null in union types or optional fields.
|
|
33
|
+
*
|
|
34
|
+
* @returns A new {@link NullSchema} instance
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* // Explicit null
|
|
39
|
+
* const nullOnlySchema = l.null()
|
|
40
|
+
*
|
|
41
|
+
* // Nullable string (string or null)
|
|
42
|
+
* const nullableStringSchema = l.union([l.string(), l.null()])
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
15
45
|
exports.nullSchema = (0, memoize_js_1.memoizedOptions)(function () {
|
|
16
46
|
return new NullSchema();
|
|
17
47
|
});
|
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;
|
|
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;IACjC,IAAI,GAAG,MAAe,CAAA;IAE/B,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO,GAAG,CAAC,mBAAmB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QAC/C,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;CACF;AAVD,gCAUC;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 readonly type = 'null' as const\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (input !== null) {\n return ctx.issueUnexpectedType(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,50 @@
|
|
|
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;
|
|
19
|
+
readonly type: "nullable";
|
|
4
20
|
constructor(validator: TValidator);
|
|
5
21
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationFailure | import("../core.js").ValidationSuccess<null> | import("../core.js").ValidationSuccess<InferInput<TValidator>>;
|
|
6
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates a nullable schema that accepts null in addition to the wrapped type.
|
|
25
|
+
*
|
|
26
|
+
* Wraps another schema to allow null values. Different from `optional()` which
|
|
27
|
+
* allows undefined.
|
|
28
|
+
*
|
|
29
|
+
* @param validator - The validator to make nullable
|
|
30
|
+
* @returns A new {@link NullableSchema} instance
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* // Nullable string
|
|
35
|
+
* const nullableString = l.nullable(l.string())
|
|
36
|
+
* nullableString.parse(null) // null
|
|
37
|
+
* nullableString.parse('hello') // 'hello'
|
|
38
|
+
*
|
|
39
|
+
* // In an object
|
|
40
|
+
* const userSchema = l.object({
|
|
41
|
+
* name: l.string(),
|
|
42
|
+
* deletedAt: l.nullable(l.string({ format: 'datetime' })),
|
|
43
|
+
* })
|
|
44
|
+
*
|
|
45
|
+
* // Combine with optional for null or undefined
|
|
46
|
+
* const maybeString = l.optional(l.nullable(l.string()))
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
7
49
|
export declare const nullable: <const TValidator extends Validator>(validator: TValidator) => NullableSchema<TValidator>;
|
|
8
50
|
//# 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;
|
|
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;IAGa,QAAQ,CAAC,SAAS,EAAE,UAAU;IAF1C,QAAQ,CAAC,IAAI,EAAG,UAAU,CAAS;gBAEd,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,8 +3,24 @@ 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;
|
|
23
|
+
type = 'nullable';
|
|
8
24
|
constructor(validator) {
|
|
9
25
|
super();
|
|
10
26
|
this.validator = validator;
|
|
@@ -17,6 +33,32 @@ class NullableSchema extends core_js_1.Schema {
|
|
|
17
33
|
}
|
|
18
34
|
}
|
|
19
35
|
exports.NullableSchema = NullableSchema;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a nullable schema that accepts null in addition to the wrapped type.
|
|
38
|
+
*
|
|
39
|
+
* Wraps another schema to allow null values. Different from `optional()` which
|
|
40
|
+
* allows undefined.
|
|
41
|
+
*
|
|
42
|
+
* @param validator - The validator to make nullable
|
|
43
|
+
* @returns A new {@link NullableSchema} instance
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* // Nullable string
|
|
48
|
+
* const nullableString = l.nullable(l.string())
|
|
49
|
+
* nullableString.parse(null) // null
|
|
50
|
+
* nullableString.parse('hello') // 'hello'
|
|
51
|
+
*
|
|
52
|
+
* // In an object
|
|
53
|
+
* const userSchema = l.object({
|
|
54
|
+
* name: l.string(),
|
|
55
|
+
* deletedAt: l.nullable(l.string({ format: 'datetime' })),
|
|
56
|
+
* })
|
|
57
|
+
*
|
|
58
|
+
* // Combine with optional for null or undefined
|
|
59
|
+
* const maybeString = l.optional(l.nullable(l.string()))
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
20
62
|
exports.nullable = (0, memoize_js_1.memoizedTransformer)(function (validator) {
|
|
21
63
|
return new NullableSchema(validator);
|
|
22
64
|
});
|
|
@@ -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;
|
|
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;IAGsB;IAFZ,IAAI,GAAG,UAAmB,CAAA;IAEnC,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;AAjBD,wCAiBC;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 readonly type = 'nullable' as const\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,14 +1,71 @@
|
|
|
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<{
|
|
6
28
|
[K in keyof TShape]: InferOutput<TShape[K]>;
|
|
7
29
|
}>> {
|
|
8
30
|
readonly shape: TShape;
|
|
31
|
+
readonly type: "object";
|
|
9
32
|
constructor(shape: TShape);
|
|
10
33
|
get validatorsMap(): Map<string, Validator>;
|
|
11
34
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<Record<string, unknown>>;
|
|
12
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Creates an object schema with the specified property validators.
|
|
38
|
+
*
|
|
39
|
+
* Validates that the input is a plain object and each property matches
|
|
40
|
+
* its corresponding schema. Properties wrapped in `optional()` are not required.
|
|
41
|
+
*
|
|
42
|
+
* @param properties - Object mapping property names to their validators
|
|
43
|
+
* @returns A new {@link ObjectSchema} instance
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* // Basic object
|
|
48
|
+
* const userSchema = l.object({
|
|
49
|
+
* name: l.string(),
|
|
50
|
+
* email: l.string({ format: 'uri' }),
|
|
51
|
+
* })
|
|
52
|
+
*
|
|
53
|
+
* // With optional properties
|
|
54
|
+
* const profileSchema = l.object({
|
|
55
|
+
* displayName: l.string(),
|
|
56
|
+
* bio: l.optional(l.string({ maxLength: 256 })),
|
|
57
|
+
* avatar: l.optional(l.blob({ accept: ['image/*'] })),
|
|
58
|
+
* })
|
|
59
|
+
*
|
|
60
|
+
* // Nested objects
|
|
61
|
+
* const postSchema = l.object({
|
|
62
|
+
* text: l.string(),
|
|
63
|
+
* author: l.object({
|
|
64
|
+
* did: l.string({ format: 'did' }),
|
|
65
|
+
* handle: l.string({ format: 'handle' }),
|
|
66
|
+
* }),
|
|
67
|
+
* })
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
13
70
|
export declare function object<const TShape extends ObjectSchemaShape>(properties: TShape): ObjectSchema<TShape>;
|
|
14
71
|
//# 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;
|
|
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;IAGa,QAAQ,CAAC,KAAK,EAAE,MAAM;IAFlC,QAAQ,CAAC,IAAI,EAAG,QAAQ,CAAS;gBAEZ,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"}
|
package/dist/schema/object.js
CHANGED
|
@@ -5,8 +5,26 @@ exports.object = object;
|
|
|
5
5
|
const lex_data_1 = require("@atproto/lex-data");
|
|
6
6
|
const core_js_1 = require("../core.js");
|
|
7
7
|
const lazy_property_js_1 = require("../util/lazy-property.js");
|
|
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
|
+
*/
|
|
8
25
|
class ObjectSchema extends core_js_1.Schema {
|
|
9
26
|
shape;
|
|
27
|
+
type = 'object';
|
|
10
28
|
constructor(shape) {
|
|
11
29
|
super();
|
|
12
30
|
this.shape = shape;
|
|
@@ -17,7 +35,7 @@ class ObjectSchema extends core_js_1.Schema {
|
|
|
17
35
|
}
|
|
18
36
|
validateInContext(input, ctx) {
|
|
19
37
|
if (!(0, lex_data_1.isPlainObject)(input)) {
|
|
20
|
-
return ctx.
|
|
38
|
+
return ctx.issueUnexpectedType(input, 'object');
|
|
21
39
|
}
|
|
22
40
|
// Lazily copy value
|
|
23
41
|
let copy;
|
|
@@ -47,6 +65,40 @@ class ObjectSchema extends core_js_1.Schema {
|
|
|
47
65
|
}
|
|
48
66
|
}
|
|
49
67
|
exports.ObjectSchema = ObjectSchema;
|
|
68
|
+
/**
|
|
69
|
+
* Creates an object schema with the specified property validators.
|
|
70
|
+
*
|
|
71
|
+
* Validates that the input is a plain object and each property matches
|
|
72
|
+
* its corresponding schema. Properties wrapped in `optional()` are not required.
|
|
73
|
+
*
|
|
74
|
+
* @param properties - Object mapping property names to their validators
|
|
75
|
+
* @returns A new {@link ObjectSchema} instance
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* // Basic object
|
|
80
|
+
* const userSchema = l.object({
|
|
81
|
+
* name: l.string(),
|
|
82
|
+
* email: l.string({ format: 'uri' }),
|
|
83
|
+
* })
|
|
84
|
+
*
|
|
85
|
+
* // With optional properties
|
|
86
|
+
* const profileSchema = l.object({
|
|
87
|
+
* displayName: l.string(),
|
|
88
|
+
* bio: l.optional(l.string({ maxLength: 256 })),
|
|
89
|
+
* avatar: l.optional(l.blob({ accept: ['image/*'] })),
|
|
90
|
+
* })
|
|
91
|
+
*
|
|
92
|
+
* // Nested objects
|
|
93
|
+
* const postSchema = l.object({
|
|
94
|
+
* text: l.string(),
|
|
95
|
+
* author: l.object({
|
|
96
|
+
* did: l.string({ format: 'did' }),
|
|
97
|
+
* handle: l.string({ format: 'handle' }),
|
|
98
|
+
* }),
|
|
99
|
+
* })
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
50
102
|
/*@__NO_SIDE_EFFECTS__*/
|
|
51
103
|
function object(properties) {
|
|
52
104
|
return new ObjectSchema(properties);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.js","sourceRoot":"","sources":["../../src/schema/object.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../../src/schema/object.ts"],"names":[],"mappings":";;;AAmIA,wBAIC;AAvID,gDAAiD;AACjD,wCAOmB;AACnB,+DAAuD;AASvD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAa,YAEX,SAAQ,gBAOT;IAGsB;IAFZ,IAAI,GAAG,QAAiB,CAAA;IAEjC,YAAqB,KAAa;QAChC,KAAK,EAAE,CAAA;QADY,UAAK,GAAL,KAAK,CAAQ;IAElC,CAAC;IAED,IAAI,aAAa;QACf,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;QAE/C,OAAO,IAAA,+BAAY,EAAC,IAAI,EAAE,eAAe,EAAE,GAAG,CAAC,CAAA;IACjD,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,CAAC,IAAA,wBAAa,EAAC,KAAK,CAAC,EAAE,CAAC;YAC1B,OAAO,GAAG,CAAC,mBAAmB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACjD,CAAC;QAED,oBAAoB;QACpB,IAAI,IAAyC,CAAA;QAE7C,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YAChD,MAAM,MAAM,GAAG,GAAG,CAAC,aAAa,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,CAAC,CAAA;YACrD,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;gBACpB,IAAI,CAAC,CAAC,GAAG,IAAI,KAAK,CAAC,EAAE,CAAC;oBACpB,sCAAsC;oBACtC,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;gBACzC,CAAC;gBAED,OAAO,MAAM,CAAA;YACf,CAAC;YAED,uEAAuE;YACvE,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,IAAI,CAAC,CAAC,GAAG,IAAI,KAAK,CAAC,EAAE,CAAC;gBAClD,SAAQ;YACV,CAAC;YAED,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;gBACzC,IAAI,GAAG,CAAC,OAAO,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;oBACpC,sEAAsE;oBACtE,OAAO,GAAG,CAAC,yBAAyB,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;gBAClE,CAAC;gBAED,IAAI,KAAK,EAAE,GAAG,KAAK,EAAE,CAAA;gBACrB,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAA;YAC1B,CAAC;QACH,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,KAAK,CAAC,CAAA;IACnC,CAAC;CACF;AA3DD,oCA2DC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAwB;AACxB,SAAgB,MAAM,CACpB,UAAkB;IAElB,OAAO,IAAI,YAAY,CAAS,UAAU,CAAC,CAAA;AAC7C,CAAC","sourcesContent":["import { isPlainObject } from '@atproto/lex-data'\nimport {\n InferInput,\n InferOutput,\n Schema,\n ValidationContext,\n Validator,\n WithOptionalProperties,\n} from '../core.js'\nimport { lazyProperty } from '../util/lazy-property.js'\n\n/**\n * Type representing the shape of an object schema.\n *\n * Maps property names to their corresponding validators.\n */\nexport type ObjectSchemaShape = Record<string, Validator>\n\n/**\n * Schema for validating objects with a defined shape.\n *\n * Each property in the shape is validated against its corresponding schema.\n * Properties wrapped in `optional()` are not required.\n *\n * @template TShape - The object shape type mapping property names to validators\n *\n * @example\n * ```ts\n * const schema = new ObjectSchema({\n * name: l.string(),\n * age: l.optional(l.integer()),\n * })\n * const result = schema.validate({ name: 'Alice' })\n * ```\n */\nexport class ObjectSchema<\n const TShape extends ObjectSchemaShape = any,\n> extends Schema<\n WithOptionalProperties<{\n [K in keyof TShape]: InferInput<TShape[K]>\n }>,\n WithOptionalProperties<{\n [K in keyof TShape]: InferOutput<TShape[K]>\n }>\n> {\n readonly type = 'object' as const\n\n constructor(readonly shape: TShape) {\n super()\n }\n\n get validatorsMap(): Map<string, Validator> {\n const map = new Map(Object.entries(this.shape))\n\n return lazyProperty(this, 'validatorsMap', map)\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (!isPlainObject(input)) {\n return ctx.issueUnexpectedType(input, 'object')\n }\n\n // Lazily copy value\n let copy: undefined | Record<string, unknown>\n\n for (const [key, propDef] of this.validatorsMap) {\n const result = ctx.validateChild(input, key, propDef)\n if (!result.success) {\n if (!(key in input)) {\n // Transform into \"required key\" issue\n return ctx.issueRequiredKey(input, key)\n }\n\n return result\n }\n\n // Skip copying if key is not present in input (and value is undefined)\n if (result.value === undefined && !(key in input)) {\n continue\n }\n\n if (!Object.is(result.value, input[key])) {\n if (ctx.options.mode === 'validate') {\n // In \"validate\" mode, we can't modify the input, so we issue an error\n return ctx.issueInvalidPropertyValue(input, key, [result.value])\n }\n\n copy ??= { ...input }\n copy[key] = result.value\n }\n }\n\n return ctx.success(copy ?? input)\n }\n}\n\n/**\n * Creates an object schema with the specified property validators.\n *\n * Validates that the input is a plain object and each property matches\n * its corresponding schema. Properties wrapped in `optional()` are not required.\n *\n * @param properties - Object mapping property names to their validators\n * @returns A new {@link ObjectSchema} instance\n *\n * @example\n * ```ts\n * // Basic object\n * const userSchema = l.object({\n * name: l.string(),\n * email: l.string({ format: 'uri' }),\n * })\n *\n * // With optional properties\n * const profileSchema = l.object({\n * displayName: l.string(),\n * bio: l.optional(l.string({ maxLength: 256 })),\n * avatar: l.optional(l.blob({ accept: ['image/*'] })),\n * })\n *\n * // Nested objects\n * const postSchema = l.object({\n * text: l.string(),\n * author: l.object({\n * did: l.string({ format: 'did' }),\n * handle: l.string({ format: 'handle' }),\n * }),\n * })\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function object<const TShape extends ObjectSchemaShape>(\n properties: TShape,\n) {\n return new ObjectSchema<TShape>(properties)\n}\n"]}
|
|
@@ -1,9 +1,52 @@
|
|
|
1
1
|
import { InferInput, InferOutput, Schema, UnwrapValidator, ValidationContext, Validator } from '../core.js';
|
|
2
2
|
import { WithDefaultSchema } from './with-default.js';
|
|
3
|
+
/**
|
|
4
|
+
* Schema wrapper that makes a value optional (allows undefined).
|
|
5
|
+
*
|
|
6
|
+
* When the input is `undefined`, validation succeeds without running the
|
|
7
|
+
* inner validator. If the inner validator has a default value (via `withDefault`),
|
|
8
|
+
* that default will be applied in parse mode.
|
|
9
|
+
*
|
|
10
|
+
* @template TValidator - The wrapped validator type
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const schema = new OptionalSchema(l.string())
|
|
15
|
+
* schema.validate(undefined) // success
|
|
16
|
+
* schema.validate('hello') // success
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
3
19
|
export declare class OptionalSchema<TValidator extends Validator> extends Schema<InferInput<TValidator> | undefined, UnwrapValidator<TValidator> extends WithDefaultSchema<infer TValidator> ? InferOutput<TValidator> : InferOutput<TValidator> | undefined> {
|
|
4
20
|
readonly validator: TValidator;
|
|
21
|
+
readonly type: "optional";
|
|
5
22
|
constructor(validator: TValidator);
|
|
6
23
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<undefined> | import("../core.js").ValidationSuccess<InferInput<TValidator>>;
|
|
7
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates an optional schema that allows undefined values.
|
|
27
|
+
*
|
|
28
|
+
* Wraps another schema to make it optional. When used in an object schema,
|
|
29
|
+
* properties with optional schemas are not required.
|
|
30
|
+
*
|
|
31
|
+
* @param validator - The validator to make optional
|
|
32
|
+
* @returns A new {@link OptionalSchema} instance
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* // Optional string
|
|
37
|
+
* const optionalBio = l.optional(l.string())
|
|
38
|
+
*
|
|
39
|
+
* // In an object - property is not required
|
|
40
|
+
* const userSchema = l.object({
|
|
41
|
+
* name: l.string(),
|
|
42
|
+
* bio: l.optional(l.string()),
|
|
43
|
+
* })
|
|
44
|
+
* userSchema.parse({ name: 'Alice' }) // Valid, bio is undefined
|
|
45
|
+
*
|
|
46
|
+
* // With default value
|
|
47
|
+
* const countSchema = l.optional(l.withDefault(l.integer(), 0))
|
|
48
|
+
* countSchema.parse(undefined) // Returns 0
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
8
51
|
export declare const optional: <const TValidator extends Validator>(validator: TValidator) => OptionalSchema<TValidator>;
|
|
9
52
|
//# sourceMappingURL=optional.d.ts.map
|