@atproto/lex-schema 0.0.20 → 0.1.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/dist/core/$type.js +2 -6
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.js +4 -8
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/result.js +6 -14
- package/dist/core/result.js.map +1 -1
- package/dist/core/schema.js +16 -20
- package/dist/core/schema.js.map +1 -1
- package/dist/core/standard-schema.js +5 -10
- package/dist/core/standard-schema.js.map +1 -1
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +37 -61
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.js +1 -2
- package/dist/core/validation-error.js +14 -25
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.js +12 -40
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.js +19 -28
- package/dist/core/validator.js.map +1 -1
- package/dist/core.js +9 -12
- package/dist/core.js.map +1 -1
- package/dist/external.js +3 -6
- package/dist/external.js.map +1 -1
- package/dist/helpers.js +5 -9
- package/dist/helpers.js.map +1 -1
- package/dist/index.js +3 -7
- package/dist/index.js.map +1 -1
- package/dist/schema/array.js +5 -11
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.js +10 -17
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.js +8 -9
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.js +7 -12
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.js +7 -12
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.js +5 -13
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.js +6 -13
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.js +10 -18
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.js +6 -11
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.js +5 -10
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.js +4 -11
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/lex-map.js +13 -14
- package/dist/schema/lex-map.js.map +1 -1
- package/dist/schema/lex-value.js +11 -12
- package/dist/schema/lex-value.js.map +1 -1
- package/dist/schema/literal.js +4 -10
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.js +8 -9
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.js +9 -10
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.js +5 -10
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.js +8 -14
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.js +5 -10
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.js +39 -44
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.js +5 -13
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.js +2 -10
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.js +2 -9
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.js +3 -13
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.js +3 -12
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.js +15 -24
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.js +4 -9
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.js +3 -6
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.js +4 -11
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.js +15 -26
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.js +3 -12
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.js +5 -11
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.js +11 -18
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.js +4 -9
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.js +8 -15
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.js +5 -11
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown.js +8 -9
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.js +4 -11
- package/dist/schema/with-default.js.map +1 -1
- package/dist/schema.js +38 -41
- package/dist/schema.js.map +1 -1
- package/dist/util/array-agg.js +1 -4
- package/dist/util/array-agg.js.map +1 -1
- package/dist/util/assertion-util.js +1 -2
- package/dist/util/if-any.js +1 -2
- package/dist/util/lazy-property.js +1 -4
- package/dist/util/lazy-property.js.map +1 -1
- package/dist/util/memoize.js +2 -6
- package/dist/util/memoize.js.map +1 -1
- package/package.json +7 -8
- package/src/core/string-format.ts +2 -1
- package/src/helpers.test.ts +52 -52
- package/src/schema/record.test.ts +2 -2
- package/src/schema/ref.test.ts +1 -1
- package/src/schema/string.test.ts +6 -6
- package/src/schema/subscription.test.ts +4 -4
- package/src/schema/typed-object.test.ts +2 -2
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.typedObject = typedObject;
|
|
5
|
-
const lex_data_1 = require("@atproto/lex-data");
|
|
6
|
-
const core_js_1 = require("../core.js");
|
|
7
|
-
const lazy_property_js_1 = require("../util/lazy-property.js");
|
|
1
|
+
import { isPlainObject } from '@atproto/lex-data';
|
|
2
|
+
import { $type, $typed, Schema, } from '../core.js';
|
|
3
|
+
import { lazyProperty } from '../util/lazy-property.js';
|
|
8
4
|
/**
|
|
9
5
|
* Schema for typed objects in Lexicon unions.
|
|
10
6
|
*
|
|
@@ -23,17 +19,15 @@ const lazy_property_js_1 = require("../util/lazy-property.js");
|
|
|
23
19
|
* )
|
|
24
20
|
* ```
|
|
25
21
|
*/
|
|
26
|
-
class TypedObjectSchema extends
|
|
27
|
-
$type;
|
|
28
|
-
schema;
|
|
29
|
-
type = 'typedObject';
|
|
22
|
+
export class TypedObjectSchema extends Schema {
|
|
30
23
|
constructor($type, schema) {
|
|
31
24
|
super();
|
|
32
25
|
this.$type = $type;
|
|
33
26
|
this.schema = schema;
|
|
27
|
+
this.type = 'typedObject';
|
|
34
28
|
}
|
|
35
29
|
validateInContext(input, ctx) {
|
|
36
|
-
if (!
|
|
30
|
+
if (!isPlainObject(input)) {
|
|
37
31
|
return ctx.issueUnexpectedType(input, 'object');
|
|
38
32
|
}
|
|
39
33
|
if ('$type' in input &&
|
|
@@ -44,7 +38,7 @@ class TypedObjectSchema extends core_js_1.Schema {
|
|
|
44
38
|
return ctx.validate(input, this.schema);
|
|
45
39
|
}
|
|
46
40
|
build(input) {
|
|
47
|
-
return
|
|
41
|
+
return $typed(input, this.$type);
|
|
48
42
|
}
|
|
49
43
|
isTypeOf(value) {
|
|
50
44
|
return value.$type === undefined || value.$type === this.$type;
|
|
@@ -54,19 +48,18 @@ class TypedObjectSchema extends core_js_1.Schema {
|
|
|
54
48
|
* @see {@link build}
|
|
55
49
|
*/
|
|
56
50
|
get $build() {
|
|
57
|
-
return
|
|
51
|
+
return lazyProperty(this, '$build', this.build.bind(this));
|
|
58
52
|
}
|
|
59
53
|
/**
|
|
60
54
|
* Bound alias for {@link isTypeOf} for compatibility with generated utilities.
|
|
61
55
|
* @see {@link isTypeOf}
|
|
62
56
|
*/
|
|
63
57
|
get $isTypeOf() {
|
|
64
|
-
return
|
|
58
|
+
return lazyProperty(this, '$isTypeOf', this.isTypeOf.bind(this));
|
|
65
59
|
}
|
|
66
60
|
}
|
|
67
|
-
exports.TypedObjectSchema = TypedObjectSchema;
|
|
68
61
|
/*@__NO_SIDE_EFFECTS__*/
|
|
69
|
-
function typedObject(nsid, hash, validator) {
|
|
70
|
-
return new TypedObjectSchema(
|
|
62
|
+
export function typedObject(nsid, hash, validator) {
|
|
63
|
+
return new TypedObjectSchema($type(nsid, hash), validator);
|
|
71
64
|
}
|
|
72
65
|
//# sourceMappingURL=typed-object.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-object.js","sourceRoot":"","sources":["../../src/schema/typed-object.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"typed-object.js","sourceRoot":"","sources":["../../src/schema/typed-object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACzD,OAAO,EAKL,KAAK,EACL,MAAM,EAIN,MAAM,GAIP,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AASvD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,iBAGX,SAAQ,MAGT;IAGC,YACW,KAAY,EACZ,MAAc;QAEvB,KAAK,EAAE,CAAA;QAHE,UAAK,GAAL,KAAK,CAAO;QACZ,WAAM,GAAN,MAAM,CAAQ;QAJhB,SAAI,GAAG,aAAsB,CAAA;IAOtC,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1B,OAAO,GAAG,CAAC,mBAAmB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACjD,CAAC;QAED,IACE,OAAO,IAAI,KAAK;YAChB,KAAK,CAAC,KAAK,KAAK,SAAS;YACzB,KAAK,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,EAC1B,CAAC;YACD,OAAO,GAAG,CAAC,yBAAyB,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;QACpE,CAAC;QAED,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;IACzC,CAAC;IAQD,KAAK,CAAC,KAA8B;QAClC,OAAO,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;IAClC,CAAC;IAED,QAAQ,CACN,KAAa;QAEb,OAAO,KAAK,CAAC,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,CAAA;IAChE,CAAC;IAED;;;OAGG;IACH,IAAI,MAAM;QACR,OAAO,YAAY,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IAC5D,CAAC;IAED;;;OAGG;IACH,IAAI,SAAS;QACX,OAAO,YAAY,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IAClE,CAAC;CACF;AAmED,wBAAwB;AACxB,MAAM,UAAU,WAAW,CAIzB,IAAO,EAAE,IAAO,EAAE,SAAY;IAC9B,OAAO,IAAI,iBAAiB,CAAiB,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,SAAS,CAAC,CAAA;AAC5E,CAAC","sourcesContent":["import { LexMap, isPlainObject } from '@atproto/lex-data'\nimport {\n $Type,\n $TypeOf,\n $Typed,\n $TypedMaybe,\n $type,\n $typed,\n InferInput,\n InferOutput,\n NsidString,\n Schema,\n Unknown$TypedObject,\n ValidationContext,\n Validator,\n} from '../core.js'\nimport { lazyProperty } from '../util/lazy-property.js'\n\nexport type MaybeTypedObject<\n TType extends $Type,\n TValue extends { $type?: unknown } = { $type?: unknown },\n> = TValue extends { $type?: TType }\n ? TValue\n : $TypedMaybe<Exclude<TValue, Unknown$TypedObject>, TType>\n\n/**\n * Schema for typed objects in Lexicon unions.\n *\n * Typed objects have a `$type` field that identifies which variant they are\n * in a union. The `$type` can be omitted in input (it's implicit), but if\n * present, it must match the expected value.\n *\n * @template TType - The $type string literal type\n * @template TShape - The validator type for the object's shape\n *\n * @example\n * ```ts\n * const schema = new TypedObjectSchema(\n * 'app.bsky.embed.images#view',\n * l.object({ images: l.array(imageSchema) })\n * )\n * ```\n */\nexport class TypedObjectSchema<\n const TType extends $Type = $Type,\n const TShape extends Validator<LexMap> = Validator<LexMap>,\n> extends Schema<\n $TypedMaybe<InferInput<TShape>, TType>,\n $TypedMaybe<InferOutput<TShape>, TType>\n> {\n readonly type = 'typedObject' as const\n\n constructor(\n readonly $type: TType,\n readonly schema: TShape,\n ) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (!isPlainObject(input)) {\n return ctx.issueUnexpectedType(input, 'object')\n }\n\n if (\n '$type' in input &&\n input.$type !== undefined &&\n input.$type !== this.$type\n ) {\n return ctx.issueInvalidPropertyValue(input, '$type', [this.$type])\n }\n\n return ctx.validate(input, this.schema)\n }\n\n build(\n input: Omit<InferOutput<TShape>, '$type'>,\n ): $Typed<InferOutput<TShape>, TType>\n build(\n input: Omit<InferInput<TShape>, '$type'>,\n ): $Typed<InferInput<TShape>, TType>\n build(input: Record<string, unknown>) {\n return $typed(input, this.$type)\n }\n\n isTypeOf<TValue extends Record<string, unknown>>(\n value: TValue,\n ): value is MaybeTypedObject<TType, TValue> {\n return value.$type === undefined || value.$type === this.$type\n }\n\n /**\n * Bound alias for {@link build} for compatibility with generated utilities.\n * @see {@link build}\n */\n get $build(): typeof this.build {\n return lazyProperty(this, '$build', this.build.bind(this))\n }\n\n /**\n * Bound alias for {@link isTypeOf} for compatibility with generated utilities.\n * @see {@link isTypeOf}\n */\n get $isTypeOf(): typeof this.isTypeOf {\n return lazyProperty(this, '$isTypeOf', this.isTypeOf.bind(this))\n }\n}\n\n/**\n * Creates a typed object schema for use in Lexicon unions.\n *\n * Typed objects are identified by their `$type` field, which combines an NSID\n * and a hash (e.g., 'app.bsky.embed.images#view'). Used for union variants.\n *\n * This function offers two overloads:\n * - One that infers the type from arguments (no circular reference support)\n * - One with explicit interface for codegen with circular references\n *\n * @param nsid - The NSID part of the type (e.g., 'app.bsky.embed.images')\n * @param hash - The hash part of the type (e.g., 'view'), defaults to 'main'\n * @param validator - Schema for validating the object properties\n * @returns A new {@link TypedObjectSchema} instance\n *\n * @example\n * ```ts\n * // Image embed view\n * const imageViewSchema = l.typedObject(\n * 'app.bsky.embed.images',\n * 'view',\n * l.object({\n * images: l.array(l.object({\n * thumb: l.string(),\n * fullsize: l.string(),\n * alt: l.string(),\n * })),\n * })\n * )\n *\n * // Main type (hash defaults to 'main')\n * const postViewSchema = l.typedObject(\n * 'app.bsky.feed.defs',\n * 'postView',\n * l.object({ uri: l.string(), cid: l.string(), author: authorSchema })\n * )\n *\n * // Use $isTypeOf to narrow union types\n * if (imageViewSchema.$isTypeOf(embed)) {\n * // embed is narrowed to image view type\n * }\n *\n * // Use $build to construct typed objects\n * const view = imageViewSchema.$build({ images: [...] })\n * // view.$type === 'app.bsky.embed.images#view'\n * ```\n */\nexport function typedObject<\n const N extends NsidString,\n const H extends string,\n const S extends Validator<LexMap>,\n>(nsid: N, hash: H, validator: S): TypedObjectSchema<$Type<N, H>, S>\nexport function typedObject<V extends { $type?: $Type }>(\n nsid: V extends { $type?: infer T extends string }\n ? T extends `${infer N}#${string}`\n ? N\n : T // (T is a \"main\" type, so already an NSID)\n : never,\n hash: V extends { $type?: infer T extends string }\n ? T extends `${string}#${infer H}`\n ? H\n : 'main'\n : never,\n validator: Validator<Omit<V, '$type'>>,\n): TypedObjectSchema<$TypeOf<V>, Validator<V>>\n/*@__NO_SIDE_EFFECTS__*/\nexport function typedObject<\n const N extends NsidString,\n const H extends string,\n const S extends Validator<LexMap>,\n>(nsid: N, hash: H, validator: S) {\n return new TypedObjectSchema<$Type<N, H>, S>($type(nsid, hash), validator)\n}\n"]}
|
package/dist/schema/typed-ref.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TypedRefSchema = void 0;
|
|
4
|
-
exports.typedRef = typedRef;
|
|
5
|
-
const core_js_1 = require("../core.js");
|
|
1
|
+
import { Schema, } from '../core.js';
|
|
6
2
|
/**
|
|
7
3
|
* Schema for referencing typed objects with lazy resolution.
|
|
8
4
|
*
|
|
@@ -18,13 +14,13 @@ const core_js_1 = require("../core.js");
|
|
|
18
14
|
* // ref.$type === 'app.bsky.embed.images#view'
|
|
19
15
|
* ```
|
|
20
16
|
*/
|
|
21
|
-
class TypedRefSchema extends
|
|
22
|
-
type = 'typedRef';
|
|
17
|
+
export class TypedRefSchema extends Schema {
|
|
23
18
|
#getter;
|
|
24
19
|
constructor(getter) {
|
|
25
20
|
// @NOTE In order to avoid circular dependency issues, we don't resolve
|
|
26
21
|
// the schema here. Instead, we resolve it lazily when first accessed.
|
|
27
22
|
super();
|
|
23
|
+
this.type = 'typedRef';
|
|
28
24
|
this.#getter = getter;
|
|
29
25
|
}
|
|
30
26
|
get validator() {
|
|
@@ -43,8 +39,7 @@ class TypedRefSchema extends core_js_1.Schema {
|
|
|
43
39
|
return result;
|
|
44
40
|
}
|
|
45
41
|
}
|
|
46
|
-
|
|
47
|
-
function typedRef(get) {
|
|
42
|
+
export function typedRef(get) {
|
|
48
43
|
return new TypedRefSchema(get);
|
|
49
44
|
}
|
|
50
45
|
//# sourceMappingURL=typed-ref.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-ref.js","sourceRoot":"","sources":["../../src/schema/typed-ref.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"typed-ref.js","sourceRoot":"","sources":["../../src/schema/typed-ref.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,MAAM,GAGP,MAAM,YAAY,CAAA;AAyBnB;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,cAEX,SAAQ,MAGT;IAGC,OAAO,CAA4B;IAEnC,YAAY,MAAkC;QAC5C,uEAAuE;QACvE,sEAAsE;QAEtE,KAAK,EAAE,CAAA;QARA,SAAI,GAAG,UAAmB,CAAA;QAUjC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;IACvB,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAChC,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAA;IAC7B,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,MAAM,MAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;QAClD,IAAI,CAAC,MAAM,CAAC,OAAO;YAAE,OAAO,MAAM,CAAA;QAElC,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YACtC,OAAO,GAAG,CAAC,yBAAyB,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;QAC3E,CAAC;QAED,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAqCD,MAAM,UAAU,QAAQ,CACtB,GAA+B;IAE/B,OAAO,IAAI,cAAc,CAAa,GAAG,CAAC,CAAA;AAC5C,CAAC","sourcesContent":["import {\n $Typed,\n InferInput,\n InferOutput,\n Schema,\n ValidationContext,\n Validator,\n} from '../core.js'\n\n/**\n * Interface for validators that have a $type property.\n *\n * Used by typed objects and records to identify their type in unions.\n *\n * @template TInput - The input type (with optional $type)\n * @template TOutput - The output type (with non-optional $type)\n */\nexport interface TypedObjectValidator<\n TInput extends { $type?: string } = { $type?: string },\n TOutput extends TInput = TInput,\n> extends Validator<TInput, TOutput> {\n $type: NonNullable<TOutput['$type']>\n}\n\n/**\n * Function type that returns a typed object validator, used for lazy resolution.\n *\n * @template TValidator - The typed object validator type\n */\nexport type TypedRefGetter<out TValidator extends TypedObjectValidator> =\n () => TValidator\n\n/**\n * Schema for referencing typed objects with lazy resolution.\n *\n * Used in typed unions to reference typed object schemas. Requires the\n * `$type` field to be present and match the referenced schema's type.\n * The referenced schema is resolved lazily to support circular references.\n *\n * @template TValidator - The referenced typed object validator type\n *\n * @example\n * ```ts\n * const ref = new TypedRefSchema(() => imageViewSchema)\n * // ref.$type === 'app.bsky.embed.images#view'\n * ```\n */\nexport class TypedRefSchema<\n const TValidator extends TypedObjectValidator = TypedObjectValidator,\n> extends Schema<\n $Typed<InferInput<TValidator>>,\n $Typed<InferOutput<TValidator>>\n> {\n readonly type = 'typedRef' as const\n\n #getter: TypedRefGetter<TValidator>\n\n constructor(getter: TypedRefGetter<TValidator>) {\n // @NOTE In order to avoid circular dependency issues, we don't resolve\n // the schema here. Instead, we resolve it lazily when first accessed.\n\n super()\n\n this.#getter = getter\n }\n\n get validator(): TValidator {\n return this.#getter.call(null)\n }\n\n get $type(): TValidator['$type'] {\n return this.validator.$type\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n const result = ctx.validate(input, this.validator)\n if (!result.success) return result\n\n if (result.value.$type !== this.$type) {\n return ctx.issueInvalidPropertyValue(result.value, '$type', [this.$type])\n }\n\n return result\n }\n}\n\n/**\n * Creates a reference to a typed object schema for use in typed unions.\n *\n * Unlike regular `ref()`, this requires the referenced schema to have a\n * `$type` property, and validates that the input's `$type` matches.\n *\n * @param get - Function that returns the typed object validator\n * @returns A new {@link TypedRefSchema} instance\n *\n * @example\n * ```ts\n * // Reference to image embed view\n * const imageRef = l.typedRef(() => imageViewSchema)\n *\n * // Use in a typed union\n * const embedUnion = l.typedUnion([\n * l.typedRef(() => imageViewSchema),\n * l.typedRef(() => videoViewSchema),\n * l.typedRef(() => externalViewSchema),\n * ], true) // closed union\n *\n * // The $type is accessible on the ref\n * console.log(imageRef.$type) // 'app.bsky.embed.images#view'\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function typedRef<const TValidator extends TypedObjectValidator>(\n get: TypedRefGetter<TValidator>,\n): TypedRefSchema<TValidator>\nexport function typedRef<\n TInput extends { $type?: string },\n TOutput extends TInput = TInput,\n>(\n get: TypedRefGetter<TypedObjectValidator<TInput, TOutput>>,\n): TypedRefSchema<TypedObjectValidator<TInput, TOutput>>\nexport function typedRef<const TValidator extends TypedObjectValidator>(\n get: TypedRefGetter<TValidator>,\n): TypedRefSchema<TValidator> {\n return new TypedRefSchema<TValidator>(get)\n}\n"]}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.typedUnion = typedUnion;
|
|
5
|
-
const lex_data_1 = require("@atproto/lex-data");
|
|
6
|
-
const core_js_1 = require("../core.js");
|
|
7
|
-
const lazy_property_js_1 = require("../util/lazy-property.js");
|
|
1
|
+
import { isPlainObject } from '@atproto/lex-data';
|
|
2
|
+
import { Schema, } from '../core.js';
|
|
3
|
+
import { lazyProperty } from '../util/lazy-property.js';
|
|
8
4
|
/**
|
|
9
5
|
* Schema for Lexicon typed unions (unions discriminated by $type).
|
|
10
6
|
*
|
|
@@ -23,10 +19,7 @@ const lazy_property_js_1 = require("../util/lazy-property.js");
|
|
|
23
19
|
* ], true) // closed - only accepts images and videos
|
|
24
20
|
* ```
|
|
25
21
|
*/
|
|
26
|
-
class TypedUnionSchema extends
|
|
27
|
-
validators;
|
|
28
|
-
closed;
|
|
29
|
-
type = 'typedUnion';
|
|
22
|
+
export class TypedUnionSchema extends Schema {
|
|
30
23
|
constructor(validators, closed) {
|
|
31
24
|
// @NOTE In order to avoid circular dependency issues, we don't access the
|
|
32
25
|
// refs's schema (or $type) here. Instead, we access them lazily when first
|
|
@@ -35,18 +28,19 @@ class TypedUnionSchema extends core_js_1.Schema {
|
|
|
35
28
|
super();
|
|
36
29
|
this.validators = validators;
|
|
37
30
|
this.closed = closed;
|
|
31
|
+
this.type = 'typedUnion';
|
|
38
32
|
}
|
|
39
33
|
get validatorsMap() {
|
|
40
34
|
const map = new Map();
|
|
41
35
|
for (const ref of this.validators)
|
|
42
36
|
map.set(ref.$type, ref);
|
|
43
|
-
return
|
|
37
|
+
return lazyProperty(this, 'validatorsMap', map);
|
|
44
38
|
}
|
|
45
39
|
get $types() {
|
|
46
40
|
return Array.from(this.validatorsMap.keys());
|
|
47
41
|
}
|
|
48
42
|
validateInContext(input, ctx) {
|
|
49
|
-
if (!
|
|
43
|
+
if (!isPlainObject(input) || !('$type' in input)) {
|
|
50
44
|
return ctx.issueUnexpectedType(input, '$typed');
|
|
51
45
|
}
|
|
52
46
|
const { $type } = input;
|
|
@@ -63,7 +57,6 @@ class TypedUnionSchema extends core_js_1.Schema {
|
|
|
63
57
|
return ctx.success(input);
|
|
64
58
|
}
|
|
65
59
|
}
|
|
66
|
-
exports.TypedUnionSchema = TypedUnionSchema;
|
|
67
60
|
/**
|
|
68
61
|
* Creates a typed union schema for Lexicon unions.
|
|
69
62
|
*
|
|
@@ -95,7 +88,7 @@ exports.TypedUnionSchema = TypedUnionSchema;
|
|
|
95
88
|
* ```
|
|
96
89
|
*/
|
|
97
90
|
/*@__NO_SIDE_EFFECTS__*/
|
|
98
|
-
function typedUnion(refs, closed) {
|
|
91
|
+
export function typedUnion(refs, closed) {
|
|
99
92
|
return new TypedUnionSchema(refs, closed);
|
|
100
93
|
}
|
|
101
94
|
//# sourceMappingURL=typed-union.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-union.js","sourceRoot":"","sources":["../../src/schema/typed-union.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"typed-union.js","sourceRoot":"","sources":["../../src/schema/typed-union.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,EAGL,MAAM,GAGP,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAIvD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,gBAMX,SAAQ,MAOT;IAGC,YACqB,UAAuB,EAC1B,MAAe;QAE/B,0EAA0E;QAC1E,2EAA2E;QAC3E,sEAAsE;QACtE,+DAA+D;QAE/D,KAAK,EAAE,CAAA;QARY,eAAU,GAAV,UAAU,CAAa;QAC1B,WAAM,GAAN,MAAM,CAAS;QAJxB,SAAI,GAAG,YAAqB,CAAA;IAYrC,CAAC;IAED,IAAI,aAAa;QACf,MAAM,GAAG,GAAG,IAAI,GAAG,EAAgC,CAAA;QACnD,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,UAAU;YAAE,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QAE1D,OAAO,YAAY,CAAC,IAAI,EAAE,eAAe,EAAE,GAAG,CAAC,CAAA;IACjD,CAAC;IAED,IAAI,MAAM;QACR,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC,CAAA;IAC9C,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,IAAI,KAAK,CAAC,EAAE,CAAC;YACjD,OAAO,GAAG,CAAC,mBAAmB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACjD,CAAC;QAED,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,CAAA;QAEvB,MAAM,SAAS,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QAC/C,IAAI,SAAS,EAAE,CAAC;YACd,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;QACvC,CAAC;QAED,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,OAAO,GAAG,CAAC,yBAAyB,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;QACnE,CAAC;QAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,OAAO,GAAG,CAAC,wBAAwB,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAA;QAC/D,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC3B,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAwB;AACxB,MAAM,UAAU,UAAU,CAGxB,IAAO,EAAE,MAAS;IAClB,OAAO,IAAI,gBAAgB,CAAO,IAAI,EAAE,MAAM,CAAC,CAAA;AACjD,CAAC","sourcesContent":["import { isPlainObject } from '@atproto/lex-data'\nimport {\n InferInput,\n InferOutput,\n Schema,\n Unknown$TypedObject,\n ValidationContext,\n} from '../core.js'\nimport { lazyProperty } from '../util/lazy-property.js'\nimport { TypedObjectSchema } from './typed-object.js'\nimport { TypedRefSchema } from './typed-ref.js'\n\n/**\n * Schema for Lexicon typed unions (unions discriminated by $type).\n *\n * Typed unions are collections of typed objects identified by their `$type`\n * field. Can be \"open\" (accept unknown types) or \"closed\" (only accept\n * known types).\n *\n * @template TValidators - Tuple of {@link TypedRefSchema} or {@link TypedObjectSchema} instances\n * @template TClosed - Whether the union is closed (rejects unknown $types)\n *\n * @example\n * ```ts\n * const embedUnion = new TypedUnionSchema([\n * l.typedRef(() => imageSchema),\n * l.typedRef(() => videoSchema),\n * ], true) // closed - only accepts images and videos\n * ```\n */\nexport class TypedUnionSchema<\n const TValidators extends readonly (\n | TypedRefSchema\n | TypedObjectSchema\n )[] = [],\n const TClosed extends boolean = boolean,\n> extends Schema<\n TClosed extends true\n ? InferInput<TValidators[number]>\n : InferInput<TValidators[number]> | Unknown$TypedObject,\n TClosed extends true\n ? InferOutput<TValidators[number]>\n : InferOutput<TValidators[number]> | Unknown$TypedObject\n> {\n readonly type = 'typedUnion' as const\n\n constructor(\n protected readonly validators: TValidators,\n public readonly closed: TClosed,\n ) {\n // @NOTE In order to avoid circular dependency issues, we don't access the\n // refs's schema (or $type) here. Instead, we access them lazily when first\n // needed. The biggest issue with this strategy is that we can't throw\n // early if the refs contain multiple refs with the same $type.\n\n super()\n }\n\n get validatorsMap(): Map<unknown, TValidators[number]> {\n const map = new Map<unknown, TValidators[number]>()\n for (const ref of this.validators) map.set(ref.$type, ref)\n\n return lazyProperty(this, 'validatorsMap', map)\n }\n\n get $types() {\n return Array.from(this.validatorsMap.keys())\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (!isPlainObject(input) || !('$type' in input)) {\n return ctx.issueUnexpectedType(input, '$typed')\n }\n\n const { $type } = input\n\n const validator = this.validatorsMap.get($type)\n if (validator) {\n return ctx.validate(input, validator)\n }\n\n if (this.closed) {\n return ctx.issueInvalidPropertyValue(input, '$type', this.$types)\n }\n\n if (typeof $type !== 'string') {\n return ctx.issueInvalidPropertyType(input, '$type', 'string')\n }\n\n return ctx.success(input)\n }\n}\n\n/**\n * Creates a typed union schema for Lexicon unions.\n *\n * Typed unions discriminate variants by their `$type` field. Can be open\n * (accepts unknown types, useful for extensibility) or closed (strict).\n *\n * @param refs - Array of typed refs for the union variants\n * @param closed - Whether to reject unknown $type values\n * @returns A new {@link TypedUnionSchema} instance\n *\n * @example\n * ```ts\n * // Closed union - only accepts known types\n * const embedSchema = l.typedUnion([\n * l.typedRef(() => imageViewSchema),\n * l.typedRef(() => videoViewSchema),\n * l.typedRef(() => externalViewSchema),\n * ], true)\n *\n * // Open union - accepts unknown types for forward compatibility\n * const feedItemSchema = l.typedUnion([\n * l.typedRef(() => postSchema),\n * l.typedRef(() => repostSchema),\n * ], false) // unknown types pass through\n *\n * // Get all known $types\n * console.log(embedSchema.$types)\n * // ['app.bsky.embed.images#view', 'app.bsky.embed.video#view', ...]\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function typedUnion<\n const R extends readonly TypedRefSchema[],\n const C extends boolean,\n>(refs: R, closed: C) {\n return new TypedUnionSchema<R, C>(refs, closed)\n}\n"]}
|
package/dist/schema/union.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UnionSchema = void 0;
|
|
4
|
-
exports.union = union;
|
|
5
|
-
const core_js_1 = require("../core.js");
|
|
1
|
+
import { LexValidationError, Schema, } from '../core.js';
|
|
6
2
|
/**
|
|
7
3
|
* Schema for validating values that match one of several possible schemas.
|
|
8
4
|
*
|
|
@@ -19,12 +15,11 @@ const core_js_1 = require("../core.js");
|
|
|
19
15
|
* schema.validate(true) // fails
|
|
20
16
|
* ```
|
|
21
17
|
*/
|
|
22
|
-
class UnionSchema extends
|
|
23
|
-
validators;
|
|
24
|
-
type = 'union';
|
|
18
|
+
export class UnionSchema extends Schema {
|
|
25
19
|
constructor(validators) {
|
|
26
20
|
super();
|
|
27
21
|
this.validators = validators;
|
|
22
|
+
this.type = 'union';
|
|
28
23
|
}
|
|
29
24
|
validateInContext(input, ctx) {
|
|
30
25
|
const failures = [];
|
|
@@ -34,10 +29,9 @@ class UnionSchema extends core_js_1.Schema {
|
|
|
34
29
|
return result;
|
|
35
30
|
failures.push(result);
|
|
36
31
|
}
|
|
37
|
-
return ctx.failure(
|
|
32
|
+
return ctx.failure(LexValidationError.fromFailures(failures));
|
|
38
33
|
}
|
|
39
34
|
}
|
|
40
|
-
exports.UnionSchema = UnionSchema;
|
|
41
35
|
/**
|
|
42
36
|
* Creates a union schema that accepts values matching any of the provided schemas.
|
|
43
37
|
*
|
|
@@ -63,7 +57,7 @@ exports.UnionSchema = UnionSchema;
|
|
|
63
57
|
* ```
|
|
64
58
|
*/
|
|
65
59
|
/*@__NO_SIDE_EFFECTS__*/
|
|
66
|
-
function union(validators) {
|
|
60
|
+
export function union(validators) {
|
|
67
61
|
return new UnionSchema(validators);
|
|
68
62
|
}
|
|
69
63
|
//# sourceMappingURL=union.js.map
|
package/dist/schema/union.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../src/schema/union.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../src/schema/union.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,kBAAkB,EAClB,MAAM,GAIP,MAAM,YAAY,CAAA;AASnB;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,WAEX,SAAQ,MAGT;IAGC,YAA+B,UAAuB;QACpD,KAAK,EAAE,CAAA;QADsB,eAAU,GAAV,UAAU,CAAa;QAF7C,SAAI,GAAG,OAAgB,CAAA;IAIhC,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,MAAM,QAAQ,GAAwB,EAAE,CAAA;QAExC,KAAK,MAAM,SAAS,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACxC,MAAM,MAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;YAC7C,IAAI,MAAM,CAAC,OAAO;gBAAE,OAAO,MAAM,CAAA;YAEjC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACvB,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,kBAAkB,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAA;IAC/D,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAwB;AACxB,MAAM,UAAU,KAAK,CACnB,UAAuB;IAEvB,OAAO,IAAI,WAAW,CAAc,UAAU,CAAC,CAAA;AACjD,CAAC","sourcesContent":["import {\n InferInput,\n InferOutput,\n LexValidationError,\n Schema,\n ValidationContext,\n ValidationFailure,\n Validator,\n} from '../core.js'\n\n/**\n * Type representing a non-empty tuple of validators for union schemas.\n *\n * Requires at least one validator in the tuple.\n */\nexport type UnionSchemaValidators = readonly [Validator, ...Validator[]]\n\n/**\n * Schema for validating values that match one of several possible schemas.\n *\n * Tries each validator in order until one succeeds. If all validators fail,\n * returns a combined error from all attempts.\n *\n * @template TValidators - Tuple type of the validators in the union\n *\n * @example\n * ```ts\n * const schema = new UnionSchema([l.string(), l.integer()])\n * schema.validate('hello') // success\n * schema.validate(42) // success\n * schema.validate(true) // fails\n * ```\n */\nexport class UnionSchema<\n const TValidators extends UnionSchemaValidators = any,\n> extends Schema<\n InferInput<TValidators[number]>,\n InferOutput<TValidators[number]>\n> {\n readonly type = 'union' as const\n\n constructor(protected readonly validators: TValidators) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n const failures: ValidationFailure[] = []\n\n for (const validator of this.validators) {\n const result = ctx.validate(input, validator)\n if (result.success) return result\n\n failures.push(result)\n }\n\n return ctx.failure(LexValidationError.fromFailures(failures))\n }\n}\n\n/**\n * Creates a union schema that accepts values matching any of the provided schemas.\n *\n * Validators are tried in order. Use `discriminatedUnion()` for better\n * performance when discriminating on a known property.\n *\n * @param validators - Non-empty array of validators to try\n * @returns A new {@link UnionSchema} instance\n *\n * @example\n * ```ts\n * // String or number\n * const stringOrNumber = l.union([l.string(), l.integer()])\n *\n * // Nullable value\n * const nullableString = l.union([l.string(), l.null()])\n *\n * // Multiple object types\n * const mediaSchema = l.union([\n * l.object({ type: l.literal('image'), url: l.string() }),\n * l.object({ type: l.literal('video'), url: l.string(), duration: l.integer() }),\n * ])\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function union<const TValidators extends UnionSchemaValidators>(\n validators: TValidators,\n) {\n return new UnionSchema<TValidators>(validators)\n}\n"]}
|
package/dist/schema/unknown.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.unknown = exports.UnknownSchema = void 0;
|
|
4
|
-
const core_js_1 = require("../core.js");
|
|
5
|
-
const memoize_js_1 = require("../util/memoize.js");
|
|
1
|
+
import { Schema } from '../core.js';
|
|
2
|
+
import { memoizedOptions } from '../util/memoize.js';
|
|
6
3
|
/**
|
|
7
4
|
* Schema that accepts any value without validation.
|
|
8
5
|
*
|
|
@@ -16,13 +13,15 @@ const memoize_js_1 = require("../util/memoize.js");
|
|
|
16
13
|
* schema.validate(anything) // always succeeds
|
|
17
14
|
* ```
|
|
18
15
|
*/
|
|
19
|
-
class UnknownSchema extends
|
|
20
|
-
|
|
16
|
+
export class UnknownSchema extends Schema {
|
|
17
|
+
constructor() {
|
|
18
|
+
super(...arguments);
|
|
19
|
+
this.type = 'unknown';
|
|
20
|
+
}
|
|
21
21
|
validateInContext(input, ctx) {
|
|
22
22
|
return ctx.success(input);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
exports.UnknownSchema = UnknownSchema;
|
|
26
25
|
/**
|
|
27
26
|
* Creates an unknown schema that accepts any value.
|
|
28
27
|
*
|
|
@@ -43,7 +42,7 @@ exports.UnknownSchema = UnknownSchema;
|
|
|
43
42
|
* })
|
|
44
43
|
* ```
|
|
45
44
|
*/
|
|
46
|
-
|
|
45
|
+
export const unknown = /*#__PURE__*/ memoizedOptions(function () {
|
|
47
46
|
return new UnknownSchema();
|
|
48
47
|
});
|
|
49
48
|
//# sourceMappingURL=unknown.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unknown.js","sourceRoot":"","sources":["../../src/schema/unknown.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"unknown.js","sourceRoot":"","sources":["../../src/schema/unknown.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAqB,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAEpD;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,aAAc,SAAQ,MAAe;IAAlD;;QACW,SAAI,GAAG,SAAkB,CAAA;IAKpC,CAAC;IAHC,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC3B,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,aAAa,CAAC,eAAe,CAAC;IACnD,OAAO,IAAI,aAAa,EAAE,CAAA;AAC5B,CAAC,CAAC,CAAA","sourcesContent":["import { Schema, ValidationContext } from '../core.js'\nimport { memoizedOptions } from '../util/memoize.js'\n\n/**\n * Schema that accepts any value without validation.\n *\n * Passes through any input unchanged. Use sparingly as it bypasses\n * type safety. Useful for dynamic data or when the schema is not\n * known at compile time.\n *\n * @example\n * ```ts\n * const schema = new UnknownSchema()\n * schema.validate(anything) // always succeeds\n * ```\n */\nexport class UnknownSchema extends Schema<unknown> {\n readonly type = 'unknown' as const\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n return ctx.success(input)\n }\n}\n\n/**\n * Creates an unknown schema that accepts any value.\n *\n * The value passes through without any validation or transformation.\n * Use this when you need to accept arbitrary data.\n *\n * @returns A new {@link UnknownSchema} instance\n *\n * @example\n * ```ts\n * // Accept any value\n * const anyDataSchema = l.unknown()\n *\n * // In an object with a dynamic field\n * const flexibleSchema = l.object({\n * type: l.string(),\n * data: l.unknown(),\n * })\n * ```\n */\nexport const unknown = /*#__PURE__*/ memoizedOptions(function () {\n return new UnknownSchema()\n})\n"]}
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WithDefaultSchema = void 0;
|
|
4
|
-
exports.withDefault = withDefault;
|
|
5
|
-
const core_js_1 = require("../core.js");
|
|
1
|
+
import { Schema, } from '../core.js';
|
|
6
2
|
/**
|
|
7
3
|
* Schema wrapper that provides a default value when the input is undefined.
|
|
8
4
|
*
|
|
@@ -19,14 +15,12 @@ const core_js_1 = require("../core.js");
|
|
|
19
15
|
* schema.parse(42) // 42
|
|
20
16
|
* ```
|
|
21
17
|
*/
|
|
22
|
-
class WithDefaultSchema extends
|
|
23
|
-
validator;
|
|
24
|
-
defaultValue;
|
|
25
|
-
type = 'withDefault';
|
|
18
|
+
export class WithDefaultSchema extends Schema {
|
|
26
19
|
constructor(validator, defaultValue) {
|
|
27
20
|
super();
|
|
28
21
|
this.validator = validator;
|
|
29
22
|
this.defaultValue = defaultValue;
|
|
23
|
+
this.type = 'withDefault';
|
|
30
24
|
}
|
|
31
25
|
validateInContext(input, ctx) {
|
|
32
26
|
// When in a validation context, the output should not be altered,
|
|
@@ -37,7 +31,6 @@ class WithDefaultSchema extends core_js_1.Schema {
|
|
|
37
31
|
return ctx.validate(input, this.validator);
|
|
38
32
|
}
|
|
39
33
|
}
|
|
40
|
-
exports.WithDefaultSchema = WithDefaultSchema;
|
|
41
34
|
/**
|
|
42
35
|
* Creates a schema that applies a default value when the input is undefined.
|
|
43
36
|
*
|
|
@@ -66,7 +59,7 @@ exports.WithDefaultSchema = WithDefaultSchema;
|
|
|
66
59
|
* const enabledSchema = l.withDefault(l.boolean(), false)
|
|
67
60
|
* ```
|
|
68
61
|
*/
|
|
69
|
-
function withDefault(validator, defaultValue) {
|
|
62
|
+
export function withDefault(validator, defaultValue) {
|
|
70
63
|
return new WithDefaultSchema(validator, defaultValue);
|
|
71
64
|
}
|
|
72
65
|
//# sourceMappingURL=with-default.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-default.js","sourceRoot":"","sources":["../../src/schema/with-default.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"with-default.js","sourceRoot":"","sources":["../../src/schema/with-default.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,MAAM,GAGP,MAAM,YAAY,CAAA;AAEnB;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,iBAEX,SAAQ,MAAuD;IAG/D,YACW,SAAqB,EACrB,YAAoC;QAE7C,KAAK,EAAE,CAAA;QAHE,cAAS,GAAT,SAAS,CAAY;QACrB,iBAAY,GAAZ,YAAY,CAAwB;QAJtC,SAAI,GAAG,aAAsB,CAAA;IAOtC,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,kEAAkE;QAClE,iCAAiC;QACjC,IAAI,KAAK,KAAK,SAAS,IAAI,GAAG,CAAC,OAAO,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;YAC3D,OAAO,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;QACxD,CAAC;QAED,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;IAC5C,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,WAAW,CACzB,SAAqB,EACrB,YAAoC;IAEpC,OAAO,IAAI,iBAAiB,CAAa,SAAS,EAAE,YAAY,CAAC,CAAA;AACnE,CAAC","sourcesContent":["import {\n InferInput,\n InferOutput,\n Schema,\n ValidationContext,\n Validator,\n} from '../core.js'\n\n/**\n * Schema wrapper that provides a default value when the input is undefined.\n *\n * In parse mode, when the input is `undefined`, the default value is used\n * instead. In validate mode, undefined values pass through unchanged (the\n * default is not applied).\n *\n * @template TValidator - The wrapped validator type\n *\n * @example\n * ```ts\n * const schema = new WithDefaultSchema(l.integer(), 0)\n * schema.parse(undefined) // 0\n * schema.parse(42) // 42\n * ```\n */\nexport class WithDefaultSchema<\n const TValidator extends Validator,\n> extends Schema<InferInput<TValidator>, InferOutput<TValidator>> {\n readonly type = 'withDefault' as const\n\n constructor(\n readonly validator: TValidator,\n readonly defaultValue: InferInput<TValidator>,\n ) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n // When in a validation context, the output should not be altered,\n // so we don't apply the default.\n if (input === undefined && ctx.options.mode !== 'validate') {\n return ctx.validate(this.defaultValue, this.validator)\n }\n\n return ctx.validate(input, this.validator)\n }\n}\n\n/**\n * Creates a schema that applies a default value when the input is undefined.\n *\n * Commonly used with `optional()` to provide fallback values for missing\n * properties. The default value is validated against the schema.\n *\n * @param validator - The validator for the value\n * @param defaultValue - The default value to use when input is undefined\n * @returns A new {@link WithDefaultSchema} instance\n *\n * @example\n * ```ts\n * // Integer with default\n * const countSchema = l.withDefault(l.integer(), 0)\n * countSchema.parse(undefined) // 0\n * countSchema.parse(5) // 5\n *\n * // Commonly combined with optional in objects\n * const settingsSchema = l.object({\n * theme: l.optional(l.withDefault(l.string(), 'light')),\n * pageSize: l.optional(l.withDefault(l.integer(), 25)),\n * })\n * settingsSchema.parse({}) // { theme: 'light', pageSize: 25 }\n *\n * // Boolean with default\n * const enabledSchema = l.withDefault(l.boolean(), false)\n * ```\n */\nexport function withDefault<const TValidator extends Validator>(\n validator: TValidator,\n defaultValue: InferInput<TValidator>,\n) {\n return new WithDefaultSchema<TValidator>(validator, defaultValue)\n}\n"]}
|
package/dist/schema.js
CHANGED
|
@@ -1,45 +1,42 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
1
|
// Concrete Types
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
2
|
+
export * from './schema/array.js';
|
|
3
|
+
export * from './schema/blob.js';
|
|
4
|
+
export * from './schema/boolean.js';
|
|
5
|
+
export * from './schema/bytes.js';
|
|
6
|
+
export * from './schema/cid.js';
|
|
7
|
+
export * from './schema/dict.js';
|
|
8
|
+
export * from './schema/enum.js';
|
|
9
|
+
export * from './schema/integer.js';
|
|
10
|
+
export * from './schema/lex-map.js';
|
|
11
|
+
export * from './schema/lex-value.js';
|
|
12
|
+
export * from './schema/literal.js';
|
|
13
|
+
export * from './schema/never.js';
|
|
14
|
+
export * from './schema/null.js';
|
|
15
|
+
export * from './schema/object.js';
|
|
16
|
+
export * from './schema/regexp.js';
|
|
17
|
+
export * from './schema/string.js';
|
|
18
|
+
export * from './schema/unknown.js';
|
|
22
19
|
// Composite Types
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
export * from './schema/custom.js';
|
|
21
|
+
export * from './schema/discriminated-union.js';
|
|
22
|
+
export * from './schema/intersection.js';
|
|
23
|
+
export * from './schema/nullable.js';
|
|
24
|
+
export * from './schema/optional.js';
|
|
25
|
+
export * from './schema/ref.js';
|
|
26
|
+
export * from './schema/refine.js';
|
|
27
|
+
export * from './schema/union.js';
|
|
28
|
+
export * from './schema/with-default.js';
|
|
32
29
|
// Lexicon specific Types
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
30
|
+
export * from './schema/params.js';
|
|
31
|
+
export * from './schema/payload.js';
|
|
32
|
+
export * from './schema/permission-set.js';
|
|
33
|
+
export * from './schema/permission.js';
|
|
34
|
+
export * from './schema/procedure.js';
|
|
35
|
+
export * from './schema/query.js';
|
|
36
|
+
export * from './schema/record.js';
|
|
37
|
+
export * from './schema/subscription.js';
|
|
38
|
+
export * from './schema/token.js';
|
|
39
|
+
export * from './schema/typed-object.js';
|
|
40
|
+
export * from './schema/typed-ref.js';
|
|
41
|
+
export * from './schema/typed-union.js';
|
|
45
42
|
//# sourceMappingURL=schema.js.map
|
package/dist/schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,iBAAiB;AACjB,cAAc,mBAAmB,CAAA;AACjC,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,qBAAqB,CAAA;AACnC,cAAc,uBAAuB,CAAA;AACrC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,kBAAkB,CAAA;AAChC,cAAc,oBAAoB,CAAA;AAClC,cAAc,oBAAoB,CAAA;AAClC,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AAEnC,kBAAkB;AAClB,cAAc,oBAAoB,CAAA;AAClC,cAAc,iCAAiC,CAAA;AAC/C,cAAc,0BAA0B,CAAA;AACxC,cAAc,sBAAsB,CAAA;AACpC,cAAc,sBAAsB,CAAA;AACpC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,oBAAoB,CAAA;AAClC,cAAc,mBAAmB,CAAA;AACjC,cAAc,0BAA0B,CAAA;AAExC,yBAAyB;AACzB,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AACnC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,wBAAwB,CAAA;AACtC,cAAc,uBAAuB,CAAA;AACrC,cAAc,mBAAmB,CAAA;AACjC,cAAc,oBAAoB,CAAA;AAClC,cAAc,0BAA0B,CAAA;AACxC,cAAc,mBAAmB,CAAA;AACjC,cAAc,0BAA0B,CAAA;AACxC,cAAc,uBAAuB,CAAA;AACrC,cAAc,yBAAyB,CAAA","sourcesContent":["// Concrete Types\nexport * from './schema/array.js'\nexport * from './schema/blob.js'\nexport * from './schema/boolean.js'\nexport * from './schema/bytes.js'\nexport * from './schema/cid.js'\nexport * from './schema/dict.js'\nexport * from './schema/enum.js'\nexport * from './schema/integer.js'\nexport * from './schema/lex-map.js'\nexport * from './schema/lex-value.js'\nexport * from './schema/literal.js'\nexport * from './schema/never.js'\nexport * from './schema/null.js'\nexport * from './schema/object.js'\nexport * from './schema/regexp.js'\nexport * from './schema/string.js'\nexport * from './schema/unknown.js'\n\n// Composite Types\nexport * from './schema/custom.js'\nexport * from './schema/discriminated-union.js'\nexport * from './schema/intersection.js'\nexport * from './schema/nullable.js'\nexport * from './schema/optional.js'\nexport * from './schema/ref.js'\nexport * from './schema/refine.js'\nexport * from './schema/union.js'\nexport * from './schema/with-default.js'\n\n// Lexicon specific Types\nexport * from './schema/params.js'\nexport * from './schema/payload.js'\nexport * from './schema/permission-set.js'\nexport * from './schema/permission.js'\nexport * from './schema/procedure.js'\nexport * from './schema/query.js'\nexport * from './schema/record.js'\nexport * from './schema/subscription.js'\nexport * from './schema/token.js'\nexport * from './schema/typed-object.js'\nexport * from './schema/typed-ref.js'\nexport * from './schema/typed-union.js'\n"]}
|
package/dist/util/array-agg.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.arrayAgg = arrayAgg;
|
|
4
1
|
/**
|
|
5
2
|
* Aggregates items in an array based on a comparison function and an aggregation function.
|
|
6
3
|
*
|
|
@@ -20,7 +17,7 @@ exports.arrayAgg = arrayAgg;
|
|
|
20
17
|
* ```
|
|
21
18
|
*/
|
|
22
19
|
/*@__NO_SIDE_EFFECTS__*/
|
|
23
|
-
function arrayAgg(arr, cmp, agg) {
|
|
20
|
+
export function arrayAgg(arr, cmp, agg) {
|
|
24
21
|
if (arr.length === 0)
|
|
25
22
|
return [];
|
|
26
23
|
const groups = [[arr[0]]];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array-agg.js","sourceRoot":"","sources":["../../src/util/array-agg.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"array-agg.js","sourceRoot":"","sources":["../../src/util/array-agg.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAwB;AACxB,MAAM,UAAU,QAAQ,CACtB,GAAiB,EACjB,GAA4B,EAC5B,GAA8B;IAE9B,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAA;IAE/B,MAAM,MAAM,GAAkB,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACxC,MAAM,OAAO,GAAG,KAAK,CAAsB,GAAG,CAAC,MAAM,CAAC,CAAA;IAEtD,KAAK,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3C,IAAI,OAAO,CAAC,CAAC,CAAC;YAAE,SAAQ;QACxB,MAAM,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC,CAAA;QACnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACvC,IAAI,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC5B,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;gBACpB,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAA;gBACjB,SAAS,KAAK,CAAA;YAChB,CAAC;QACH,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;IACrB,CAAC;IAED,OAAO,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;AACxB,CAAC","sourcesContent":["/**\n * Aggregates items in an array based on a comparison function and an aggregation function.\n *\n * @param arr - The input array to aggregate.\n * @param cmp - A comparison function that determines if two items belong to the same group.\n * @param agg - An aggregation function that combines items in a group into a single item.\n * @returns An array of aggregated items.\n * @example\n * ```ts\n * const input = [1, 1, 2, 2, 3, 3, 3]\n * const result = arrayAgg(\n * input,\n * (a, b) => a === b,\n * (items) => { value: items[0], sum: items.reduce((sum, item) => sum + item, 0) },\n * )\n * // result is [{ value: 1, sum: 2 }, { value: 2, sum: 4 }, { value: 3, sum: 6 }]\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function arrayAgg<T, O>(\n arr: readonly T[],\n cmp: (a: T, b: T) => boolean,\n agg: (items: [T, ...T[]]) => O,\n): O[] {\n if (arr.length === 0) return []\n\n const groups: [T, ...T[]][] = [[arr[0]]]\n const skipped = Array<undefined | boolean>(arr.length)\n\n outer: for (let i = 1; i < arr.length; i++) {\n if (skipped[i]) continue\n const item = arr[i]\n for (let j = 0; j < groups.length; j++) {\n if (cmp(item, groups[j][0])) {\n groups[j].push(item)\n skipped[i] = true\n continue outer\n }\n }\n groups.push([item])\n }\n\n return groups.map(agg)\n}\n"]}
|
package/dist/util/if-any.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.lazyProperty = lazyProperty;
|
|
4
1
|
/*@__NO_SIDE_EFFECTS__*/
|
|
5
|
-
function lazyProperty(obj, key, value) {
|
|
2
|
+
export function lazyProperty(obj, key, value) {
|
|
6
3
|
Object.defineProperty(obj, key, {
|
|
7
4
|
value,
|
|
8
5
|
writable: false,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lazy-property.js","sourceRoot":"","sources":["../../src/util/lazy-property.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"lazy-property.js","sourceRoot":"","sources":["../../src/util/lazy-property.ts"],"names":[],"mappings":"AAAA,wBAAwB;AACxB,MAAM,UAAU,YAAY,CAI1B,GAAM,EAAE,GAAM,EAAE,KAAQ;IACxB,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,GAAG,EAAE;QAC9B,KAAK;QACL,QAAQ,EAAE,KAAK;QACf,UAAU,EAAE,KAAK;QACjB,YAAY,EAAE,IAAI;KACnB,CAAC,CAAA;IACF,OAAO,KAAK,CAAA;AACd,CAAC","sourcesContent":["/*@__NO_SIDE_EFFECTS__*/\nexport function lazyProperty<\n O extends object,\n const K extends keyof O,\n const V extends O[K],\n>(obj: O, key: K, value: V): V {\n Object.defineProperty(obj, key, {\n value,\n writable: false,\n enumerable: false,\n configurable: true,\n })\n return value\n}\n"]}
|