@atproto/lex-schema 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/dist/core/$type.d.ts +149 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +44 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +44 -0
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js +30 -0
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/result.d.ts +85 -4
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +60 -4
- package/dist/core/result.js.map +1 -1
- package/dist/core/schema.d.ts +229 -2
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +197 -4
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +244 -11
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +150 -0
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.d.ts +90 -3
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/validation-error.d.ts +61 -1
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +60 -0
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.d.ts +61 -0
- package/dist/core/validation-issue.d.ts.map +1 -1
- package/dist/core/validation-issue.js +51 -0
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +347 -10
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +184 -3
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +13 -25
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +2 -2
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +45 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +14 -0
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +46 -0
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +39 -0
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +28 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +28 -0
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +38 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +32 -0
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +38 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +33 -0
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +66 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +54 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +44 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +44 -0
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +58 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +45 -0
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +48 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +48 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +42 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +36 -0
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +54 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +49 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +44 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +44 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +42 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +42 -0
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +29 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +29 -0
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +41 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +41 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +56 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +51 -0
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +42 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +42 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +89 -7
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +84 -10
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +111 -15
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +70 -0
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +58 -0
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +50 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +42 -0
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +39 -0
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +64 -0
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +64 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +55 -0
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +55 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +63 -8
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +20 -0
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +50 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +17 -0
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +58 -9
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +44 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +44 -0
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +50 -0
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +41 -0
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +72 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +59 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +47 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +47 -0
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +62 -8
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +18 -0
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +53 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +15 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +50 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +50 -1
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +45 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +40 -0
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +34 -0
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +31 -0
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +33 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +33 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +44 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +44 -0
- package/dist/schema/with-default.js.map +1 -1
- package/package.json +4 -4
- package/src/core/$type.ts +150 -18
- package/src/core/record-key.ts +44 -0
- package/src/core/result.ts +86 -4
- package/src/core/schema.ts +236 -7
- package/src/core/string-format.ts +259 -13
- package/src/core/types.ts +91 -3
- package/src/core/validation-error.ts +60 -0
- package/src/core/validation-issue.ts +65 -0
- package/src/core/validator.ts +351 -10
- package/src/helpers.test.ts +110 -29
- package/src/helpers.ts +14 -14
- package/src/schema/array.test.ts +94 -79
- package/src/schema/array.ts +45 -0
- package/src/schema/blob.ts +46 -0
- package/src/schema/boolean.ts +28 -0
- package/src/schema/bytes.ts +38 -0
- package/src/schema/cid.ts +38 -0
- package/src/schema/custom.ts +66 -1
- package/src/schema/dict.ts +44 -0
- package/src/schema/discriminated-union.ts +58 -0
- package/src/schema/enum.ts +48 -0
- package/src/schema/integer.ts +42 -0
- package/src/schema/intersection.ts +54 -0
- package/src/schema/literal.ts +44 -0
- package/src/schema/never.ts +42 -0
- package/src/schema/null.ts +29 -0
- package/src/schema/nullable.ts +41 -0
- package/src/schema/object.ts +56 -0
- package/src/schema/optional.ts +42 -0
- package/src/schema/params.test.ts +58 -2
- package/src/schema/params.ts +124 -16
- package/src/schema/payload.test.ts +3 -3
- package/src/schema/payload.ts +142 -38
- package/src/schema/permission-set.ts +58 -0
- package/src/schema/permission.ts +42 -0
- package/src/schema/procedure.ts +64 -0
- package/src/schema/query.ts +55 -0
- package/src/schema/record.ts +63 -8
- package/src/schema/ref.ts +50 -0
- package/src/schema/refine.ts +58 -9
- package/src/schema/regexp.ts +44 -0
- package/src/schema/string.ts +50 -0
- package/src/schema/subscription.ts +72 -2
- package/src/schema/token.ts +47 -0
- package/src/schema/typed-object.ts +62 -8
- package/src/schema/typed-ref.ts +53 -0
- package/src/schema/typed-union.ts +55 -2
- package/src/schema/union.ts +45 -0
- package/src/schema/unknown-object.ts +34 -0
- package/src/schema/unknown.ts +33 -0
- package/src/schema/with-default.ts +44 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refine.js","sourceRoot":"","sources":["../../src/schema/refine.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"refine.js","sourceRoot":"","sources":["../../src/schema/refine.ts"],"names":[],"mappings":";;AAmHA,wBAiBC;AApID,wCAOmB;AA2GnB,wBAAwB;AACxB,SAAgB,MAAM,CACpB,MAAkB,EAClB,UAA+B;IAE/B,wEAAwE;IACxE,4EAA4E;IAC5E,4EAA4E;IAC5E,6BAA6B;IAC7B,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE;QAC3B,iBAAiB,EAAE;YACjB,8DAA8D;YAC9D,KAAK,EAAE,wBAAwB,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;YAC5D,UAAU,EAAE,KAAK;YACjB,QAAQ,EAAE,KAAK;YACf,YAAY,EAAE,IAAI;SACnB;KACF,CAAC,CAAA;AACJ,CAAC;AAED,wBAAwB;AACxB,SAAS,wBAAwB,CAK/B,KAAc,EACd,GAAsB;IAEtB,MAAM,MAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;IAC/C,IAAI,CAAC,MAAM,CAAC,OAAO;QAAE,OAAO,MAAM,CAAA;IAElC,MAAM,WAAW,GAAG,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;IACvE,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QACjD,OAAO,GAAG,CAAC,KAAK,CAAC,IAAI,qBAAW,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAA;IACzE,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["import {\n InferInput,\n IssueCustom,\n PropertyKey,\n ValidationContext,\n ValidationResult,\n Validator,\n} from '../core.js'\nimport { CustomAssertionContext } from './custom.js'\n\n/**\n * Configuration for a refinement check that validates a condition.\n *\n * @template T - The type being validated\n * @property check - Function that returns true if the value passes the check\n * @property message - Error message when the check fails\n * @property path - Optional path to associate with the error\n */\nexport type RefinementCheck<T> = {\n check: (value: T, ctx: CustomAssertionContext) => boolean\n message: string\n path?: PropertyKey | readonly PropertyKey[]\n}\n\n/**\n * Configuration for a refinement assertion that narrows the type.\n *\n * @template T - The input type being validated\n * @template Out - The narrowed output type\n * @property check - Type guard function that narrows the type\n * @property message - Error message when the assertion fails\n * @property path - Optional path to associate with the error\n */\nexport type RefinementAssertion<T, Out extends T> = {\n check: (this: null, value: T, ctx: CustomAssertionContext) => value is Out\n message: string\n path?: PropertyKey | readonly PropertyKey[]\n}\n\n/**\n * Infers the input type from a refinement configuration.\n *\n * @template R - The refinement type\n */\nexport type InferRefinement<R> =\n R extends RefinementCheck<infer T>\n ? T\n : R extends RefinementAssertion<infer T, any>\n ? T\n : never\n\n/**\n * Union type of refinement check or assertion.\n *\n * @template T - The input type being validated\n * @template Out - The output type (same as T for checks, narrowed for assertions)\n */\nexport type Refinement<T = any, Out extends T = T> =\n | RefinementCheck<T>\n | RefinementAssertion<T, Out>\n\n/**\n * Creates a refined schema by adding additional validation constraints.\n *\n * Wraps an existing schema with an additional check function. The base schema\n * is validated first, then the refinement check is applied to the result.\n *\n * @param schema - The base schema to refine\n * @param refinement - The refinement check or assertion to apply\n * @returns A new schema that includes the refinement\n *\n * @example\n * ```ts\n * // Simple check refinement\n * const positiveInt = l.refine(l.integer(), {\n * check: (value) => value > 0,\n * message: 'Value must be positive',\n * })\n *\n * positiveInt.parse(5) // 5\n * positiveInt.parse(-1) // throws\n *\n * // Type-narrowing assertion\n * const nonEmptyString = l.refine(l.string(), {\n * check: (value): value is string & { length: number } => value.length > 0,\n * message: 'String must not be empty',\n * })\n *\n * // With custom path for nested errors\n * const validDateRange = l.refine(\n * l.object({ start: l.string(), end: l.string() }),\n * {\n * check: (v) => new Date(v.start) < new Date(v.end),\n * message: 'Start date must be before end date',\n * path: ['end'],\n * }\n * )\n * ```\n */\nexport function refine<\n const TValidator extends Validator,\n TInput extends InferInput<TValidator>,\n>(\n schema: TValidator,\n refinement: RefinementAssertion<InferInput<TValidator>, TInput>,\n): TValidator & Validator<TInput>\nexport function refine<const TValidator extends Validator>(\n schema: TValidator,\n refinement: RefinementCheck<InferInput<TValidator>>,\n): TValidator\nexport function refine<\n TRefinement extends Refinement,\n const TValidator extends Validator<InferRefinement<TRefinement>>,\n>(schema: TValidator, refinement: TRefinement): TValidator\n/*@__NO_SIDE_EFFECTS__*/\nexport function refine<const TValidator extends Validator>(\n schema: TValidator,\n refinement: Refinement<unknown>,\n): TValidator {\n // This is basically the same as monkey patching the \"validateInContext\"\n // method to the schema, but done in a way that does not mutate the original\n // schema. This is safe to do because Validators don't update their internal\n // state over their lifetime.\n return Object.create(schema, {\n validateInContext: {\n // We do not use an arrow function to avoid creating a closure\n value: validateInContextUnbound.bind({ schema, refinement }),\n enumerable: false,\n writable: false,\n configurable: true,\n },\n })\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nfunction validateInContextUnbound<S extends Validator>(\n this: {\n schema: S\n refinement: Refinement<InferInput<S>>\n },\n input: unknown,\n ctx: ValidationContext,\n): ValidationResult<InferInput<S>> {\n const result = ctx.validate(input, this.schema)\n if (!result.success) return result\n\n const checkResult = this.refinement.check.call(null, result.value, ctx)\n if (!checkResult) {\n const path = ctx.concatPath(this.refinement.path)\n return ctx.issue(new IssueCustom(path, input, this.refinement.message))\n }\n\n return result\n}\n"]}
|
package/dist/schema/regexp.d.ts
CHANGED
|
@@ -1,8 +1,52 @@
|
|
|
1
1
|
import { Schema, ValidationContext } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Schema for validating strings against a regular expression pattern.
|
|
4
|
+
*
|
|
5
|
+
* Validates that the input is a string and matches the provided pattern.
|
|
6
|
+
* The pattern is tested using RegExp.test().
|
|
7
|
+
*
|
|
8
|
+
* @template TValue - The string type (can be narrowed with branded types)
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const schema = new RegexpSchema(/^[a-z]+$/)
|
|
13
|
+
* schema.validate('hello') // success
|
|
14
|
+
* schema.validate('Hello') // fails - uppercase not allowed
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
2
17
|
export declare class RegexpSchema<TValue extends string = string> extends Schema<TValue> {
|
|
3
18
|
readonly pattern: RegExp;
|
|
4
19
|
constructor(pattern: RegExp);
|
|
5
20
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<TValue>;
|
|
6
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a regexp schema that validates strings against a pattern.
|
|
24
|
+
*
|
|
25
|
+
* Useful for custom string formats not covered by the built-in format
|
|
26
|
+
* validators.
|
|
27
|
+
*
|
|
28
|
+
* @param pattern - Regular expression pattern to match against
|
|
29
|
+
* @returns A new {@link RegexpSchema} instance
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* // Simple pattern
|
|
34
|
+
* const slugSchema = l.regexp(/^[a-z0-9-]+$/)
|
|
35
|
+
*
|
|
36
|
+
* // With anchors for exact match
|
|
37
|
+
* const uuidSchema = l.regexp(
|
|
38
|
+
* /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
|
39
|
+
* )
|
|
40
|
+
*
|
|
41
|
+
* // Semantic versioning
|
|
42
|
+
* const semverSchema = l.regexp(/^\d+\.\d+\.\d+(-[\w.]+)?(\+[\w.]+)?$/)
|
|
43
|
+
*
|
|
44
|
+
* // Use in object
|
|
45
|
+
* const configSchema = l.object({
|
|
46
|
+
* name: l.regexp(/^[a-z][a-z0-9-]*$/), // kebab-case identifier
|
|
47
|
+
* version: semverSchema,
|
|
48
|
+
* })
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
7
51
|
export declare function regexp<TInput extends string = string>(pattern: RegExp): RegexpSchema<TInput>;
|
|
8
52
|
//# sourceMappingURL=regexp.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regexp.d.ts","sourceRoot":"","sources":["../../src/schema/regexp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEtD,qBAAa,YAAY,CACvB,MAAM,SAAS,MAAM,GAAG,MAAM,CAC9B,SAAQ,MAAM,CAAC,MAAM,CAAC;aACM,OAAO,EAAE,MAAM;gBAAf,OAAO,EAAE,MAAM;IAI3C,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAWzD;
|
|
1
|
+
{"version":3,"file":"regexp.d.ts","sourceRoot":"","sources":["../../src/schema/regexp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEtD;;;;;;;;;;;;;;GAcG;AACH,qBAAa,YAAY,CACvB,MAAM,SAAS,MAAM,GAAG,MAAM,CAC9B,SAAQ,MAAM,CAAC,MAAM,CAAC;aACM,OAAO,EAAE,MAAM;gBAAf,OAAO,EAAE,MAAM;IAI3C,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAWzD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,wBAAgB,MAAM,CAAC,MAAM,SAAS,MAAM,GAAG,MAAM,EAAE,OAAO,EAAE,MAAM,wBAErE"}
|
package/dist/schema/regexp.js
CHANGED
|
@@ -3,6 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RegexpSchema = void 0;
|
|
4
4
|
exports.regexp = regexp;
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema for validating strings against a regular expression pattern.
|
|
8
|
+
*
|
|
9
|
+
* Validates that the input is a string and matches the provided pattern.
|
|
10
|
+
* The pattern is tested using RegExp.test().
|
|
11
|
+
*
|
|
12
|
+
* @template TValue - The string type (can be narrowed with branded types)
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const schema = new RegexpSchema(/^[a-z]+$/)
|
|
17
|
+
* schema.validate('hello') // success
|
|
18
|
+
* schema.validate('Hello') // fails - uppercase not allowed
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
6
21
|
class RegexpSchema extends core_js_1.Schema {
|
|
7
22
|
pattern;
|
|
8
23
|
constructor(pattern) {
|
|
@@ -20,6 +35,35 @@ class RegexpSchema extends core_js_1.Schema {
|
|
|
20
35
|
}
|
|
21
36
|
}
|
|
22
37
|
exports.RegexpSchema = RegexpSchema;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a regexp schema that validates strings against a pattern.
|
|
40
|
+
*
|
|
41
|
+
* Useful for custom string formats not covered by the built-in format
|
|
42
|
+
* validators.
|
|
43
|
+
*
|
|
44
|
+
* @param pattern - Regular expression pattern to match against
|
|
45
|
+
* @returns A new {@link RegexpSchema} instance
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* // Simple pattern
|
|
50
|
+
* const slugSchema = l.regexp(/^[a-z0-9-]+$/)
|
|
51
|
+
*
|
|
52
|
+
* // With anchors for exact match
|
|
53
|
+
* const uuidSchema = l.regexp(
|
|
54
|
+
* /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
|
55
|
+
* )
|
|
56
|
+
*
|
|
57
|
+
* // Semantic versioning
|
|
58
|
+
* const semverSchema = l.regexp(/^\d+\.\d+\.\d+(-[\w.]+)?(\+[\w.]+)?$/)
|
|
59
|
+
*
|
|
60
|
+
* // Use in object
|
|
61
|
+
* const configSchema = l.object({
|
|
62
|
+
* name: l.regexp(/^[a-z][a-z0-9-]*$/), // kebab-case identifier
|
|
63
|
+
* version: semverSchema,
|
|
64
|
+
* })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
23
67
|
/*@__NO_SIDE_EFFECTS__*/
|
|
24
68
|
function regexp(pattern) {
|
|
25
69
|
return new RegexpSchema(pattern);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regexp.js","sourceRoot":"","sources":["../../src/schema/regexp.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"regexp.js","sourceRoot":"","sources":["../../src/schema/regexp.ts"],"names":[],"mappings":";;;AAmEA,wBAEC;AArED,wCAAsD;AAEtD;;;;;;;;;;;;;;GAcG;AACH,MAAa,YAEX,SAAQ,gBAAc;IACM;IAA5B,YAA4B,OAAe;QACzC,KAAK,EAAE,CAAA;QADmB,YAAO,GAAP,OAAO,CAAQ;IAE3C,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QAC9C,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAC9B,OAAO,GAAG,CAAC,kBAAkB,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAA;QAC/D,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,KAAe,CAAC,CAAA;IACrC,CAAC;CACF;AAlBD,oCAkBC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAwB;AACxB,SAAgB,MAAM,CAAiC,OAAe;IACpE,OAAO,IAAI,YAAY,CAAS,OAAO,CAAC,CAAA;AAC1C,CAAC","sourcesContent":["import { Schema, ValidationContext } from '../core.js'\n\n/**\n * Schema for validating strings against a regular expression pattern.\n *\n * Validates that the input is a string and matches the provided pattern.\n * The pattern is tested using RegExp.test().\n *\n * @template TValue - The string type (can be narrowed with branded types)\n *\n * @example\n * ```ts\n * const schema = new RegexpSchema(/^[a-z]+$/)\n * schema.validate('hello') // success\n * schema.validate('Hello') // fails - uppercase not allowed\n * ```\n */\nexport class RegexpSchema<\n TValue extends string = string,\n> extends Schema<TValue> {\n constructor(public readonly pattern: RegExp) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (typeof input !== 'string') {\n return ctx.issueInvalidType(input, 'string')\n }\n\n if (!this.pattern.test(input)) {\n return ctx.issueInvalidFormat(input, this.pattern.toString())\n }\n\n return ctx.success(input as TValue)\n }\n}\n\n/**\n * Creates a regexp schema that validates strings against a pattern.\n *\n * Useful for custom string formats not covered by the built-in format\n * validators.\n *\n * @param pattern - Regular expression pattern to match against\n * @returns A new {@link RegexpSchema} instance\n *\n * @example\n * ```ts\n * // Simple pattern\n * const slugSchema = l.regexp(/^[a-z0-9-]+$/)\n *\n * // With anchors for exact match\n * const uuidSchema = l.regexp(\n * /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i\n * )\n *\n * // Semantic versioning\n * const semverSchema = l.regexp(/^\\d+\\.\\d+\\.\\d+(-[\\w.]+)?(\\+[\\w.]+)?$/)\n *\n * // Use in object\n * const configSchema = l.object({\n * name: l.regexp(/^[a-z][a-z0-9-]*$/), // kebab-case identifier\n * version: semverSchema,\n * })\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function regexp<TInput extends string = string>(pattern: RegExp) {\n return new RegexpSchema<TInput>(pattern)\n}\n"]}
|
package/dist/schema/string.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { InferStringFormat, Schema, StringFormat, ValidationContext } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Configuration options for string schema validation.
|
|
4
|
+
*
|
|
5
|
+
* @property format - Expected string format (e.g., 'datetime', 'uri', 'at-uri', 'did', 'handle', 'nsid', 'cid', 'tid', 'record-key', 'at-identifier', 'language')
|
|
6
|
+
* @property minLength - Minimum length in UTF-8 bytes
|
|
7
|
+
* @property maxLength - Maximum length in UTF-8 bytes
|
|
8
|
+
* @property minGraphemes - Minimum number of grapheme clusters
|
|
9
|
+
* @property maxGraphemes - Maximum number of grapheme clusters
|
|
10
|
+
*/
|
|
2
11
|
export type StringSchemaOptions = {
|
|
3
12
|
format?: StringFormat;
|
|
4
13
|
minLength?: number;
|
|
@@ -6,6 +15,20 @@ export type StringSchemaOptions = {
|
|
|
6
15
|
minGraphemes?: number;
|
|
7
16
|
maxGraphemes?: number;
|
|
8
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* Schema for validating string values with optional format and length constraints.
|
|
20
|
+
*
|
|
21
|
+
* Supports various string formats defined in the Lexicon specification, as well as
|
|
22
|
+
* length constraints measured in UTF-8 bytes or grapheme clusters.
|
|
23
|
+
*
|
|
24
|
+
* @template TOptions - The configuration options type
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const schema = new StringSchema({ format: 'datetime', maxLength: 64 })
|
|
29
|
+
* const result = schema.validate('2024-01-15T10:30:00Z')
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
9
32
|
export declare class StringSchema<const TOptions extends StringSchemaOptions = StringSchemaOptions> extends Schema<TOptions extends {
|
|
10
33
|
format: infer F extends StringFormat;
|
|
11
34
|
} ? InferStringFormat<F> : string> {
|
|
@@ -14,5 +37,32 @@ export declare class StringSchema<const TOptions extends StringSchemaOptions = S
|
|
|
14
37
|
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<string>;
|
|
15
38
|
}
|
|
16
39
|
export declare function coerceToString(input: unknown): string | null;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a string schema with optional format and length constraints.
|
|
42
|
+
*
|
|
43
|
+
* Strings can be validated against various formats (datetime, uri, did, handle, etc.)
|
|
44
|
+
* and constrained by length in UTF-8 bytes or grapheme clusters.
|
|
45
|
+
*
|
|
46
|
+
* @param options - Optional configuration for format and length constraints
|
|
47
|
+
* @returns A new {@link StringSchema} instance
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* // Basic string
|
|
52
|
+
* const nameSchema = l.string()
|
|
53
|
+
*
|
|
54
|
+
* // With format validation
|
|
55
|
+
* const dateSchema = l.string({ format: 'datetime' })
|
|
56
|
+
*
|
|
57
|
+
* // With length constraints (UTF-8 bytes)
|
|
58
|
+
* const bioSchema = l.string({ maxLength: 256 })
|
|
59
|
+
*
|
|
60
|
+
* // With grapheme constraints (user-perceived characters)
|
|
61
|
+
* const displayNameSchema = l.string({ maxGraphemes: 64 })
|
|
62
|
+
*
|
|
63
|
+
* // Combining constraints
|
|
64
|
+
* const handleSchema = l.string({ format: 'handle', minLength: 3, maxLength: 253 })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
17
67
|
export declare const string: <const O extends StringSchemaOptions = {}>(options?: StringSchemaOptions & O) => StringSchema<O>;
|
|
18
68
|
//# sourceMappingURL=string.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../src/schema/string.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EACjB,MAAM,EACN,YAAY,EACZ,iBAAiB,EAElB,MAAM,YAAY,CAAA;AAInB,MAAM,MAAM,mBAAmB,GAAG;IAChC,MAAM,CAAC,EAAE,YAAY,CAAA;IACrB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB,CAAA;AAED,qBAAa,YAAY,CACvB,KAAK,CAAC,QAAQ,SAAS,mBAAmB,GAAG,mBAAmB,CAChE,SAAQ,MAAM,CACd,QAAQ,SAAS;IAAE,MAAM,EAAE,MAAM,CAAC,SAAS,YAAY,CAAA;CAAE,GACrD,iBAAiB,CAAC,CAAC,CAAC,GACpB,MAAM,CACX;IACa,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ;gBAAlB,OAAO,CAAC,EAAE,QAAQ,YAAA;IAIvC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAqDzD;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAsC5D;AAED,eAAO,MAAM,MAAM,SACX,CAAC,SAAS,mBAAmB,iBACzB,mBAAmB,GAAG,CAAC,oBAEjC,CAAA"}
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../src/schema/string.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EACjB,MAAM,EACN,YAAY,EACZ,iBAAiB,EAElB,MAAM,YAAY,CAAA;AAInB;;;;;;;;GAQG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,MAAM,CAAC,EAAE,YAAY,CAAA;IACrB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB,CAAA;AAED;;;;;;;;;;;;;GAaG;AACH,qBAAa,YAAY,CACvB,KAAK,CAAC,QAAQ,SAAS,mBAAmB,GAAG,mBAAmB,CAChE,SAAQ,MAAM,CACd,QAAQ,SAAS;IAAE,MAAM,EAAE,MAAM,CAAC,SAAS,YAAY,CAAA;CAAE,GACrD,iBAAiB,CAAC,CAAC,CAAC,GACpB,MAAM,CACX;IACa,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ;gBAAlB,OAAO,CAAC,EAAE,QAAQ,YAAA;IAIvC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAqDzD;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAsC5D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,MAAM,SACX,CAAC,SAAS,mBAAmB,iBACzB,mBAAmB,GAAG,CAAC,oBAEjC,CAAA"}
|
package/dist/schema/string.js
CHANGED
|
@@ -6,6 +6,20 @@ const lex_data_1 = require("@atproto/lex-data");
|
|
|
6
6
|
const core_js_1 = require("../core.js");
|
|
7
7
|
const memoize_js_1 = require("../util/memoize.js");
|
|
8
8
|
const token_js_1 = require("./token.js");
|
|
9
|
+
/**
|
|
10
|
+
* Schema for validating string values with optional format and length constraints.
|
|
11
|
+
*
|
|
12
|
+
* Supports various string formats defined in the Lexicon specification, as well as
|
|
13
|
+
* length constraints measured in UTF-8 bytes or grapheme clusters.
|
|
14
|
+
*
|
|
15
|
+
* @template TOptions - The configuration options type
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const schema = new StringSchema({ format: 'datetime', maxLength: 64 })
|
|
20
|
+
* const result = schema.validate('2024-01-15T10:30:00Z')
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
9
23
|
class StringSchema extends core_js_1.Schema {
|
|
10
24
|
options;
|
|
11
25
|
constructor(options) {
|
|
@@ -97,6 +111,33 @@ function coerceToString(input) {
|
|
|
97
111
|
return null;
|
|
98
112
|
}
|
|
99
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Creates a string schema with optional format and length constraints.
|
|
116
|
+
*
|
|
117
|
+
* Strings can be validated against various formats (datetime, uri, did, handle, etc.)
|
|
118
|
+
* and constrained by length in UTF-8 bytes or grapheme clusters.
|
|
119
|
+
*
|
|
120
|
+
* @param options - Optional configuration for format and length constraints
|
|
121
|
+
* @returns A new {@link StringSchema} instance
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* // Basic string
|
|
126
|
+
* const nameSchema = l.string()
|
|
127
|
+
*
|
|
128
|
+
* // With format validation
|
|
129
|
+
* const dateSchema = l.string({ format: 'datetime' })
|
|
130
|
+
*
|
|
131
|
+
* // With length constraints (UTF-8 bytes)
|
|
132
|
+
* const bioSchema = l.string({ maxLength: 256 })
|
|
133
|
+
*
|
|
134
|
+
* // With grapheme constraints (user-perceived characters)
|
|
135
|
+
* const displayNameSchema = l.string({ maxGraphemes: 64 })
|
|
136
|
+
*
|
|
137
|
+
* // Combining constraints
|
|
138
|
+
* const handleSchema = l.string({ format: 'handle', minLength: 3, maxLength: 253 })
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
100
141
|
exports.string = (0, memoize_js_1.memoizedOptions)(function (options) {
|
|
101
142
|
return new StringSchema(options);
|
|
102
143
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.js","sourceRoot":"","sources":["../../src/schema/string.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"string.js","sourceRoot":"","sources":["../../src/schema/string.ts"],"names":[],"mappings":"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","sourcesContent":["import { graphemeLen, ifCid, utf8Len } from '@atproto/lex-data'\nimport {\n InferStringFormat,\n Schema,\n StringFormat,\n ValidationContext,\n isStringFormat,\n} from '../core.js'\nimport { memoizedOptions } from '../util/memoize.js'\nimport { TokenSchema } from './token.js'\n\n/**\n * Configuration options for string schema validation.\n *\n * @property format - Expected string format (e.g., 'datetime', 'uri', 'at-uri', 'did', 'handle', 'nsid', 'cid', 'tid', 'record-key', 'at-identifier', 'language')\n * @property minLength - Minimum length in UTF-8 bytes\n * @property maxLength - Maximum length in UTF-8 bytes\n * @property minGraphemes - Minimum number of grapheme clusters\n * @property maxGraphemes - Maximum number of grapheme clusters\n */\nexport type StringSchemaOptions = {\n format?: StringFormat\n minLength?: number\n maxLength?: number\n minGraphemes?: number\n maxGraphemes?: number\n}\n\n/**\n * Schema for validating string values with optional format and length constraints.\n *\n * Supports various string formats defined in the Lexicon specification, as well as\n * length constraints measured in UTF-8 bytes or grapheme clusters.\n *\n * @template TOptions - The configuration options type\n *\n * @example\n * ```ts\n * const schema = new StringSchema({ format: 'datetime', maxLength: 64 })\n * const result = schema.validate('2024-01-15T10:30:00Z')\n * ```\n */\nexport class StringSchema<\n const TOptions extends StringSchemaOptions = StringSchemaOptions,\n> extends Schema<\n TOptions extends { format: infer F extends StringFormat }\n ? InferStringFormat<F>\n : string\n> {\n constructor(readonly options?: TOptions) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n const str = coerceToString(input)\n if (str == null) {\n return ctx.issueInvalidType(input, 'string')\n }\n\n let lazyUtf8Len: number\n\n const minLength = this.options?.minLength\n if (minLength != null) {\n if ((lazyUtf8Len ??= utf8Len(str)) < minLength) {\n return ctx.issueTooSmall(str, 'string', minLength, lazyUtf8Len)\n }\n }\n\n const maxLength = this.options?.maxLength\n if (maxLength != null) {\n // Optimization: we can avoid computing the UTF-8 length if the maximum\n // possible length, in bytes, of the input JS string is smaller than the\n // maxLength (in UTF-8 string bytes).\n if (str.length * 3 <= maxLength) {\n // Input string so small it can't possibly exceed maxLength\n } else if ((lazyUtf8Len ??= utf8Len(str)) > maxLength) {\n return ctx.issueTooBig(str, 'string', maxLength, lazyUtf8Len)\n }\n }\n\n let lazyGraphLen: number\n\n const minGraphemes = this.options?.minGraphemes\n if (minGraphemes != null) {\n // Optimization: avoid counting graphemes if the length check already fails\n if (str.length < minGraphemes) {\n return ctx.issueTooSmall(str, 'grapheme', minGraphemes, str.length)\n } else if ((lazyGraphLen ??= graphemeLen(str)) < minGraphemes) {\n return ctx.issueTooSmall(str, 'grapheme', minGraphemes, lazyGraphLen)\n }\n }\n\n const maxGraphemes = this.options?.maxGraphemes\n if (maxGraphemes != null) {\n if ((lazyGraphLen ??= graphemeLen(str)) > maxGraphemes) {\n return ctx.issueTooBig(str, 'grapheme', maxGraphemes, lazyGraphLen)\n }\n }\n\n const format = this.options?.format\n if (format != null && !isStringFormat(str, format)) {\n return ctx.issueInvalidFormat(str, format)\n }\n\n return ctx.success(str)\n }\n}\n\nexport function coerceToString(input: unknown): string | null {\n switch (typeof input) {\n // @NOTE We do *not* coerce numbers/booleans to strings because that can\n // lead to them being accepted as string instead of being coerced to\n // number/boolean when the input is a string and the expected result is\n // number/boolean (e.g. in params).\n case 'string':\n return input\n case 'object': {\n if (input == null) return null\n\n // @NOTE Allow using TokenSchema instances in places expecting strings,\n // converting them to their string value.\n if (input instanceof TokenSchema) {\n return input.toString()\n }\n\n if (input instanceof Date) {\n if (Number.isNaN(input.getTime())) return null\n return input.toISOString()\n }\n\n if (input instanceof URL) {\n return input.toString()\n }\n\n const cid = ifCid(input)\n if (cid) return cid.toString()\n\n if (input instanceof String) {\n return input.valueOf()\n }\n }\n\n // falls through\n default:\n return null\n }\n}\n\n/**\n * Creates a string schema with optional format and length constraints.\n *\n * Strings can be validated against various formats (datetime, uri, did, handle, etc.)\n * and constrained by length in UTF-8 bytes or grapheme clusters.\n *\n * @param options - Optional configuration for format and length constraints\n * @returns A new {@link StringSchema} instance\n *\n * @example\n * ```ts\n * // Basic string\n * const nameSchema = l.string()\n *\n * // With format validation\n * const dateSchema = l.string({ format: 'datetime' })\n *\n * // With length constraints (UTF-8 bytes)\n * const bioSchema = l.string({ maxLength: 256 })\n *\n * // With grapheme constraints (user-perceived characters)\n * const displayNameSchema = l.string({ maxGraphemes: 64 })\n *\n * // Combining constraints\n * const handleSchema = l.string({ format: 'handle', minLength: 3, maxLength: 253 })\n * ```\n */\nexport const string = /*#__PURE__*/ memoizedOptions(function <\n const O extends StringSchemaOptions = NonNullable<unknown>,\n>(options?: StringSchemaOptions & O) {\n return new StringSchema<O>(options)\n})\n"]}
|
|
@@ -1,8 +1,41 @@
|
|
|
1
|
+
import { LexValue } from '@atproto/lex-data';
|
|
1
2
|
import { Infer, NsidString, Schema } from '../core.js';
|
|
2
3
|
import { ParamsSchema } from './params.js';
|
|
4
|
+
/**
|
|
5
|
+
* Infers the parameters type from a Subscription definition.
|
|
6
|
+
*
|
|
7
|
+
* @template S - The Subscription type
|
|
8
|
+
*/
|
|
3
9
|
export type InferSubscriptionParameters<S extends Subscription> = Infer<S['parameters']>;
|
|
10
|
+
/**
|
|
11
|
+
* Infers the message type from a Subscription definition.
|
|
12
|
+
*
|
|
13
|
+
* @template S - The Subscription type
|
|
14
|
+
*/
|
|
4
15
|
export type InferSubscriptionMessage<S extends Subscription> = Infer<S['message']>;
|
|
5
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Represents a Lexicon subscription (WebSocket) endpoint definition.
|
|
18
|
+
*
|
|
19
|
+
* Subscriptions are real-time event streams delivered over WebSocket.
|
|
20
|
+
* They have parameters for initializing the connection and a message
|
|
21
|
+
* schema for validating incoming events.
|
|
22
|
+
*
|
|
23
|
+
* @template TNsid - The NSID identifying this subscription
|
|
24
|
+
* @template TParameters - The connection parameters schema type
|
|
25
|
+
* @template TMessage - The message schema type
|
|
26
|
+
* @template TErrors - Array of error type strings, or undefined
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const firehose = new Subscription(
|
|
31
|
+
* 'com.atproto.sync.subscribeRepos',
|
|
32
|
+
* l.params({ cursor: l.optional(l.integer()) }),
|
|
33
|
+
* repoEventSchema,
|
|
34
|
+
* ['FutureCursor']
|
|
35
|
+
* )
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare class Subscription<const TNsid extends NsidString = NsidString, const TParameters extends ParamsSchema = ParamsSchema, const TMessage extends Schema<LexValue> = Schema<LexValue>, const TErrors extends undefined | readonly string[] = undefined | readonly string[]> {
|
|
6
39
|
readonly nsid: TNsid;
|
|
7
40
|
readonly parameters: TParameters;
|
|
8
41
|
readonly message: TMessage;
|
|
@@ -10,5 +43,42 @@ export declare class Subscription<const TNsid extends NsidString = NsidString, c
|
|
|
10
43
|
readonly type: "subscription";
|
|
11
44
|
constructor(nsid: TNsid, parameters: TParameters, message: TMessage, errors: TErrors);
|
|
12
45
|
}
|
|
13
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Creates a subscription definition for a Lexicon WebSocket endpoint.
|
|
48
|
+
*
|
|
49
|
+
* Subscriptions enable real-time event streaming. The connection is
|
|
50
|
+
* initialized with parameters, and the server sends messages matching
|
|
51
|
+
* the message schema.
|
|
52
|
+
*
|
|
53
|
+
* @param nsid - The NSID identifying this subscription endpoint
|
|
54
|
+
* @param parameters - Schema for connection parameters
|
|
55
|
+
* @param message - Schema for validating incoming messages
|
|
56
|
+
* @param errors - Optional array of error type strings
|
|
57
|
+
* @returns A new {@link Subscription} instance
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* // Repository event stream
|
|
62
|
+
* const subscribeRepos = l.subscription(
|
|
63
|
+
* 'com.atproto.sync.subscribeRepos',
|
|
64
|
+
* l.params({
|
|
65
|
+
* cursor: l.optional(l.integer()),
|
|
66
|
+
* }),
|
|
67
|
+
* l.typedUnion([
|
|
68
|
+
* l.typedRef(() => commitEventSchema),
|
|
69
|
+
* l.typedRef(() => handleEventSchema),
|
|
70
|
+
* l.typedRef(() => identityEventSchema),
|
|
71
|
+
* ], false),
|
|
72
|
+
* ['FutureCursor', 'ConsumerTooSlow'],
|
|
73
|
+
* )
|
|
74
|
+
*
|
|
75
|
+
* // Label stream
|
|
76
|
+
* const subscribeLabels = l.subscription(
|
|
77
|
+
* 'com.atproto.label.subscribeLabels',
|
|
78
|
+
* l.params({ cursor: l.optional(l.integer()) }),
|
|
79
|
+
* labelEventSchema,
|
|
80
|
+
* )
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function subscription<const N extends NsidString, const P extends ParamsSchema, const M extends Schema<LexValue>, const E extends undefined | readonly string[] = undefined>(nsid: N, parameters: P, message: M, errors?: E): Subscription<N, P, M, E>;
|
|
14
84
|
//# sourceMappingURL=subscription.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscription.d.ts","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CACrE,CAAC,CAAC,YAAY,CAAC,CAChB,CAAA;AAED,MAAM,MAAM,wBAAwB,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CAClE,CAAC,CAAC,SAAS,CAAC,CACb,CAAA;AAED,qBAAa,YAAY,CACvB,KAAK,CAAC,KAAK,SAAS,UAAU,GAAG,UAAU,EAC3C,KAAK,CAAC,WAAW,SAAS,YAAY,GAAG,YAAY,EACrD,KAAK,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"subscription.d.ts","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C;;;;GAIG;AACH,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CACrE,CAAC,CAAC,YAAY,CAAC,CAChB,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CAClE,CAAC,CAAC,SAAS,CAAC,CACb,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,YAAY,CACvB,KAAK,CAAC,KAAK,SAAS,UAAU,GAAG,UAAU,EAC3C,KAAK,CAAC,WAAW,SAAS,YAAY,GAAG,YAAY,EACrD,KAAK,CAAC,QAAQ,SAAS,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,EAC1D,KAAK,CAAC,OAAO,SAAS,SAAS,GAAG,SAAS,MAAM,EAAE,GAC/C,SAAS,GACT,SAAS,MAAM,EAAE;IAKnB,QAAQ,CAAC,IAAI,EAAE,KAAK;IACpB,QAAQ,CAAC,UAAU,EAAE,WAAW;IAChC,QAAQ,CAAC,OAAO,EAAE,QAAQ;IAC1B,QAAQ,CAAC,MAAM,EAAE,OAAO;IAN1B,QAAQ,CAAC,IAAI,EAAG,cAAc,CAAS;gBAG5B,IAAI,EAAE,KAAK,EACX,UAAU,EAAE,WAAW,EACvB,OAAO,EAAE,QAAQ,EACjB,MAAM,EAAE,OAAO;CAE3B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,wBAAgB,YAAY,CAC1B,KAAK,CAAC,CAAC,SAAS,UAAU,EAC1B,KAAK,CAAC,CAAC,SAAS,YAAY,EAC5B,KAAK,CAAC,CAAC,SAAS,MAAM,CAAC,QAAQ,CAAC,EAChC,KAAK,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,EACzD,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,GAAE,CAAkB,4BAE/D"}
|
|
@@ -2,6 +2,28 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Subscription = void 0;
|
|
4
4
|
exports.subscription = subscription;
|
|
5
|
+
/**
|
|
6
|
+
* Represents a Lexicon subscription (WebSocket) endpoint definition.
|
|
7
|
+
*
|
|
8
|
+
* Subscriptions are real-time event streams delivered over WebSocket.
|
|
9
|
+
* They have parameters for initializing the connection and a message
|
|
10
|
+
* schema for validating incoming events.
|
|
11
|
+
*
|
|
12
|
+
* @template TNsid - The NSID identifying this subscription
|
|
13
|
+
* @template TParameters - The connection parameters schema type
|
|
14
|
+
* @template TMessage - The message schema type
|
|
15
|
+
* @template TErrors - Array of error type strings, or undefined
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const firehose = new Subscription(
|
|
20
|
+
* 'com.atproto.sync.subscribeRepos',
|
|
21
|
+
* l.params({ cursor: l.optional(l.integer()) }),
|
|
22
|
+
* repoEventSchema,
|
|
23
|
+
* ['FutureCursor']
|
|
24
|
+
* )
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
5
27
|
class Subscription {
|
|
6
28
|
nsid;
|
|
7
29
|
parameters;
|
|
@@ -16,6 +38,43 @@ class Subscription {
|
|
|
16
38
|
}
|
|
17
39
|
}
|
|
18
40
|
exports.Subscription = Subscription;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a subscription definition for a Lexicon WebSocket endpoint.
|
|
43
|
+
*
|
|
44
|
+
* Subscriptions enable real-time event streaming. The connection is
|
|
45
|
+
* initialized with parameters, and the server sends messages matching
|
|
46
|
+
* the message schema.
|
|
47
|
+
*
|
|
48
|
+
* @param nsid - The NSID identifying this subscription endpoint
|
|
49
|
+
* @param parameters - Schema for connection parameters
|
|
50
|
+
* @param message - Schema for validating incoming messages
|
|
51
|
+
* @param errors - Optional array of error type strings
|
|
52
|
+
* @returns A new {@link Subscription} instance
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* // Repository event stream
|
|
57
|
+
* const subscribeRepos = l.subscription(
|
|
58
|
+
* 'com.atproto.sync.subscribeRepos',
|
|
59
|
+
* l.params({
|
|
60
|
+
* cursor: l.optional(l.integer()),
|
|
61
|
+
* }),
|
|
62
|
+
* l.typedUnion([
|
|
63
|
+
* l.typedRef(() => commitEventSchema),
|
|
64
|
+
* l.typedRef(() => handleEventSchema),
|
|
65
|
+
* l.typedRef(() => identityEventSchema),
|
|
66
|
+
* ], false),
|
|
67
|
+
* ['FutureCursor', 'ConsumerTooSlow'],
|
|
68
|
+
* )
|
|
69
|
+
*
|
|
70
|
+
* // Label stream
|
|
71
|
+
* const subscribeLabels = l.subscription(
|
|
72
|
+
* 'com.atproto.label.subscribeLabels',
|
|
73
|
+
* l.params({ cursor: l.optional(l.integer()) }),
|
|
74
|
+
* labelEventSchema,
|
|
75
|
+
* )
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
19
78
|
/*@__NO_SIDE_EFFECTS__*/
|
|
20
79
|
function subscription(nsid, parameters, message, errors = undefined) {
|
|
21
80
|
return new Subscription(nsid, parameters, message, errors);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":";;;AAoGA,oCAOC;AArFD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAa,YAAY;IAWZ;IACA;IACA;IACA;IANF,IAAI,GAAG,cAAuB,CAAA;IAEvC,YACW,IAAW,EACX,UAAuB,EACvB,OAAiB,EACjB,MAAe;QAHf,SAAI,GAAJ,IAAI,CAAO;QACX,eAAU,GAAV,UAAU,CAAa;QACvB,YAAO,GAAP,OAAO,CAAU;QACjB,WAAM,GAAN,MAAM,CAAS;IACvB,CAAC;CACL;AAhBD,oCAgBC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAwB;AACxB,SAAgB,YAAY,CAK1B,IAAO,EAAE,UAAa,EAAE,OAAU,EAAE,SAAY,SAAc;IAC9D,OAAO,IAAI,YAAY,CAAa,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;AACxE,CAAC","sourcesContent":["import { LexValue } from '@atproto/lex-data'\nimport { Infer, NsidString, Schema } from '../core.js'\nimport { ParamsSchema } from './params.js'\n\n/**\n * Infers the parameters type from a Subscription definition.\n *\n * @template S - The Subscription type\n */\nexport type InferSubscriptionParameters<S extends Subscription> = Infer<\n S['parameters']\n>\n\n/**\n * Infers the message type from a Subscription definition.\n *\n * @template S - The Subscription type\n */\nexport type InferSubscriptionMessage<S extends Subscription> = Infer<\n S['message']\n>\n\n/**\n * Represents a Lexicon subscription (WebSocket) endpoint definition.\n *\n * Subscriptions are real-time event streams delivered over WebSocket.\n * They have parameters for initializing the connection and a message\n * schema for validating incoming events.\n *\n * @template TNsid - The NSID identifying this subscription\n * @template TParameters - The connection parameters schema type\n * @template TMessage - The message schema type\n * @template TErrors - Array of error type strings, or undefined\n *\n * @example\n * ```ts\n * const firehose = new Subscription(\n * 'com.atproto.sync.subscribeRepos',\n * l.params({ cursor: l.optional(l.integer()) }),\n * repoEventSchema,\n * ['FutureCursor']\n * )\n * ```\n */\nexport class Subscription<\n const TNsid extends NsidString = NsidString,\n const TParameters extends ParamsSchema = ParamsSchema,\n const TMessage extends Schema<LexValue> = Schema<LexValue>,\n const TErrors extends undefined | readonly string[] =\n | undefined\n | readonly string[],\n> {\n readonly type = 'subscription' as const\n\n constructor(\n readonly nsid: TNsid,\n readonly parameters: TParameters,\n readonly message: TMessage,\n readonly errors: TErrors,\n ) {}\n}\n\n/**\n * Creates a subscription definition for a Lexicon WebSocket endpoint.\n *\n * Subscriptions enable real-time event streaming. The connection is\n * initialized with parameters, and the server sends messages matching\n * the message schema.\n *\n * @param nsid - The NSID identifying this subscription endpoint\n * @param parameters - Schema for connection parameters\n * @param message - Schema for validating incoming messages\n * @param errors - Optional array of error type strings\n * @returns A new {@link Subscription} instance\n *\n * @example\n * ```ts\n * // Repository event stream\n * const subscribeRepos = l.subscription(\n * 'com.atproto.sync.subscribeRepos',\n * l.params({\n * cursor: l.optional(l.integer()),\n * }),\n * l.typedUnion([\n * l.typedRef(() => commitEventSchema),\n * l.typedRef(() => handleEventSchema),\n * l.typedRef(() => identityEventSchema),\n * ], false),\n * ['FutureCursor', 'ConsumerTooSlow'],\n * )\n *\n * // Label stream\n * const subscribeLabels = l.subscription(\n * 'com.atproto.label.subscribeLabels',\n * l.params({ cursor: l.optional(l.integer()) }),\n * labelEventSchema,\n * )\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function subscription<\n const N extends NsidString,\n const P extends ParamsSchema,\n const M extends Schema<LexValue>,\n const E extends undefined | readonly string[] = undefined,\n>(nsid: N, parameters: P, message: M, errors: E = undefined as E) {\n return new Subscription<N, P, M, E>(nsid, parameters, message, errors)\n}\n"]}
|
package/dist/schema/token.d.ts
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
import { NsidString, Schema, ValidationContext } from '../core.js';
|
|
2
|
+
/**
|
|
3
|
+
* Schema for Lexicon token values.
|
|
4
|
+
*
|
|
5
|
+
* Tokens are named constants in Lexicon, identified by their NSID and hash.
|
|
6
|
+
* They validate to their string value (e.g., 'app.bsky.feed.defs#requestLess').
|
|
7
|
+
* TokenSchema instances can also be used as values themselves.
|
|
8
|
+
*
|
|
9
|
+
* @template TValue - The token string literal type
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const schema = new TokenSchema('app.bsky.feed.defs#requestLess')
|
|
14
|
+
* schema.validate('app.bsky.feed.defs#requestLess') // success
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
2
17
|
export declare class TokenSchema<const TValue extends string = string> extends Schema<TValue> {
|
|
3
18
|
readonly value: TValue;
|
|
4
19
|
constructor(value: TValue);
|
|
@@ -6,5 +21,37 @@ export declare class TokenSchema<const TValue extends string = string> extends S
|
|
|
6
21
|
toJSON(): string;
|
|
7
22
|
toString(): string;
|
|
8
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Creates a token schema for Lexicon named constants.
|
|
26
|
+
*
|
|
27
|
+
* Tokens are used in Lexicon as named constants or enum-like values.
|
|
28
|
+
* The token instance can be used both as a schema validator and as
|
|
29
|
+
* the token value itself (it serializes to its string value).
|
|
30
|
+
*
|
|
31
|
+
* @param nsid - The NSID part of the token
|
|
32
|
+
* @param hash - The hash part of the token (defaults to 'main')
|
|
33
|
+
* @returns A new {@link TokenSchema} instance
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* // Define tokens
|
|
38
|
+
* const requestLess = l.token('app.bsky.feed.defs', 'requestLess')
|
|
39
|
+
* const requestMore = l.token('app.bsky.feed.defs', 'requestMore')
|
|
40
|
+
*
|
|
41
|
+
* // Use as a value
|
|
42
|
+
* console.log(requestLess.toString()) // 'app.bsky.feed.defs#requestLess'
|
|
43
|
+
*
|
|
44
|
+
* // Use in union for validation
|
|
45
|
+
* const feedbackSchema = l.union([requestLess, requestMore])
|
|
46
|
+
*
|
|
47
|
+
* // Validate
|
|
48
|
+
* feedbackSchema.parse('app.bsky.feed.defs#requestLess') // success
|
|
49
|
+
*
|
|
50
|
+
* // Token instances can be used as values in other schemas
|
|
51
|
+
* const feedbackRequest = l.object({
|
|
52
|
+
* feedback: requestLess, // Accepts the token value
|
|
53
|
+
* })
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
9
56
|
export declare function token<const N extends NsidString, const H extends string = 'main'>(nsid: N, hash?: H): TokenSchema<import("../core.js").$Type<N, H>>;
|
|
10
57
|
//# sourceMappingURL=token.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/schema/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,UAAU,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEzE,qBAAa,WAAW,CACtB,KAAK,CAAC,MAAM,SAAS,MAAM,GAAG,MAAM,CACpC,SAAQ,MAAM,CAAC,MAAM,CAAC;IACV,QAAQ,CAAC,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;IAIlC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;IAqBxD,MAAM,IAAI,MAAM;IAIhB,QAAQ,IAAI,MAAM;CAGnB;
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/schema/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,UAAU,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEzE;;;;;;;;;;;;;;GAcG;AACH,qBAAa,WAAW,CACtB,KAAK,CAAC,MAAM,SAAS,MAAM,GAAG,MAAM,CACpC,SAAQ,MAAM,CAAC,MAAM,CAAC;IACV,QAAQ,CAAC,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;IAIlC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;IAqBxD,MAAM,IAAI,MAAM;IAIhB,QAAQ,IAAI,MAAM;CAGnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,wBAAgB,KAAK,CACnB,KAAK,CAAC,CAAC,SAAS,UAAU,EAC1B,KAAK,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAC/B,IAAI,EAAE,CAAC,EAAE,IAAI,GAAE,CAAe,iDAE/B"}
|
package/dist/schema/token.js
CHANGED
|
@@ -3,6 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.TokenSchema = void 0;
|
|
4
4
|
exports.token = token;
|
|
5
5
|
const core_js_1 = require("../core.js");
|
|
6
|
+
/**
|
|
7
|
+
* Schema for Lexicon token values.
|
|
8
|
+
*
|
|
9
|
+
* Tokens are named constants in Lexicon, identified by their NSID and hash.
|
|
10
|
+
* They validate to their string value (e.g., 'app.bsky.feed.defs#requestLess').
|
|
11
|
+
* TokenSchema instances can also be used as values themselves.
|
|
12
|
+
*
|
|
13
|
+
* @template TValue - The token string literal type
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const schema = new TokenSchema('app.bsky.feed.defs#requestLess')
|
|
18
|
+
* schema.validate('app.bsky.feed.defs#requestLess') // success
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
6
21
|
class TokenSchema extends core_js_1.Schema {
|
|
7
22
|
value;
|
|
8
23
|
constructor(value) {
|
|
@@ -33,6 +48,38 @@ class TokenSchema extends core_js_1.Schema {
|
|
|
33
48
|
}
|
|
34
49
|
}
|
|
35
50
|
exports.TokenSchema = TokenSchema;
|
|
51
|
+
/**
|
|
52
|
+
* Creates a token schema for Lexicon named constants.
|
|
53
|
+
*
|
|
54
|
+
* Tokens are used in Lexicon as named constants or enum-like values.
|
|
55
|
+
* The token instance can be used both as a schema validator and as
|
|
56
|
+
* the token value itself (it serializes to its string value).
|
|
57
|
+
*
|
|
58
|
+
* @param nsid - The NSID part of the token
|
|
59
|
+
* @param hash - The hash part of the token (defaults to 'main')
|
|
60
|
+
* @returns A new {@link TokenSchema} instance
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* // Define tokens
|
|
65
|
+
* const requestLess = l.token('app.bsky.feed.defs', 'requestLess')
|
|
66
|
+
* const requestMore = l.token('app.bsky.feed.defs', 'requestMore')
|
|
67
|
+
*
|
|
68
|
+
* // Use as a value
|
|
69
|
+
* console.log(requestLess.toString()) // 'app.bsky.feed.defs#requestLess'
|
|
70
|
+
*
|
|
71
|
+
* // Use in union for validation
|
|
72
|
+
* const feedbackSchema = l.union([requestLess, requestMore])
|
|
73
|
+
*
|
|
74
|
+
* // Validate
|
|
75
|
+
* feedbackSchema.parse('app.bsky.feed.defs#requestLess') // success
|
|
76
|
+
*
|
|
77
|
+
* // Token instances can be used as values in other schemas
|
|
78
|
+
* const feedbackRequest = l.object({
|
|
79
|
+
* feedback: requestLess, // Accepts the token value
|
|
80
|
+
* })
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
36
83
|
/*@__NO_SIDE_EFFECTS__*/
|
|
37
84
|
function token(nsid, hash = 'main') {
|
|
38
85
|
return new TokenSchema((0, core_js_1.$type)(nsid, hash));
|