@atproto/lex-schema 0.0.11 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +54 -0
- package/dist/core/$type.d.ts +149 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +44 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +44 -0
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js +30 -0
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/result.d.ts +85 -4
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +60 -4
- package/dist/core/result.js.map +1 -1
- package/dist/core/schema.d.ts +232 -5
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +197 -4
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +244 -11
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +150 -0
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.d.ts +90 -3
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/validation-error.d.ts +60 -0
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +60 -0
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.d.ts +61 -0
- package/dist/core/validation-issue.d.ts.map +1 -1
- package/dist/core/validation-issue.js +54 -1
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +356 -11
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +203 -4
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +12 -28
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +46 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +16 -1
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +50 -2
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +44 -2
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +29 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +30 -1
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +39 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +34 -1
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +39 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +35 -1
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +67 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +55 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +45 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +46 -1
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +59 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +47 -1
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +49 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +49 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +43 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +38 -1
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +55 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +50 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/lex-map.d.ts +37 -0
- package/dist/schema/lex-map.d.ts.map +1 -0
- package/dist/schema/lex-map.js +60 -0
- package/dist/schema/lex-map.js.map +1 -0
- package/dist/schema/lex-value.d.ts +35 -0
- package/dist/schema/lex-value.d.ts.map +1 -0
- package/dist/schema/lex-value.js +87 -0
- package/dist/schema/lex-value.js.map +1 -0
- package/dist/schema/literal.d.ts +45 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +45 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +43 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +44 -1
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +30 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +31 -1
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +42 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +42 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +57 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +53 -1
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +43 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +43 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +96 -12
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +155 -21
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +111 -15
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +73 -3
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +58 -0
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +50 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +42 -0
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +39 -0
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +64 -0
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +64 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +55 -0
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +55 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +76 -25
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +21 -0
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +51 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +18 -0
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +58 -9
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +45 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +46 -1
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +72 -6
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +56 -8
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +72 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +59 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +48 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +49 -1
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +73 -23
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +20 -1
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +54 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +16 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +51 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +52 -2
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +46 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +41 -0
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown.d.ts +34 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +34 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +45 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +45 -0
- package/dist/schema/with-default.js.map +1 -1
- package/dist/schema.d.ts +2 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +2 -1
- package/dist/schema.js.map +1 -1
- package/dist/util/if-any.d.ts +2 -0
- package/dist/util/if-any.d.ts.map +1 -0
- package/dist/util/if-any.js +3 -0
- package/dist/util/if-any.js.map +1 -0
- package/package.json +3 -3
- package/src/core/$type.ts +150 -18
- package/src/core/record-key.ts +44 -0
- package/src/core/result.ts +86 -4
- package/src/core/schema.ts +244 -9
- package/src/core/string-format.ts +259 -13
- package/src/core/types.ts +91 -3
- package/src/core/validation-error.ts +60 -0
- package/src/core/validation-issue.ts +68 -2
- package/src/core/validator.ts +373 -12
- package/src/helpers.test.ts +110 -29
- package/src/helpers.ts +54 -25
- package/src/schema/array.test.ts +94 -79
- package/src/schema/array.ts +48 -1
- package/src/schema/blob.ts +50 -1
- package/src/schema/boolean.ts +31 -1
- package/src/schema/bytes.ts +41 -1
- package/src/schema/cid.ts +41 -1
- package/src/schema/custom.ts +68 -1
- package/src/schema/dict.ts +47 -1
- package/src/schema/discriminated-union.ts +61 -1
- package/src/schema/enum.ts +50 -0
- package/src/schema/integer.ts +45 -1
- package/src/schema/intersection.ts +56 -0
- package/src/schema/{unknown-object.test.ts → lex-map.test.ts} +9 -9
- package/src/schema/lex-map.ts +63 -0
- package/src/schema/lex-value.test.ts +81 -0
- package/src/schema/lex-value.ts +86 -0
- package/src/schema/literal.ts +46 -0
- package/src/schema/never.ts +45 -1
- package/src/schema/null.ts +32 -1
- package/src/schema/nullable.ts +43 -0
- package/src/schema/object.ts +59 -1
- package/src/schema/optional.ts +44 -0
- package/src/schema/params.test.ts +133 -38
- package/src/schema/params.ts +237 -37
- package/src/schema/payload.test.ts +3 -3
- package/src/schema/payload.ts +145 -42
- package/src/schema/permission-set.ts +58 -0
- package/src/schema/permission.ts +42 -0
- package/src/schema/procedure.ts +64 -0
- package/src/schema/query.ts +55 -0
- package/src/schema/record.ts +82 -16
- package/src/schema/ref.ts +52 -0
- package/src/schema/refine.ts +58 -9
- package/src/schema/regexp.ts +47 -1
- package/src/schema/string.test.ts +99 -2
- package/src/schema/string.ts +108 -15
- package/src/schema/subscription.ts +72 -2
- package/src/schema/token.ts +50 -1
- package/src/schema/typed-object.ts +81 -16
- package/src/schema/typed-ref.ts +55 -0
- package/src/schema/typed-union.ts +58 -3
- package/src/schema/union.ts +47 -0
- package/src/schema/unknown.ts +35 -0
- package/src/schema/with-default.ts +46 -0
- package/src/schema.ts +2 -1
- package/src/util/if-any.ts +3 -0
- package/dist/schema/unknown-object.d.ts +0 -8
- package/dist/schema/unknown-object.d.ts.map +0 -1
- package/dist/schema/unknown-object.js +0 -19
- package/dist/schema/unknown-object.js.map +0 -1
- package/src/schema/unknown-object.ts +0 -19
package/dist/core/schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":";;;AAAA,iDAOuB;AAavB,MAAsB,MAAM;IAgB1B;;;;;OAKG;IACH,MAAM,CAAC,KAAc;QACnB,MAAM,MAAM,GAAG,gCAAiB,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;QACtD,IAAI,CAAC,MAAM,CAAC,OAAO;YAAE,MAAM,MAAM,CAAC,MAAM,CAAA;IAC1C,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,KAAc;QAClB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IACpB,CAAC;IAED;;;;OAIG;IACH,IAAI,CAAI,KAAQ;QACd,MAAM,MAAM,GAAG,gCAAiB,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;QACtD,IAAI,MAAM,CAAC,OAAO;Y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EAAiE;IACjE,uEAAuE;IACvE,wEAAwE;IACxE,8EAA8E;IAC9E,2EAA2E;IAC3E,6EAA6E;IAC7E,+DAA+D;IAC/D,EAAE;IACF,wFAAwF;IACxF,qGAAqG;IAErG,OAAO,CAAC,KAAc;QACpB,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAC3B,CAAC;IAED,MAAM,CAAC,KAAc;QACnB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;IAC1B,CAAC;IAED,KAAK,CAAI,KAAQ;QACf,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IACzB,CAAC;IAED,QAAQ,CAAC,KAAc;QACrB,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC5B,CAAC;IAED,UAAU,CAAI,KAAQ;QACpB,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;IAED,MAAM,CAAC,KAAc,EAAE,OAAyB;QAC9C,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACnC,CAAC;IAED,UAAU,CACR,KAAc,EACd,OAAyB;QAEzB,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACvC,CAAC;IAED,SAAS,CAAI,KAAQ,EAAE,OAAyB;QAC9C,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACtC,CAAC;IAED,aAAa,CACX,KAAQ,EACR,OAAyB;QAEzB,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAC1C,CAAC;CACF;AAhJD,wBAgJC","sourcesContent":["import {\n InferInput,\n InferOutput,\n ValidationContext,\n ValidationOptions,\n ValidationResult,\n Validator,\n} from './validator.js'\n\ntype ParseOptions = Omit<ValidationOptions, 'mode'>\ntype ValidateOptions = Omit<ValidationOptions, 'mode'>\n\nexport interface SchemaInternals<out TInput = unknown, out TOutput = TInput> {\n /** @internal The inferred validation type */\n input: TInput\n\n /** @internal The inferred parse type */\n output: TOutput\n}\n\nexport abstract class Schema<\n out TInput = unknown,\n out TOutput = TInput,\n out TInternals extends SchemaInternals<TInput, TOutput> = SchemaInternals<\n TInput,\n TOutput\n >,\n> implements Validator<TInternals['input'], TInternals['output']>\n{\n declare readonly ['__lex']: TInternals\n\n abstract validateInContext(\n input: unknown,\n ctx: ValidationContext,\n ): ValidationResult\n\n /**\n * @note use {@link check}() instead of {@link assert}() if you encounter a\n * `ts(2775)` error and you are not able to fully type the validator. This\n * will typically arise in generic contexts, where the narrowed type is not\n * needed.\n */\n assert(input: unknown): asserts input is InferInput<this> {\n const result = ValidationContext.validate(input, this)\n if (!result.success) throw result.reason\n }\n\n /**\n * Alias for {@link assert}(). Most useful in generic contexts where the\n * validator is not exactly typed, allowing to avoid \"_Assertions require\n * every name in the call target to be declared with an explicit type\n * annotation. ts(2775)_\" errors.\n */\n check(input: unknown): void {\n this.assert(input)\n }\n\n /**\n * Casts the input (by validating it) to the output type if it matches the\n * schema, otherwise throws. This is the same as calling {@link parse}() with\n * `mode: \"validate\"`.\n */\n cast<I>(input: I): I & InferInput<this> {\n const result = ValidationContext.validate(input, this)\n if (result.success) return result.value\n throw result.reason\n }\n\n matches<I>(input: I): input is I & InferInput<this> {\n const result = ValidationContext.validate(input, this)\n return result.success\n }\n\n ifMatches<I>(input: I): (I & InferInput<this>) | undefined {\n return this.matches(input) ? input : undefined\n }\n\n parse(input: unknown, options?: ParseOptions): InferOutput<this> {\n const result = this.safeParse(input, options)\n if (result.success) return result.value\n throw result.reason\n }\n\n safeParse(\n input: unknown,\n options?: ParseOptions,\n ): ValidationResult<InferOutput<this>> {\n return ValidationContext.validate(input, this, {\n ...options,\n mode: 'parse',\n })\n }\n\n validate<I>(input: I, options?: ValidateOptions): I & InferInput<this> {\n const result = this.safeValidate(input, options)\n if (result.success) return result.value\n throw result.reason\n }\n\n safeValidate<I>(\n input: I,\n options?: ValidateOptions,\n ): ValidationResult<I & InferInput<this>> {\n return ValidationContext.validate(input, this, {\n ...options,\n mode: 'validate',\n })\n }\n\n // @NOTE The built lexicons namespaces will export utility functions that\n // allow accessing the schema's methods without the need to specify \".main.\"\n // as part of the namespace. This way, a utility for a particular record type\n // can be called like \"app.bsky.feed.post.<utility>()\" instead of\n // \"app.bsky.feed.post.main.<utility>()\". Because those utilities could\n // conflict with other schemas (e.g. if there is a lexicon definition at\n // \"#<utility>\"), those exported utilities will be prefixed with \"$\". In order\n // to be able to consistently call the utilities, when using the \"main\" and\n // non \"main\" definitions, we also expose the same methods with a \"$\" prefix.\n // Thanks to this, both of the following call will be possible:\n //\n // - \"app.bsky.feed.post.$parse(...)\" // calls a utility function created by \"lex build\"\n // - \"app.bsky.feed.defs.postView.$parse(...)\" // uses the alias defined below on the schema instance\n\n $assert(input: unknown): asserts input is InferInput<this> {\n return this.assert(input)\n }\n\n $check(input: unknown): void {\n return this.check(input)\n }\n\n $cast<I>(input: I): I & InferInput<this> {\n return this.cast(input)\n }\n\n $matches(input: unknown): input is InferInput<this> {\n return this.matches(input)\n }\n\n $ifMatches<I>(input: I): (I & InferInput<this>) | undefined {\n return this.ifMatches(input)\n }\n\n $parse(input: unknown, options?: ValidateOptions): InferOutput<this> {\n return this.parse(input, options)\n }\n\n $safeParse(\n input: unknown,\n options?: ValidateOptions,\n ): ValidationResult<InferOutput<this>> {\n return this.safeParse(input, options)\n }\n\n $validate<I>(input: I, options?: ValidateOptions): I & InferInput<this> {\n return this.validate(input, options)\n }\n\n $safeValidate<I>(\n input: I,\n options?: ValidateOptions,\n ): ValidationResult<I & InferInput<this>> {\n return this.safeValidate(input, options)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":";;;AAAA,iDAOuB;AA8BvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;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AAA;IAC5B,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAI,KAAQ;QACpB,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,KAAc,EAAE,OAAyB;QAC9C,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACnC,CAAC;IAED;;;;OAIG;IACH,UAAU,CACR,KAAc,EACd,OAAyB;QAEzB,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACvC,CAAC;IAED;;;;OAIG;IACH,SAAS,CAAI,KAAQ,EAAE,OAAyB;QAC9C,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACtC,CAAC;IAED;;;;OAIG;IACH,aAAa,CACX,KAAQ,EACR,OAAyB;QAEzB,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAC1C,CAAC;CACF;AAtUD,wBAsUC","sourcesContent":["import {\n InferInput,\n InferOutput,\n ValidationContext,\n ValidationOptions,\n ValidationResult,\n Validator,\n} from './validator.js'\n\n/**\n * Options for parsing operations.\n * Excludes the `mode` option as it is implicitly set to `\"parse\"`.\n */\nexport type ParseOptions = Omit<ValidationOptions, 'mode'>\n\n/**\n * Options for validation operations.\n * Excludes the `mode` option as it is implicitly set to `\"validate\"`.\n */\nexport type ValidateOptions = Omit<ValidationOptions, 'mode'>\n\n/**\n * Internal type structure for schema type inference.\n *\n * This interface defines the phantom types used for compile-time type inference\n * without affecting runtime behavior. The `input` and `output` properties\n * represent the expected input type during validation and the resulting output\n * type after parsing, respectively.\n *\n * @typeParam TInput - The type accepted as input during validation\n * @typeParam TOutput - The type returned after parsing (may differ from input due to coercion)\n */\nexport interface SchemaInternals<out TInput = unknown, out TOutput = TInput> {\n input: TInput\n output: TOutput\n}\n\n/**\n * Abstract base class for all schema validators in the lexicon system.\n *\n * This class provides the standard validation interface that all schema types\n * implement. It offers multiple methods for validating and parsing data:\n *\n * - **Assertion methods**: `assert()`, `check()` - throw on invalid input\n * - **Type guard methods**: `matches()`, `ifMatches()` - return boolean or optional value\n * - **Parse methods**: `parse()`, `safeParse()` - allow value transformation/coercion\n * - **Validate methods**: `validate()`, `safeValidate()` - strict validation without coercion\n *\n * All methods are also available with a `$` prefix (e.g., `$parse()`, `$validate()`)\n * for consistent access in generated lexicon namespaces.\n *\n * @typeParam TInput - The type accepted as valid input during validation\n * @typeParam TOutput - The type returned after parsing (may include transformations)\n * @typeParam TInternals - Internal type structure for type inference\n *\n * @example\n * ```typescript\n * class MySchema extends Schema<string> {\n * validateInContext(input: unknown, ctx: ValidationContext): ValidationResult {\n * if (typeof input !== 'string') {\n * return ctx.issueUnexpectedType(input, 'string')\n * }\n * return ctx.success(input)\n * }\n * }\n *\n * const schema = new MySchema()\n * schema.assert('hello') // OK\n * schema.assert(123) // Throws ValidationError\n * schema.matches('hello') // true\n * schema.matches(123) // false\n * ```\n */\nexport abstract class Schema<\n out TInput = unknown,\n out TOutput = TInput,\n out TInternals extends SchemaInternals<TInput, TOutput> = SchemaInternals<\n TInput,\n TOutput\n >,\n> implements Validator<TInternals['input'], TInternals['output']>\n{\n /**\n * Internal phantom property for type inference.\n * This property does not exist at runtime.\n *\n * @internal\n */\n declare readonly ['__lex']: TInternals\n\n // Needed to discriminate multiple schema types when used in unions. Without\n // this, Typescript could allow an EnumSchema<\"foo\" | \"bar\"> to be used where\n // a StringSchema is expected, since they would both be structurally\n // compatible.\n abstract readonly type: string\n\n /**\n * Performs validation of the input value within a validation context.\n *\n * This method must be implemented by subclasses to define the actual\n * validation logic. It should not be called directly; use\n * {@link ValidationContext.validate} instead to ensure proper mode enforcement.\n *\n * @param input - The value to validate\n * @param ctx - The validation context providing path tracking and issue reporting\n * @returns A validation result indicating success with the validated value or failure with issues\n *\n * @internal\n */\n abstract validateInContext(\n input: unknown,\n ctx: ValidationContext,\n ): ValidationResult\n\n /**\n * @note use {@link check}() instead of {@link assert}() if you encounter a\n * `ts(2775)` error and you are not able to fully type the validator. This\n * will typically arise in generic contexts, where the narrowed type is not\n * needed.\n */\n assert(input: unknown): asserts input is InferInput<this> {\n const result = ValidationContext.validate(input, this)\n if (!result.success) throw result.reason\n }\n\n /**\n * Alias for {@link assert}(). Most useful in generic contexts where the\n * validator is not exactly typed, allowing to avoid \"_Assertions require\n * every name in the call target to be declared with an explicit type\n * annotation. ts(2775)_\" errors.\n */\n check(input: unknown): void {\n this.assert(input)\n }\n\n /**\n * Casts the input (by validating it) to the output type if it matches the\n * schema, otherwise throws. This is the same as calling {@link parse}() with\n * `mode: \"validate\"`.\n */\n cast<I>(input: I): I & InferInput<this> {\n const result = ValidationContext.validate(input, this)\n if (result.success) return result.value\n throw result.reason\n }\n\n /**\n * Type guard that checks if the input matches this schema.\n *\n * @param input - The value to check\n * @returns `true` if the input is valid according to this schema\n *\n * @example\n * ```typescript\n * if (schema.matches(data)) {\n * // data is narrowed to the schema's input type\n * console.log(data)\n * }\n * ```\n */\n matches<I>(input: I): input is I & InferInput<this> {\n const result = ValidationContext.validate(input, this)\n return result.success\n }\n\n /**\n * Returns the input if it matches this schema, otherwise returns `undefined`.\n *\n * This is useful for optional filtering operations where you want to\n * conditionally extract values that match a schema.\n *\n * @param input - The value to check\n * @returns The input value with narrowed type if valid, otherwise `undefined`\n *\n * @example\n * ```typescript\n * const validData = schema.ifMatches(data)\n * if (validData !== undefined) {\n * // validData is the schema's input type\n * console.log(validData)\n * }\n * ```\n */\n ifMatches<I>(input: I): (I & InferInput<this>) | undefined {\n return this.matches(input) ? input : undefined\n }\n\n /**\n * Parses the input, allowing value transformations and coercion.\n *\n * Unlike {@link validate}, this method allows the schema to transform\n * the input value (e.g., applying default values, type coercion).\n * Throws a {@link ValidationError} if the input is invalid.\n *\n * @param input - The value to parse\n * @param options - Optional parsing configuration\n * @returns The parsed and potentially transformed value\n * @throws {ValidationError} If the input fails validation\n *\n * @example\n * ```typescript\n * const result = schema.parse(rawData)\n * // result has defaults applied and is fully typed\n * ```\n */\n parse(input: unknown, options?: ParseOptions): InferOutput<this> {\n const result = this.safeParse(input, options)\n if (result.success) return result.value\n throw result.reason\n }\n\n /**\n * Safely parses the input without throwing, returning a result object.\n *\n * This method allows value transformations like {@link parse}, but\n * returns a discriminated union result instead of throwing on error.\n *\n * @param input - The value to parse\n * @param options - Optional parsing configuration\n * @returns A {@link ValidationResult} with either the parsed value or validation errors\n *\n * @example\n * ```typescript\n * const result = schema.safeParse(data)\n * if (result.success) {\n * console.log(result.value)\n * } else {\n * console.error(result.reason.issues)\n * }\n * ```\n */\n safeParse(\n input: unknown,\n options?: ParseOptions,\n ): ValidationResult<InferOutput<this>> {\n return ValidationContext.validate(input, this, {\n ...options,\n mode: 'parse',\n })\n }\n\n /**\n * Validates the input strictly without allowing transformations.\n *\n * Unlike {@link parse}, this method requires the input to exactly match\n * the schema without any transformations (no defaults applied, no coercion).\n * Throws a {@link ValidationError} if the input is invalid or would require transformation.\n *\n * @typeParam I - The input type (preserved in the return type)\n * @param input - The value to validate\n * @param options - Optional validation configuration\n * @returns The validated input with narrowed type\n * @throws {ValidationError} If the input fails validation or requires transformation\n *\n * @example\n * ```typescript\n * const validated = schema.validate(data)\n * // validated is typed as the intersection of input type and schema type\n * ```\n */\n validate<I>(input: I, options?: ValidateOptions): I & InferInput<this> {\n const result = this.safeValidate(input, options)\n if (result.success) return result.value\n throw result.reason\n }\n\n /**\n * Safely validates the input without throwing, returning a result object.\n *\n * This method performs strict validation like {@link validate}, but\n * returns a discriminated union result instead of throwing on error.\n *\n * @typeParam I - The input type (preserved in the result value type)\n * @param input - The value to validate\n * @param options - Optional validation configuration\n * @returns A {@link ValidationResult} with either the validated value or validation errors\n *\n * @example\n * ```typescript\n * const result = schema.safeValidate(data)\n * if (result.success) {\n * console.log(result.value)\n * } else {\n * console.error(result.reason.issues)\n * }\n * ```\n */\n safeValidate<I>(\n input: I,\n options?: ValidateOptions,\n ): ValidationResult<I & InferInput<this>> {\n return ValidationContext.validate(input, this, {\n ...options,\n mode: 'validate',\n })\n }\n\n // @NOTE Dollar-prefixed aliases\n //\n // The built lexicons namespaces export utility functions that allow accessing\n // the schema's methods without the need to specify \".main.\" as part of the\n // namespace. This way, a utility for a particular record type can be called\n // like \"app.bsky.feed.post.<utility>()\" instead of\n // \"app.bsky.feed.post.main.<utility>()\". Because those utilities could\n // conflict with other schemas (e.g. if there is a lexicon definition at\n // \"#<utility>\"), those exported utilities will be prefixed with \"$\". In order\n // to be able to consistently call the utilities, when using the \"main\" and\n // non \"main\" definitions, we also expose the same methods with a \"$\" prefix.\n // Thanks to this, both of the following call will be possible:\n //\n // - \"app.bsky.feed.post.$parse(...)\" // calls a utility function created by \"lex build\"\n // - \"app.bsky.feed.defs.postView.$parse(...)\" // uses the alias defined below on the schema instance\n\n /**\n * Alias for {@link assert} with `$` prefix for namespace compatibility.\n *\n * @see {@link assert}\n */\n $assert(input: unknown): asserts input is InferInput<this> {\n return this.assert(input)\n }\n\n /**\n * Alias for {@link check} with `$` prefix for namespace compatibility.\n *\n * @see {@link check}\n */\n $check(input: unknown): void {\n return this.check(input)\n }\n\n /**\n * Alias for {@link cast} with `$` prefix for namespace compatibility.\n *\n * @see {@link cast}\n */\n $cast<I>(input: I): I & InferInput<this> {\n return this.cast(input)\n }\n\n /**\n * Alias for {@link matches} with `$` prefix for namespace compatibility.\n *\n * @see {@link matches}\n */\n $matches(input: unknown): input is InferInput<this> {\n return this.matches(input)\n }\n\n /**\n * Alias for {@link ifMatches} with `$` prefix for namespace compatibility.\n *\n * @see {@link ifMatches}\n */\n $ifMatches<I>(input: I): (I & InferInput<this>) | undefined {\n return this.ifMatches(input)\n }\n\n /**\n * Alias for {@link parse} with `$` prefix for namespace compatibility.\n *\n * @see {@link parse}\n */\n $parse(input: unknown, options?: ValidateOptions): InferOutput<this> {\n return this.parse(input, options)\n }\n\n /**\n * Alias for {@link safeParse} with `$` prefix for namespace compatibility.\n *\n * @see {@link safeParse}\n */\n $safeParse(\n input: unknown,\n options?: ValidateOptions,\n ): ValidationResult<InferOutput<this>> {\n return this.safeParse(input, options)\n }\n\n /**\n * Alias for {@link validate} with `$` prefix for namespace compatibility.\n *\n * @see {@link validate}\n */\n $validate<I>(input: I, options?: ValidateOptions): I & InferInput<this> {\n return this.validate(input, options)\n }\n\n /**\n * Alias for {@link safeValidate} with `$` prefix for namespace compatibility.\n *\n * @see {@link safeValidate}\n */\n $safeValidate<I>(\n input: I,\n options?: ValidateOptions,\n ): ValidationResult<I & InferInput<this>> {\n return this.safeValidate(input, options)\n }\n}\n"]}
|
|
@@ -1,27 +1,165 @@
|
|
|
1
1
|
import { AtIdentifierString, AtUriString, DatetimeString, DidString, HandleString, NsidString, RecordKeyString, TidString, UriString } from '@atproto/syntax';
|
|
2
2
|
import { CheckFn } from '../util/assertion-util.js';
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Type guard that checks if a value is a valid AT identifier (DID or handle).
|
|
5
|
+
*
|
|
6
|
+
* @param value - The value to check
|
|
7
|
+
* @returns `true` if the value is a valid AT identifier
|
|
8
|
+
*/
|
|
4
9
|
export declare const isAtIdentifierString: CheckFn<AtIdentifierString>;
|
|
5
|
-
export type {
|
|
10
|
+
export type {
|
|
11
|
+
/**
|
|
12
|
+
* An AT identifier string - either a DID or a handle.
|
|
13
|
+
*
|
|
14
|
+
* @example `"did:plc:1234..."` or `"alice.bsky.social"`
|
|
15
|
+
*/
|
|
16
|
+
AtIdentifierString, };
|
|
17
|
+
/**
|
|
18
|
+
* Type guard that checks if a value is a valid AT URI.
|
|
19
|
+
*
|
|
20
|
+
* @param value - The value to check
|
|
21
|
+
* @returns `true` if the value is a valid AT URI
|
|
22
|
+
*/
|
|
6
23
|
export declare const isAtUriString: CheckFn<AtUriString>;
|
|
7
|
-
export type
|
|
24
|
+
export type {
|
|
25
|
+
/**
|
|
26
|
+
* An AT URI string pointing to a resource in the AT Protocol network.
|
|
27
|
+
*
|
|
28
|
+
* @example `"at://did:plc:1234.../app.bsky.feed.post/3k2..."`
|
|
29
|
+
*/
|
|
30
|
+
AtUriString, };
|
|
31
|
+
/**
|
|
32
|
+
* Type guard that checks if a value is a valid CID string.
|
|
33
|
+
*
|
|
34
|
+
* @param value - The value to check
|
|
35
|
+
* @returns `true` if the value is a valid CID string
|
|
36
|
+
*/
|
|
8
37
|
export declare const isCidString: CheckFn<CidString>;
|
|
9
|
-
|
|
38
|
+
/**
|
|
39
|
+
* A Content Identifier (CID) string.
|
|
40
|
+
*
|
|
41
|
+
* CIDs are self-describing content addresses used to identify data by its hash.
|
|
42
|
+
*
|
|
43
|
+
* @example `"bafyreig..."`
|
|
44
|
+
*/
|
|
45
|
+
export type CidString = string;
|
|
46
|
+
/**
|
|
47
|
+
* Type guard that checks if a value is a valid datetime string.
|
|
48
|
+
*
|
|
49
|
+
* @param value - The value to check
|
|
50
|
+
* @returns `true` if the value is a valid datetime string
|
|
51
|
+
*/
|
|
10
52
|
export declare const isDatetimeString: CheckFn<DatetimeString>;
|
|
11
|
-
export type {
|
|
53
|
+
export type {
|
|
54
|
+
/**
|
|
55
|
+
* An ISO 8601 datetime string.
|
|
56
|
+
*
|
|
57
|
+
* @example `"2024-01-15T12:30:00.000Z"`
|
|
58
|
+
*/
|
|
59
|
+
DatetimeString, };
|
|
60
|
+
/**
|
|
61
|
+
* Type guard that checks if a value is a valid DID string.
|
|
62
|
+
*
|
|
63
|
+
* @param value - The value to check
|
|
64
|
+
* @returns `true` if the value is a valid DID string
|
|
65
|
+
*/
|
|
12
66
|
export declare const isDidString: CheckFn<DidString>;
|
|
13
|
-
export type {
|
|
67
|
+
export type {
|
|
68
|
+
/**
|
|
69
|
+
* A Decentralized Identifier (DID) string.
|
|
70
|
+
*
|
|
71
|
+
* DIDs are globally unique identifiers that don't require a central authority.
|
|
72
|
+
*
|
|
73
|
+
* @example `"did:plc:1234abcd..."` or `"did:web:example.com"`
|
|
74
|
+
*/
|
|
75
|
+
DidString, };
|
|
76
|
+
/**
|
|
77
|
+
* Type guard that checks if a value is a valid handle string.
|
|
78
|
+
*
|
|
79
|
+
* @param value - The value to check
|
|
80
|
+
* @returns `true` if the value is a valid handle string
|
|
81
|
+
*/
|
|
14
82
|
export declare const isHandleString: CheckFn<HandleString>;
|
|
15
|
-
export type
|
|
83
|
+
export type {
|
|
84
|
+
/**
|
|
85
|
+
* A handle string - a human-readable identifier for users.
|
|
86
|
+
*
|
|
87
|
+
* @example `"alice.bsky.social"` or `"bob.example.com"`
|
|
88
|
+
*/
|
|
89
|
+
HandleString, };
|
|
90
|
+
/**
|
|
91
|
+
* Type guard that checks if a value is a valid BCP-47 language tag.
|
|
92
|
+
*
|
|
93
|
+
* @param value - The value to check
|
|
94
|
+
* @returns `true` if the value is a valid language string
|
|
95
|
+
*/
|
|
16
96
|
export declare const isLanguageString: CheckFn<LanguageString>;
|
|
17
|
-
|
|
97
|
+
/**
|
|
98
|
+
* A BCP-47 language tag string.
|
|
99
|
+
*
|
|
100
|
+
* @example `"en"`, `"en-US"`, `"zh-Hans"`
|
|
101
|
+
*/
|
|
102
|
+
export type LanguageString = string;
|
|
103
|
+
/**
|
|
104
|
+
* Type guard that checks if a value is a valid NSID string.
|
|
105
|
+
*
|
|
106
|
+
* @param value - The value to check
|
|
107
|
+
* @returns `true` if the value is a valid NSID string
|
|
108
|
+
*/
|
|
18
109
|
export declare const isNsidString: CheckFn<NsidString>;
|
|
19
|
-
export type {
|
|
110
|
+
export type {
|
|
111
|
+
/**
|
|
112
|
+
* A Namespaced Identifier (NSID) string identifying a lexicon.
|
|
113
|
+
*
|
|
114
|
+
* NSIDs use reverse-domain notation to identify schemas.
|
|
115
|
+
*
|
|
116
|
+
* @example `"app.bsky.feed.post"`, `"com.atproto.repo.createRecord"`
|
|
117
|
+
*/
|
|
118
|
+
NsidString, };
|
|
119
|
+
/**
|
|
120
|
+
* Type guard that checks if a value is a valid record key string.
|
|
121
|
+
*
|
|
122
|
+
* @param value - The value to check
|
|
123
|
+
* @returns `true` if the value is a valid record key string
|
|
124
|
+
*/
|
|
20
125
|
export declare const isRecordKeyString: CheckFn<RecordKeyString>;
|
|
21
|
-
export type {
|
|
126
|
+
export type {
|
|
127
|
+
/**
|
|
128
|
+
* A record key string identifying a record within a collection.
|
|
129
|
+
*
|
|
130
|
+
* @example `"3k2..."` (TID format) or `"self"` (literal key)
|
|
131
|
+
*/
|
|
132
|
+
RecordKeyString, };
|
|
133
|
+
/**
|
|
134
|
+
* Type guard that checks if a value is a valid TID string.
|
|
135
|
+
*
|
|
136
|
+
* @param value - The value to check
|
|
137
|
+
* @returns `true` if the value is a valid TID string
|
|
138
|
+
*/
|
|
22
139
|
export declare const isTidString: CheckFn<TidString>;
|
|
23
|
-
export type {
|
|
140
|
+
export type {
|
|
141
|
+
/**
|
|
142
|
+
* A Timestamp Identifier (TID) string.
|
|
143
|
+
*
|
|
144
|
+
* TIDs are time-based identifiers used for record keys.
|
|
145
|
+
*
|
|
146
|
+
* @example `"3k2..."`
|
|
147
|
+
*/
|
|
148
|
+
TidString, };
|
|
149
|
+
/**
|
|
150
|
+
* Type guard that checks if a value is a valid URI string.
|
|
151
|
+
*
|
|
152
|
+
* @param value - The value to check
|
|
153
|
+
* @returns `true` if the value is a valid URI string
|
|
154
|
+
*/
|
|
24
155
|
export declare const isUriString: CheckFn<UriString>;
|
|
156
|
+
export type {
|
|
157
|
+
/**
|
|
158
|
+
* A standard URI string.
|
|
159
|
+
*
|
|
160
|
+
* @example `"https://example.com/path"`
|
|
161
|
+
*/
|
|
162
|
+
UriString, };
|
|
25
163
|
type StringFormats = {
|
|
26
164
|
'at-identifier': AtIdentifierString;
|
|
27
165
|
'at-uri': AtUriString;
|
|
@@ -35,11 +173,106 @@ type StringFormats = {
|
|
|
35
173
|
tid: TidString;
|
|
36
174
|
uri: UriString;
|
|
37
175
|
};
|
|
176
|
+
/**
|
|
177
|
+
* Union type of all valid string format names.
|
|
178
|
+
*/
|
|
38
179
|
export type StringFormat = Extract<keyof StringFormats, string>;
|
|
180
|
+
/**
|
|
181
|
+
* Infers the string type for a given format name.
|
|
182
|
+
*
|
|
183
|
+
* @typeParam F - The format name
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* type Did = InferStringFormat<'did'>
|
|
188
|
+
* // Result: DidString
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
39
191
|
export type InferStringFormat<F extends StringFormat> = F extends StringFormat ? StringFormats[F] : never;
|
|
192
|
+
/**
|
|
193
|
+
* Type guard that checks if a string matches a specific format.
|
|
194
|
+
*
|
|
195
|
+
* @typeParam I - The input string type
|
|
196
|
+
* @typeParam F - The format to check
|
|
197
|
+
* @param input - The string to validate
|
|
198
|
+
* @param format - The format name to validate against
|
|
199
|
+
* @returns `true` if the string matches the format
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```typescript
|
|
203
|
+
* const value: string = 'did:plc:1234...'
|
|
204
|
+
* if (isStringFormat(value, 'did')) {
|
|
205
|
+
* // value is typed as DidString
|
|
206
|
+
* console.log('Valid DID:', value)
|
|
207
|
+
* }
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
40
210
|
export declare function isStringFormat<I extends string, F extends StringFormat>(input: I, format: F): input is I & StringFormats[F];
|
|
211
|
+
/**
|
|
212
|
+
* Asserts that a string matches a specific format, throwing if invalid.
|
|
213
|
+
*
|
|
214
|
+
* @typeParam I - The input string type
|
|
215
|
+
* @typeParam F - The format to check
|
|
216
|
+
* @param input - The string to validate
|
|
217
|
+
* @param format - The format name to validate against
|
|
218
|
+
* @throws {TypeError} If the string doesn't match the format
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```typescript
|
|
222
|
+
* assertStringFormat(value, 'handle')
|
|
223
|
+
* // value is now typed as HandleString
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
41
226
|
export declare function assertStringFormat<I extends string, F extends StringFormat>(input: I, format: F): asserts input is I & StringFormats[F];
|
|
227
|
+
/**
|
|
228
|
+
* Validates and returns a string as the specified format type, throwing if invalid.
|
|
229
|
+
*
|
|
230
|
+
* This is useful when you need to convert a string to a format type in an expression.
|
|
231
|
+
*
|
|
232
|
+
* @typeParam I - The input string type
|
|
233
|
+
* @typeParam F - The format to validate against
|
|
234
|
+
* @param input - The string to validate
|
|
235
|
+
* @param format - The format name to validate against
|
|
236
|
+
* @returns The input typed as the format type
|
|
237
|
+
* @throws {TypeError} If the string doesn't match the format
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* const did = asStringFormat(userInput, 'did')
|
|
242
|
+
* // did is typed as DidString
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
42
245
|
export declare function asStringFormat<I extends string, F extends StringFormat>(input: I, format: F): I & StringFormats[F];
|
|
246
|
+
/**
|
|
247
|
+
* Returns the string as the format type if valid, otherwise returns `undefined`.
|
|
248
|
+
*
|
|
249
|
+
* This is useful for optional validation where you want to handle invalid values
|
|
250
|
+
* without throwing.
|
|
251
|
+
*
|
|
252
|
+
* @typeParam I - The input string type
|
|
253
|
+
* @typeParam F - The format to validate against
|
|
254
|
+
* @param input - The string to validate
|
|
255
|
+
* @param format - The format name to validate against
|
|
256
|
+
* @returns The typed string if valid, otherwise `undefined`
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```typescript
|
|
260
|
+
* const did = ifStringFormat(maybeInvalid, 'did')
|
|
261
|
+
* if (did) {
|
|
262
|
+
* // did is typed as DidString
|
|
263
|
+
* }
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
43
266
|
export declare function ifStringFormat<I extends string, F extends StringFormat>(input: I, format: F): undefined | (I & StringFormats[F]);
|
|
267
|
+
/**
|
|
268
|
+
* Array of all valid string format names.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
272
|
+
* for (const format of STRING_FORMATS) {
|
|
273
|
+
* console.log(format) // 'at-identifier', 'at-uri', 'cid', ...
|
|
274
|
+
* }
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
44
277
|
export declare const STRING_FORMATS: readonly StringFormat[];
|
|
45
278
|
//# sourceMappingURL=string-format.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-format.d.ts","sourceRoot":"","sources":["../../src/core/string-format.ts"],"names":[],"mappings":"AACA,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,SAAS,EACT,YAAY,EACZ,UAAU,EACV,eAAe,EACf,SAAS,EACT,SAAS,EAWV,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;
|
|
1
|
+
{"version":3,"file":"string-format.d.ts","sourceRoot":"","sources":["../../src/core/string-format.ts"],"names":[],"mappings":"AACA,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,SAAS,EACT,YAAY,EACZ,UAAU,EACV,eAAe,EACf,SAAS,EACT,SAAS,EAWV,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;AAMnD;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,EAAE,OAAO,CAAC,kBAAkB,CAAe,CAAA;AAC5E,YAAY;AACV;;;;GAIG;AACH,kBAAkB,GACnB,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,aAAa,EAAE,OAAO,CAAC,WAAW,CAAgB,CAAA;AAC/D,YAAY;AACV;;;;GAIG;AACH,WAAW,GACZ,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,WAAW,EAAoC,OAAO,CAAC,SAAS,CAAC,CAAA;AAC9E;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,CAAA;AAE9B;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,EAAE,OAAO,CAAC,cAAc,CAAmB,CAAA;AACxE,YAAY;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7,85 @@ exports.asStringFormat = asStringFormat;
|
|
|
7
7
|
exports.ifStringFormat = ifStringFormat;
|
|
8
8
|
const lex_data_1 = require("@atproto/lex-data");
|
|
9
9
|
const syntax_1 = require("@atproto/syntax");
|
|
10
|
+
// -----------------------------------------------------------------------------
|
|
11
|
+
// Individual string format types and type guards
|
|
12
|
+
// -----------------------------------------------------------------------------
|
|
13
|
+
/**
|
|
14
|
+
* Type guard that checks if a value is a valid AT identifier (DID or handle).
|
|
15
|
+
*
|
|
16
|
+
* @param value - The value to check
|
|
17
|
+
* @returns `true` if the value is a valid AT identifier
|
|
18
|
+
*/
|
|
10
19
|
exports.isAtIdentifierString = syntax_1.isValidAtIdentifier;
|
|
20
|
+
/**
|
|
21
|
+
* Type guard that checks if a value is a valid AT URI.
|
|
22
|
+
*
|
|
23
|
+
* @param value - The value to check
|
|
24
|
+
* @returns `true` if the value is a valid AT URI
|
|
25
|
+
*/
|
|
11
26
|
exports.isAtUriString = syntax_1.isValidAtUri;
|
|
27
|
+
/**
|
|
28
|
+
* Type guard that checks if a value is a valid CID string.
|
|
29
|
+
*
|
|
30
|
+
* @param value - The value to check
|
|
31
|
+
* @returns `true` if the value is a valid CID string
|
|
32
|
+
*/
|
|
12
33
|
exports.isCidString = ((v) => (0, lex_data_1.validateCidString)(v));
|
|
34
|
+
/**
|
|
35
|
+
* Type guard that checks if a value is a valid datetime string.
|
|
36
|
+
*
|
|
37
|
+
* @param value - The value to check
|
|
38
|
+
* @returns `true` if the value is a valid datetime string
|
|
39
|
+
*/
|
|
13
40
|
exports.isDatetimeString = syntax_1.isValidDatetime;
|
|
41
|
+
/**
|
|
42
|
+
* Type guard that checks if a value is a valid DID string.
|
|
43
|
+
*
|
|
44
|
+
* @param value - The value to check
|
|
45
|
+
* @returns `true` if the value is a valid DID string
|
|
46
|
+
*/
|
|
14
47
|
exports.isDidString = syntax_1.isValidDid;
|
|
48
|
+
/**
|
|
49
|
+
* Type guard that checks if a value is a valid handle string.
|
|
50
|
+
*
|
|
51
|
+
* @param value - The value to check
|
|
52
|
+
* @returns `true` if the value is a valid handle string
|
|
53
|
+
*/
|
|
15
54
|
exports.isHandleString = syntax_1.isValidHandle;
|
|
55
|
+
/**
|
|
56
|
+
* Type guard that checks if a value is a valid BCP-47 language tag.
|
|
57
|
+
*
|
|
58
|
+
* @param value - The value to check
|
|
59
|
+
* @returns `true` if the value is a valid language string
|
|
60
|
+
*/
|
|
16
61
|
exports.isLanguageString = syntax_1.isValidLanguage;
|
|
62
|
+
/**
|
|
63
|
+
* Type guard that checks if a value is a valid NSID string.
|
|
64
|
+
*
|
|
65
|
+
* @param value - The value to check
|
|
66
|
+
* @returns `true` if the value is a valid NSID string
|
|
67
|
+
*/
|
|
17
68
|
exports.isNsidString = syntax_1.isValidNsid;
|
|
69
|
+
/**
|
|
70
|
+
* Type guard that checks if a value is a valid record key string.
|
|
71
|
+
*
|
|
72
|
+
* @param value - The value to check
|
|
73
|
+
* @returns `true` if the value is a valid record key string
|
|
74
|
+
*/
|
|
18
75
|
exports.isRecordKeyString = syntax_1.isValidRecordKey;
|
|
76
|
+
/**
|
|
77
|
+
* Type guard that checks if a value is a valid TID string.
|
|
78
|
+
*
|
|
79
|
+
* @param value - The value to check
|
|
80
|
+
* @returns `true` if the value is a valid TID string
|
|
81
|
+
*/
|
|
19
82
|
exports.isTidString = syntax_1.isValidTid;
|
|
83
|
+
/**
|
|
84
|
+
* Type guard that checks if a value is a valid URI string.
|
|
85
|
+
*
|
|
86
|
+
* @param value - The value to check
|
|
87
|
+
* @returns `true` if the value is a valid URI string
|
|
88
|
+
*/
|
|
20
89
|
exports.isUriString = syntax_1.isValidUri;
|
|
21
90
|
const stringFormatVerifiers = /*#__PURE__*/ Object.freeze({
|
|
22
91
|
__proto__: null,
|
|
@@ -32,6 +101,24 @@ const stringFormatVerifiers = /*#__PURE__*/ Object.freeze({
|
|
|
32
101
|
tid: exports.isTidString,
|
|
33
102
|
uri: exports.isUriString,
|
|
34
103
|
});
|
|
104
|
+
/**
|
|
105
|
+
* Type guard that checks if a string matches a specific format.
|
|
106
|
+
*
|
|
107
|
+
* @typeParam I - The input string type
|
|
108
|
+
* @typeParam F - The format to check
|
|
109
|
+
* @param input - The string to validate
|
|
110
|
+
* @param format - The format name to validate against
|
|
111
|
+
* @returns `true` if the string matches the format
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* const value: string = 'did:plc:1234...'
|
|
116
|
+
* if (isStringFormat(value, 'did')) {
|
|
117
|
+
* // value is typed as DidString
|
|
118
|
+
* console.log('Valid DID:', value)
|
|
119
|
+
* }
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
35
122
|
/*@__NO_SIDE_EFFECTS__*/
|
|
36
123
|
function isStringFormat(input, format) {
|
|
37
124
|
const formatVerifier = stringFormatVerifiers[format];
|
|
@@ -40,21 +127,84 @@ function isStringFormat(input, format) {
|
|
|
40
127
|
throw new TypeError(`Unknown string format: ${format}`);
|
|
41
128
|
return formatVerifier(input);
|
|
42
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Asserts that a string matches a specific format, throwing if invalid.
|
|
132
|
+
*
|
|
133
|
+
* @typeParam I - The input string type
|
|
134
|
+
* @typeParam F - The format to check
|
|
135
|
+
* @param input - The string to validate
|
|
136
|
+
* @param format - The format name to validate against
|
|
137
|
+
* @throws {TypeError} If the string doesn't match the format
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```typescript
|
|
141
|
+
* assertStringFormat(value, 'handle')
|
|
142
|
+
* // value is now typed as HandleString
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
43
145
|
/*@__NO_SIDE_EFFECTS__*/
|
|
44
146
|
function assertStringFormat(input, format) {
|
|
45
147
|
if (!isStringFormat(input, format)) {
|
|
46
148
|
throw new TypeError(`Invalid string format (${format}): ${input}`);
|
|
47
149
|
}
|
|
48
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Validates and returns a string as the specified format type, throwing if invalid.
|
|
153
|
+
*
|
|
154
|
+
* This is useful when you need to convert a string to a format type in an expression.
|
|
155
|
+
*
|
|
156
|
+
* @typeParam I - The input string type
|
|
157
|
+
* @typeParam F - The format to validate against
|
|
158
|
+
* @param input - The string to validate
|
|
159
|
+
* @param format - The format name to validate against
|
|
160
|
+
* @returns The input typed as the format type
|
|
161
|
+
* @throws {TypeError} If the string doesn't match the format
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* const did = asStringFormat(userInput, 'did')
|
|
166
|
+
* // did is typed as DidString
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
49
169
|
/*@__NO_SIDE_EFFECTS__*/
|
|
50
170
|
function asStringFormat(input, format) {
|
|
51
171
|
assertStringFormat(input, format);
|
|
52
172
|
return input;
|
|
53
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Returns the string as the format type if valid, otherwise returns `undefined`.
|
|
176
|
+
*
|
|
177
|
+
* This is useful for optional validation where you want to handle invalid values
|
|
178
|
+
* without throwing.
|
|
179
|
+
*
|
|
180
|
+
* @typeParam I - The input string type
|
|
181
|
+
* @typeParam F - The format to validate against
|
|
182
|
+
* @param input - The string to validate
|
|
183
|
+
* @param format - The format name to validate against
|
|
184
|
+
* @returns The typed string if valid, otherwise `undefined`
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* const did = ifStringFormat(maybeInvalid, 'did')
|
|
189
|
+
* if (did) {
|
|
190
|
+
* // did is typed as DidString
|
|
191
|
+
* }
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
54
194
|
/*@__NO_SIDE_EFFECTS__*/
|
|
55
195
|
function ifStringFormat(input, format) {
|
|
56
196
|
return isStringFormat(input, format) ? input : undefined;
|
|
57
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* Array of all valid string format names.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```typescript
|
|
203
|
+
* for (const format of STRING_FORMATS) {
|
|
204
|
+
* console.log(format) // 'at-identifier', 'at-uri', 'cid', ...
|
|
205
|
+
* }
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
58
208
|
exports.STRING_FORMATS = Object.freeze(
|
|
59
209
|
/*#__PURE__*/ Object.keys(stringFormatVerifiers));
|
|
60
210
|
//# sourceMappingURL=string-format.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-format.js","sourceRoot":"","sources":["../../src/core/string-format.ts"],"names":[],"mappings":";;;AAoGA,wCASC;AAGD,gDAOC;AAGD,wCAMC;AAGD,wCAKC;AAxID,gDAAqD;AACrD,4CAoBwB;AAMX,QAAA,oBAAoB,GAAgC,4BAAW,CAAA;AAG/D,QAAA,aAAa,GAAyB,qBAAY,CAAA;AAGlD,QAAA,WAAW,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAA,4BAAiB,EAAC,CAAC,CAAC,CAAuB,CAAA;AAGjE,QAAA,gBAAgB,GAA4B,wBAAe,CAAA;AAG3D,QAAA,WAAW,GAAuB,mBAAU,CAAA;AAG5C,QAAA,cAAc,GAA0B,sBAAa,CAAA;AAGrD,QAAA,gBAAgB,GAAG,wBAA0C,CAAA;AAG7D,QAAA,YAAY,GAAwB,oBAAW,CAAA;AAG/C,QAAA,iBAAiB,GAA6B,yBAAgB,CAAA;AAG9D,QAAA,WAAW,GAAuB,mBAAU,CAAA;AAG5C,QAAA,WAAW,GAAuB,mBAAU,CAAA;AAoBzD,MAAM,qBAAqB,GAEvB,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC;IAC9B,SAAS,EAAE,IAAI;IAEf,eAAe,EAAE,4BAAoB;IACrC,QAAQ,EAAE,qBAAa;IACvB,GAAG,EAAE,mBAAW;IAChB,QAAQ,EAAE,wBAAgB;IAC1B,GAAG,EAAE,mBAAW;IAChB,MAAM,EAAE,sBAAc;IACtB,QAAQ,EAAE,wBAAgB;IAC1B,IAAI,EAAE,oBAAY;IAClB,YAAY,EAAE,yBAAiB;IAC/B,GAAG,EAAE,mBAAW;IAChB,GAAG,EAAE,mBAAW;CACjB,CAAC,CAAA;AAMF,wBAAwB;AACxB,SAAgB,cAAc,CAC5B,KAAQ,EACR,MAAS;IAET,MAAM,cAAc,GAAG,qBAAqB,CAAC,MAAM,CAAC,CAAA;IACpD,aAAa;IACb,IAAI,CAAC,cAAc;QAAE,MAAM,IAAI,SAAS,CAAC,0BAA0B,MAAM,EAAE,CAAC,CAAA;IAE5E,OAAO,cAAc,CAAC,KAAK,CAAC,CAAA;AAC9B,CAAC;AAED,wBAAwB;AACxB,SAAgB,kBAAkB,CAChC,KAAQ,EACR,MAAS;IAET,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,SAAS,CAAC,0BAA0B,MAAM,MAAM,KAAK,EAAE,CAAC,CAAA;IACpE,CAAC;AACH,CAAC;AAED,wBAAwB;AACxB,SAAgB,cAAc,CAC5B,KAAQ,EACR,MAAS;IAET,kBAAkB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;IACjC,OAAO,KAAK,CAAA;AACd,CAAC;AAED,wBAAwB;AACxB,SAAgB,cAAc,CAC5B,KAAQ,EACR,MAAS;IAET,OAAO,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AAC1D,CAAC;AAEY,QAAA,cAAc,GAAiB,MAAM,CAAC,MAAM;AACvD,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,qBAAqB,CAAC,CACtB,CAAA","sourcesContent":["import { validateCidString } from '@atproto/lex-data'\nimport {\n AtIdentifierString,\n AtUriString,\n DatetimeString,\n DidString,\n HandleString,\n NsidString,\n RecordKeyString,\n TidString,\n UriString,\n isValidAtIdentifier as isValidAtId,\n isValidAtUri,\n isValidDatetime,\n isValidDid,\n isValidHandle,\n isValidLanguage,\n isValidNsid,\n isValidRecordKey,\n isValidTid,\n isValidUri,\n} from '@atproto/syntax'\nimport { CheckFn } from '../util/assertion-util.js'\n\n// Expose all individual string format types and type guards\n\nexport type { AtIdentifierString }\nexport const isAtIdentifierString: CheckFn<AtIdentifierString> = isValidAtId\n\nexport type { AtUriString }\nexport const isAtUriString: CheckFn<AtUriString> = isValidAtUri\n\nexport type CidString = string\nexport const isCidString = ((v) => validateCidString(v)) as CheckFn<CidString>\n\nexport type { DatetimeString }\nexport const isDatetimeString: CheckFn<DatetimeString> = isValidDatetime\n\nexport type { DidString }\nexport const isDidString: CheckFn<DidString> = isValidDid\n\nexport type { HandleString }\nexport const isHandleString: CheckFn<HandleString> = isValidHandle\n\nexport type LanguageString = string\nexport const isLanguageString = isValidLanguage as CheckFn<LanguageString>\n\nexport type { NsidString }\nexport const isNsidString: CheckFn<NsidString> = isValidNsid\n\nexport type { RecordKeyString }\nexport const isRecordKeyString: CheckFn<RecordKeyString> = isValidRecordKey\n\nexport type { TidString }\nexport const isTidString: CheckFn<TidString> = isValidTid\n\nexport type { UriString }\nexport const isUriString: CheckFn<UriString> = isValidUri\n\n// String format registry (maps format names to their types and type guards)\n\ntype StringFormats = {\n 'at-identifier': AtIdentifierString\n 'at-uri': AtUriString\n cid: CidString\n datetime: DatetimeString\n did: DidString\n handle: HandleString\n language: LanguageString\n nsid: NsidString\n 'record-key': RecordKeyString\n tid: TidString\n uri: UriString\n}\n\nexport type StringFormat = Extract<keyof StringFormats, string>\n\nconst stringFormatVerifiers: {\n readonly [K in StringFormat]: CheckFn<StringFormats[K]>\n} = /*#__PURE__*/ Object.freeze({\n __proto__: null,\n\n 'at-identifier': isAtIdentifierString,\n 'at-uri': isAtUriString,\n cid: isCidString,\n datetime: isDatetimeString,\n did: isDidString,\n handle: isHandleString,\n language: isLanguageString,\n nsid: isNsidString,\n 'record-key': isRecordKeyString,\n tid: isTidString,\n uri: isUriString,\n})\n\nexport type InferStringFormat<F extends StringFormat> = F extends StringFormat\n ? StringFormats[F]\n : never\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function isStringFormat<I extends string, F extends StringFormat>(\n input: I,\n format: F,\n): input is I & StringFormats[F] {\n const formatVerifier = stringFormatVerifiers[format]\n // Fool-proof\n if (!formatVerifier) throw new TypeError(`Unknown string format: ${format}`)\n\n return formatVerifier(input)\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function assertStringFormat<I extends string, F extends StringFormat>(\n input: I,\n format: F,\n): asserts input is I & StringFormats[F] {\n if (!isStringFormat(input, format)) {\n throw new TypeError(`Invalid string format (${format}): ${input}`)\n }\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function asStringFormat<I extends string, F extends StringFormat>(\n input: I,\n format: F,\n): I & StringFormats[F] {\n assertStringFormat(input, format)\n return input\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function ifStringFormat<I extends string, F extends StringFormat>(\n input: I,\n format: F,\n): undefined | (I & StringFormats[F]) {\n return isStringFormat(input, format) ? input : undefined\n}\n\nexport const STRING_FORMATS = /*#__PURE__*/ Object.freeze(\n /*#__PURE__*/ Object.keys(stringFormatVerifiers),\n) as readonly StringFormat[]\n"]}
|
|
1
|
+
{"version":3,"file":"string-format.js","sourceRoot":"","sources":["../../src/core/string-format.ts"],"names":[],"mappings":";;;AA2RA,wCASC;AAkBD,gDAOC;AAqBD,wCAMC;AAuBD,wCAKC;AApXD,gDAAqD;AACrD,4CAoBwB;AAGxB,gFAAgF;AAChF,iDAAiD;AACjD,gFAAgF;AAEhF;;;;;GAKG;AACU,QAAA,oBAAoB,GAAgC,4BAAW,CAAA;AAU5E;;;;;GAKG;AACU,QAAA,aAAa,GAAyB,qBAAY,CAAA;AAU/D;;;;;GAKG;AACU,QAAA,WAAW,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAA,4BAAiB,EAAC,CAAC,CAAC,CAAuB,CAAA;AAU9E;;;;;GAKG;AACU,QAAA,gBAAgB,GAA4B,wBAAe,C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CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,SAAS,CAAC,0BAA0B,MAAM,MAAM,KAAK,EAAE,CAAC,CAAA;IACpE,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAwB;AACxB,SAAgB,cAAc,CAC5B,KAAQ,EACR,MAAS;IAET,kBAAkB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;IACjC,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAwB;AACxB,SAAgB,cAAc,CAC5B,KAAQ,EACR,MAAS;IAET,OAAO,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AAC1D,CAAC;AAED;;;;;;;;;GASG;AACU,QAAA,cAAc,GAAiB,MAAM,CAAC,MAAM;AACvD,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,qBAAqB,CAAC,CACtB,CAAA","sourcesContent":["import { validateCidString } from '@atproto/lex-data'\nimport {\n AtIdentifierString,\n AtUriString,\n DatetimeString,\n DidString,\n HandleString,\n NsidString,\n RecordKeyString,\n TidString,\n UriString,\n isValidAtIdentifier as isValidAtId,\n isValidAtUri,\n isValidDatetime,\n isValidDid,\n isValidHandle,\n isValidLanguage,\n isValidNsid,\n isValidRecordKey,\n isValidTid,\n isValidUri,\n} from '@atproto/syntax'\nimport { CheckFn } from '../util/assertion-util.js'\n\n// -----------------------------------------------------------------------------\n// Individual string format types and type guards\n// -----------------------------------------------------------------------------\n\n/**\n * Type guard that checks if a value is a valid AT identifier (DID or handle).\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid AT identifier\n */\nexport const isAtIdentifierString: CheckFn<AtIdentifierString> = isValidAtId\nexport type {\n /**\n * An AT identifier string - either a DID or a handle.\n *\n * @example `\"did:plc:1234...\"` or `\"alice.bsky.social\"`\n */\n AtIdentifierString,\n}\n\n/**\n * Type guard that checks if a value is a valid AT URI.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid AT URI\n */\nexport const isAtUriString: CheckFn<AtUriString> = isValidAtUri\nexport type {\n /**\n * An AT URI string pointing to a resource in the AT Protocol network.\n *\n * @example `\"at://did:plc:1234.../app.bsky.feed.post/3k2...\"`\n */\n AtUriString,\n}\n\n/**\n * Type guard that checks if a value is a valid CID string.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid CID string\n */\nexport const isCidString = ((v) => validateCidString(v)) as CheckFn<CidString>\n/**\n * A Content Identifier (CID) string.\n *\n * CIDs are self-describing content addresses used to identify data by its hash.\n *\n * @example `\"bafyreig...\"`\n */\nexport type CidString = string\n\n/**\n * Type guard that checks if a value is a valid datetime string.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid datetime string\n */\nexport const isDatetimeString: CheckFn<DatetimeString> = isValidDatetime\nexport type {\n /**\n * An ISO 8601 datetime string.\n *\n * @example `\"2024-01-15T12:30:00.000Z\"`\n */\n DatetimeString,\n}\n\n/**\n * Type guard that checks if a value is a valid DID string.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid DID string\n */\nexport const isDidString: CheckFn<DidString> = isValidDid\nexport type {\n /**\n * A Decentralized Identifier (DID) string.\n *\n * DIDs are globally unique identifiers that don't require a central authority.\n *\n * @example `\"did:plc:1234abcd...\"` or `\"did:web:example.com\"`\n */\n DidString,\n}\n\n/**\n * Type guard that checks if a value is a valid handle string.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid handle string\n */\nexport const isHandleString: CheckFn<HandleString> = isValidHandle\nexport type {\n /**\n * A handle string - a human-readable identifier for users.\n *\n * @example `\"alice.bsky.social\"` or `\"bob.example.com\"`\n */\n HandleString,\n}\n\n/**\n * Type guard that checks if a value is a valid BCP-47 language tag.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid language string\n */\nexport const isLanguageString = isValidLanguage as CheckFn<LanguageString>\n/**\n * A BCP-47 language tag string.\n *\n * @example `\"en\"`, `\"en-US\"`, `\"zh-Hans\"`\n */\nexport type LanguageString = string\n\n/**\n * Type guard that checks if a value is a valid NSID string.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid NSID string\n */\nexport const isNsidString: CheckFn<NsidString> = isValidNsid\nexport type {\n /**\n * A Namespaced Identifier (NSID) string identifying a lexicon.\n *\n * NSIDs use reverse-domain notation to identify schemas.\n *\n * @example `\"app.bsky.feed.post\"`, `\"com.atproto.repo.createRecord\"`\n */\n NsidString,\n}\n\n/**\n * Type guard that checks if a value is a valid record key string.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid record key string\n */\nexport const isRecordKeyString: CheckFn<RecordKeyString> = isValidRecordKey\nexport type {\n /**\n * A record key string identifying a record within a collection.\n *\n * @example `\"3k2...\"` (TID format) or `\"self\"` (literal key)\n */\n RecordKeyString,\n}\n\n/**\n * Type guard that checks if a value is a valid TID string.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid TID string\n */\nexport const isTidString: CheckFn<TidString> = isValidTid\nexport type {\n /**\n * A Timestamp Identifier (TID) string.\n *\n * TIDs are time-based identifiers used for record keys.\n *\n * @example `\"3k2...\"`\n */\n TidString,\n}\n\n/**\n * Type guard that checks if a value is a valid URI string.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid URI string\n */\nexport const isUriString: CheckFn<UriString> = isValidUri\nexport type {\n /**\n * A standard URI string.\n *\n * @example `\"https://example.com/path\"`\n */\n UriString,\n}\n\n// -----------------------------------------------------------------------------\n// String format registry\n// -----------------------------------------------------------------------------\n\ntype StringFormats = {\n 'at-identifier': AtIdentifierString\n 'at-uri': AtUriString\n cid: CidString\n datetime: DatetimeString\n did: DidString\n handle: HandleString\n language: LanguageString\n nsid: NsidString\n 'record-key': RecordKeyString\n tid: TidString\n uri: UriString\n}\n\n/**\n * Union type of all valid string format names.\n */\nexport type StringFormat = Extract<keyof StringFormats, string>\n\nconst stringFormatVerifiers: {\n readonly [K in StringFormat]: CheckFn<StringFormats[K]>\n} = /*#__PURE__*/ Object.freeze({\n __proto__: null,\n\n 'at-identifier': isAtIdentifierString,\n 'at-uri': isAtUriString,\n cid: isCidString,\n datetime: isDatetimeString,\n did: isDidString,\n handle: isHandleString,\n language: isLanguageString,\n nsid: isNsidString,\n 'record-key': isRecordKeyString,\n tid: isTidString,\n uri: isUriString,\n})\n\n/**\n * Infers the string type for a given format name.\n *\n * @typeParam F - The format name\n *\n * @example\n * ```typescript\n * type Did = InferStringFormat<'did'>\n * // Result: DidString\n * ```\n */\nexport type InferStringFormat<F extends StringFormat> = F extends StringFormat\n ? StringFormats[F]\n : never\n\n/**\n * Type guard that checks if a string matches a specific format.\n *\n * @typeParam I - The input string type\n * @typeParam F - The format to check\n * @param input - The string to validate\n * @param format - The format name to validate against\n * @returns `true` if the string matches the format\n *\n * @example\n * ```typescript\n * const value: string = 'did:plc:1234...'\n * if (isStringFormat(value, 'did')) {\n * // value is typed as DidString\n * console.log('Valid DID:', value)\n * }\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function isStringFormat<I extends string, F extends StringFormat>(\n input: I,\n format: F,\n): input is I & StringFormats[F] {\n const formatVerifier = stringFormatVerifiers[format]\n // Fool-proof\n if (!formatVerifier) throw new TypeError(`Unknown string format: ${format}`)\n\n return formatVerifier(input)\n}\n\n/**\n * Asserts that a string matches a specific format, throwing if invalid.\n *\n * @typeParam I - The input string type\n * @typeParam F - The format to check\n * @param input - The string to validate\n * @param format - The format name to validate against\n * @throws {TypeError} If the string doesn't match the format\n *\n * @example\n * ```typescript\n * assertStringFormat(value, 'handle')\n * // value is now typed as HandleString\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function assertStringFormat<I extends string, F extends StringFormat>(\n input: I,\n format: F,\n): asserts input is I & StringFormats[F] {\n if (!isStringFormat(input, format)) {\n throw new TypeError(`Invalid string format (${format}): ${input}`)\n }\n}\n\n/**\n * Validates and returns a string as the specified format type, throwing if invalid.\n *\n * This is useful when you need to convert a string to a format type in an expression.\n *\n * @typeParam I - The input string type\n * @typeParam F - The format to validate against\n * @param input - The string to validate\n * @param format - The format name to validate against\n * @returns The input typed as the format type\n * @throws {TypeError} If the string doesn't match the format\n *\n * @example\n * ```typescript\n * const did = asStringFormat(userInput, 'did')\n * // did is typed as DidString\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function asStringFormat<I extends string, F extends StringFormat>(\n input: I,\n format: F,\n): I & StringFormats[F] {\n assertStringFormat(input, format)\n return input\n}\n\n/**\n * Returns the string as the format type if valid, otherwise returns `undefined`.\n *\n * This is useful for optional validation where you want to handle invalid values\n * without throwing.\n *\n * @typeParam I - The input string type\n * @typeParam F - The format to validate against\n * @param input - The string to validate\n * @param format - The format name to validate against\n * @returns The typed string if valid, otherwise `undefined`\n *\n * @example\n * ```typescript\n * const did = ifStringFormat(maybeInvalid, 'did')\n * if (did) {\n * // did is typed as DidString\n * }\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function ifStringFormat<I extends string, F extends StringFormat>(\n input: I,\n format: F,\n): undefined | (I & StringFormats[F]) {\n return isStringFormat(input, format) ? input : undefined\n}\n\n/**\n * Array of all valid string format names.\n *\n * @example\n * ```typescript\n * for (const format of STRING_FORMATS) {\n * console.log(format) // 'at-identifier', 'at-uri', 'cid', ...\n * }\n * ```\n */\nexport const STRING_FORMATS = /*#__PURE__*/ Object.freeze(\n /*#__PURE__*/ Object.keys(stringFormatVerifiers),\n) as readonly StringFormat[]\n"]}
|