@atproto/lex-schema 0.0.12 → 0.0.14
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 +53 -0
- package/dist/core/schema.d.ts +27 -36
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +68 -54
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +1 -14
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +12 -9
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/validation-error.d.ts +5 -5
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +8 -8
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.js +3 -1
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +16 -8
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +24 -6
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +10 -11
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +1 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +2 -1
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +4 -2
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +5 -2
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +1 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +2 -1
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +1 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +2 -1
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +1 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +2 -1
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +1 -0
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +1 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +1 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +2 -1
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +1 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +2 -1
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +1 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +1 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +1 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +2 -1
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +1 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +1 -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 +1 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +1 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +1 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +2 -1
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +1 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +2 -1
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +1 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +1 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +1 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +2 -1
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +1 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +1 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +14 -10
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +87 -24
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +3 -3
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/record.d.ts +21 -19
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +22 -12
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +1 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +1 -0
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/regexp.d.ts +1 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +2 -1
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +22 -6
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +16 -9
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/token.d.ts +1 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +2 -1
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +20 -16
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +23 -13
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +1 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +1 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +1 -0
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +2 -1
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +1 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +2 -1
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown.d.ts +1 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +1 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +1 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +1 -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/schema.ts +76 -62
- package/src/core/string-format.ts +14 -17
- package/src/core/validation-error.ts +10 -10
- package/src/core/validation-issue.ts +3 -2
- package/src/core/validator.ts +32 -12
- package/src/helpers.test.ts +1 -1
- package/src/helpers.ts +53 -19
- package/src/schema/array.ts +3 -1
- package/src/schema/blob.ts +4 -1
- package/src/schema/boolean.ts +3 -1
- package/src/schema/bytes.ts +3 -1
- package/src/schema/cid.ts +3 -1
- package/src/schema/custom.ts +2 -0
- package/src/schema/dict.ts +3 -1
- package/src/schema/discriminated-union.ts +3 -1
- package/src/schema/enum.ts +2 -0
- package/src/schema/integer.ts +3 -1
- package/src/schema/intersection.ts +2 -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 +2 -0
- package/src/schema/never.ts +3 -1
- package/src/schema/null.ts +3 -1
- package/src/schema/nullable.ts +2 -0
- package/src/schema/object.ts +3 -1
- package/src/schema/optional.ts +2 -0
- package/src/schema/params.test.ts +98 -43
- package/src/schema/params.ts +136 -39
- package/src/schema/payload.test.ts +2 -2
- package/src/schema/payload.ts +3 -4
- package/src/schema/record.ts +38 -22
- package/src/schema/ref.ts +2 -0
- package/src/schema/regexp.ts +3 -1
- package/src/schema/string.test.ts +99 -2
- package/src/schema/string.ts +58 -15
- package/src/schema/token.ts +3 -1
- package/src/schema/typed-object.test.ts +38 -0
- package/src/schema/typed-object.ts +40 -24
- package/src/schema/typed-ref.ts +2 -0
- package/src/schema/typed-union.ts +3 -1
- package/src/schema/union.ts +4 -2
- package/src/schema/unknown.ts +2 -0
- package/src/schema/with-default.ts +2 -0
- package/src/schema.ts +2 -1
- package/src/util/if-any.ts +3 -0
- package/dist/schema/unknown-object.d.ts +0 -42
- package/dist/schema/unknown-object.d.ts.map +0 -1
- package/dist/schema/unknown-object.js +0 -50
- package/dist/schema/unknown-object.js.map +0 -1
- package/src/schema/unknown-object.ts +0 -53
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,58 @@
|
|
|
1
1
|
# @atproto/lex-schema
|
|
2
2
|
|
|
3
|
+
## 0.0.14
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4688](https://github.com/bluesky-social/atproto/pull/4688) [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Allow `WWWAuthenticate` to have multiple challenges for the same scheme
|
|
8
|
+
|
|
9
|
+
- [#4689](https://github.com/bluesky-social/atproto/pull/4689) [`f7c2610`](https://github.com/bluesky-social/atproto/commit/f7c26103a6d4e24e5bedbb6fd908be140420e0dd) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Export more `DatetimeString` utilities from `@atproto/syntax`
|
|
10
|
+
|
|
11
|
+
- [#4688](https://github.com/bluesky-social/atproto/pull/4688) [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Make `$` utility function bound to the instance, allowing to use them without the schema as `this` context
|
|
12
|
+
|
|
13
|
+
- [#4688](https://github.com/bluesky-social/atproto/pull/4688) [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Ignore empty strings from `params` parsing
|
|
14
|
+
|
|
15
|
+
- [#4688](https://github.com/bluesky-social/atproto/pull/4688) [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Renaming `ValidationError` to `LexValidationError` for consistency with `LexError` & sub-classes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies [[`f7c2610`](https://github.com/bluesky-social/atproto/commit/f7c26103a6d4e24e5bedbb6fd908be140420e0dd), [`f7c2610`](https://github.com/bluesky-social/atproto/commit/f7c26103a6d4e24e5bedbb6fd908be140420e0dd), [`f7c2610`](https://github.com/bluesky-social/atproto/commit/f7c26103a6d4e24e5bedbb6fd908be140420e0dd), [`f7c2610`](https://github.com/bluesky-social/atproto/commit/f7c26103a6d4e24e5bedbb6fd908be140420e0dd), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7), [`f7c2610`](https://github.com/bluesky-social/atproto/commit/f7c26103a6d4e24e5bedbb6fd908be140420e0dd), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7), [`f7c2610`](https://github.com/bluesky-social/atproto/commit/f7c26103a6d4e24e5bedbb6fd908be140420e0dd), [`52834ab`](https://github.com/bluesky-social/atproto/commit/52834aba182da8df3611fd9dff924e6c6a3973a7)]:
|
|
18
|
+
- @atproto/syntax@0.5.0
|
|
19
|
+
- @atproto/lex-data@0.0.13
|
|
20
|
+
|
|
21
|
+
## 0.0.13
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- [#4654](https://github.com/bluesky-social/atproto/pull/4654) [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Params cannot be arrays of mixed string, number and booleans (items can only be of one type)
|
|
26
|
+
|
|
27
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Improve typeing of `string()` schemas
|
|
28
|
+
|
|
29
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Fix import statement
|
|
30
|
+
|
|
31
|
+
- [#4654](https://github.com/bluesky-social/atproto/pull/4654) [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Schemas now define a `type` property that allows to better discriminate them
|
|
32
|
+
|
|
33
|
+
- [#4654](https://github.com/bluesky-social/atproto/pull/4654) [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add support for `knownValues` in string schemas
|
|
34
|
+
|
|
35
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Expose `MaybeTypedObject` type utility (retrun type of `TypedObjectSchema.isTypeOf` method)
|
|
36
|
+
|
|
37
|
+
- [#4654](https://github.com/bluesky-social/atproto/pull/4654) [`39dea03`](https://github.com/bluesky-social/atproto/commit/39dea03c417a1da069962560505427a7aa25ad7a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Properly support "enum" and "const" schemas in params
|
|
38
|
+
|
|
39
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Allow using `InferMethod*` type helpers without any argument
|
|
40
|
+
|
|
41
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Minor optimization when checking payload encoding
|
|
42
|
+
|
|
43
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Improve error messages when encountering an unexpected legacy blob
|
|
44
|
+
|
|
45
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add new `issueUnexpectedType` method on `ValidationContext` to better allow creation issues with multiple allowed types
|
|
46
|
+
|
|
47
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Update validation message for $typed object to match working of `@atproto/lexicon`
|
|
48
|
+
|
|
49
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Replace `UnknownObject` with `LexMap` to improve consistency with underlying data structure being validated
|
|
50
|
+
|
|
51
|
+
- [#4660](https://github.com/bluesky-social/atproto/pull/4660) [`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Expose `TypedRecord` type utility (result of `RecordSchema.isTypeOf` methid
|
|
52
|
+
|
|
53
|
+
- Updated dependencies [[`ea5df64`](https://github.com/bluesky-social/atproto/commit/ea5df64db9e408d2b320f5f75eb2878aef6bc6df)]:
|
|
54
|
+
- @atproto/lex-data@0.0.12
|
|
55
|
+
|
|
3
56
|
## 0.0.12
|
|
4
57
|
|
|
5
58
|
### Patch Changes
|
package/dist/core/schema.d.ts
CHANGED
|
@@ -3,12 +3,12 @@ import { InferInput, InferOutput, ValidationContext, ValidationOptions, Validati
|
|
|
3
3
|
* Options for parsing operations.
|
|
4
4
|
* Excludes the `mode` option as it is implicitly set to `"parse"`.
|
|
5
5
|
*/
|
|
6
|
-
type ParseOptions = Omit<ValidationOptions, 'mode'>;
|
|
6
|
+
export type ParseOptions = Omit<ValidationOptions, 'mode'>;
|
|
7
7
|
/**
|
|
8
8
|
* Options for validation operations.
|
|
9
9
|
* Excludes the `mode` option as it is implicitly set to `"validate"`.
|
|
10
10
|
*/
|
|
11
|
-
type ValidateOptions = Omit<ValidationOptions, 'mode'>;
|
|
11
|
+
export type ValidateOptions = Omit<ValidationOptions, 'mode'>;
|
|
12
12
|
/**
|
|
13
13
|
* Internal type structure for schema type inference.
|
|
14
14
|
*
|
|
@@ -47,7 +47,7 @@ export interface SchemaInternals<out TInput = unknown, out TOutput = TInput> {
|
|
|
47
47
|
* class MySchema extends Schema<string> {
|
|
48
48
|
* validateInContext(input: unknown, ctx: ValidationContext): ValidationResult {
|
|
49
49
|
* if (typeof input !== 'string') {
|
|
50
|
-
* return ctx.
|
|
50
|
+
* return ctx.issueUnexpectedType(input, 'string')
|
|
51
51
|
* }
|
|
52
52
|
* return ctx.success(input)
|
|
53
53
|
* }
|
|
@@ -55,7 +55,7 @@ export interface SchemaInternals<out TInput = unknown, out TOutput = TInput> {
|
|
|
55
55
|
*
|
|
56
56
|
* const schema = new MySchema()
|
|
57
57
|
* schema.assert('hello') // OK
|
|
58
|
-
* schema.assert(123) // Throws
|
|
58
|
+
* schema.assert(123) // Throws LexValidationError
|
|
59
59
|
* schema.matches('hello') // true
|
|
60
60
|
* schema.matches(123) // false
|
|
61
61
|
* ```
|
|
@@ -68,6 +68,7 @@ export declare abstract class Schema<out TInput = unknown, out TOutput = TInput,
|
|
|
68
68
|
* @internal
|
|
69
69
|
*/
|
|
70
70
|
readonly ['__lex']: TInternals;
|
|
71
|
+
abstract readonly type: string;
|
|
71
72
|
/**
|
|
72
73
|
* Performs validation of the input value within a validation context.
|
|
73
74
|
*
|
|
@@ -141,12 +142,12 @@ export declare abstract class Schema<out TInput = unknown, out TOutput = TInput,
|
|
|
141
142
|
*
|
|
142
143
|
* Unlike {@link validate}, this method allows the schema to transform
|
|
143
144
|
* the input value (e.g., applying default values, type coercion).
|
|
144
|
-
* Throws a {@link
|
|
145
|
+
* Throws a {@link LexValidationError} if the input is invalid.
|
|
145
146
|
*
|
|
146
147
|
* @param input - The value to parse
|
|
147
148
|
* @param options - Optional parsing configuration
|
|
148
149
|
* @returns The parsed and potentially transformed value
|
|
149
|
-
* @throws {
|
|
150
|
+
* @throws {LexValidationError} If the input fails validation
|
|
150
151
|
*
|
|
151
152
|
* @example
|
|
152
153
|
* ```typescript
|
|
@@ -181,13 +182,13 @@ export declare abstract class Schema<out TInput = unknown, out TOutput = TInput,
|
|
|
181
182
|
*
|
|
182
183
|
* Unlike {@link parse}, this method requires the input to exactly match
|
|
183
184
|
* the schema without any transformations (no defaults applied, no coercion).
|
|
184
|
-
* Throws a {@link
|
|
185
|
+
* Throws a {@link LexValidationError} if the input is invalid or would require transformation.
|
|
185
186
|
*
|
|
186
187
|
* @typeParam I - The input type (preserved in the return type)
|
|
187
188
|
* @param input - The value to validate
|
|
188
189
|
* @param options - Optional validation configuration
|
|
189
190
|
* @returns The validated input with narrowed type
|
|
190
|
-
* @throws {
|
|
191
|
+
* @throws {LexValidationError} If the input fails validation or requires transformation
|
|
191
192
|
*
|
|
192
193
|
* @example
|
|
193
194
|
* ```typescript
|
|
@@ -219,59 +220,49 @@ export declare abstract class Schema<out TInput = unknown, out TOutput = TInput,
|
|
|
219
220
|
*/
|
|
220
221
|
safeValidate<I>(input: I, options?: ValidateOptions): ValidationResult<I & InferInput<this>>;
|
|
221
222
|
/**
|
|
222
|
-
*
|
|
223
|
-
*
|
|
223
|
+
* Bound alias for {@link assert} for compatibility with generated utilities.
|
|
224
224
|
* @see {@link assert}
|
|
225
225
|
*/
|
|
226
|
-
$assert(
|
|
226
|
+
get $assert(): typeof this.assert;
|
|
227
227
|
/**
|
|
228
|
-
*
|
|
229
|
-
*
|
|
228
|
+
* Bound alias for {@link check} for compatibility with generated utilities.
|
|
230
229
|
* @see {@link check}
|
|
231
230
|
*/
|
|
232
|
-
$check(
|
|
231
|
+
get $check(): typeof this.check;
|
|
233
232
|
/**
|
|
234
|
-
*
|
|
235
|
-
*
|
|
233
|
+
* Bound alias for {@link cast} for compatibility with generated utilities.
|
|
236
234
|
* @see {@link cast}
|
|
237
235
|
*/
|
|
238
|
-
$cast
|
|
236
|
+
get $cast(): typeof this.cast;
|
|
239
237
|
/**
|
|
240
|
-
*
|
|
241
|
-
*
|
|
238
|
+
* Bound alias for {@link matches} for compatibility with generated utilities.
|
|
242
239
|
* @see {@link matches}
|
|
243
240
|
*/
|
|
244
|
-
$matches(
|
|
241
|
+
get $matches(): typeof this.matches;
|
|
245
242
|
/**
|
|
246
|
-
*
|
|
247
|
-
*
|
|
243
|
+
* Bound alias for {@link ifMatches} for compatibility with generated utilities.
|
|
248
244
|
* @see {@link ifMatches}
|
|
249
245
|
*/
|
|
250
|
-
$ifMatches
|
|
246
|
+
get $ifMatches(): typeof this.ifMatches;
|
|
251
247
|
/**
|
|
252
|
-
*
|
|
253
|
-
*
|
|
248
|
+
* Bound alias for {@link parse} for compatibility with generated utilities.
|
|
254
249
|
* @see {@link parse}
|
|
255
250
|
*/
|
|
256
|
-
$parse(
|
|
251
|
+
get $parse(): typeof this.parse;
|
|
257
252
|
/**
|
|
258
|
-
*
|
|
259
|
-
*
|
|
253
|
+
* Bound alias for {@link safeParse} for compatibility with generated utilities.
|
|
260
254
|
* @see {@link safeParse}
|
|
261
255
|
*/
|
|
262
|
-
$safeParse(
|
|
256
|
+
get $safeParse(): typeof this.safeParse;
|
|
263
257
|
/**
|
|
264
|
-
*
|
|
265
|
-
*
|
|
258
|
+
* Bound alias for {@link validate} for compatibility with generated utilities.
|
|
266
259
|
* @see {@link validate}
|
|
267
260
|
*/
|
|
268
|
-
$validate
|
|
261
|
+
get $validate(): typeof this.validate;
|
|
269
262
|
/**
|
|
270
|
-
*
|
|
271
|
-
*
|
|
263
|
+
* Bound alias for {@link safeValidate} for compatibility with generated utilities.
|
|
272
264
|
* @see {@link safeValidate}
|
|
273
265
|
*/
|
|
274
|
-
$safeValidate
|
|
266
|
+
get $safeValidate(): typeof this.safeValidate;
|
|
275
267
|
}
|
|
276
|
-
export {};
|
|
277
268
|
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,SAAS,EACV,MAAM,gBAAgB,CAAA;AAEvB;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,IAAI,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAA;AAE1D;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,IAAI,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAA;AAE7D;;;;;;;;;;GAUG;AACH,M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a,IAAI,OAAO,IAAI,CAAC,YAAY,CAE5C;CACF"}
|
package/dist/core/schema.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Schema = void 0;
|
|
4
|
+
const lazy_property_js_1 = require("../util/lazy-property.js");
|
|
4
5
|
const validator_js_1 = require("./validator.js");
|
|
5
6
|
/**
|
|
6
7
|
* Abstract base class for all schema validators in the lexicon system.
|
|
@@ -25,7 +26,7 @@ const validator_js_1 = require("./validator.js");
|
|
|
25
26
|
* class MySchema extends Schema<string> {
|
|
26
27
|
* validateInContext(input: unknown, ctx: ValidationContext): ValidationResult {
|
|
27
28
|
* if (typeof input !== 'string') {
|
|
28
|
-
* return ctx.
|
|
29
|
+
* return ctx.issueUnexpectedType(input, 'string')
|
|
29
30
|
* }
|
|
30
31
|
* return ctx.success(input)
|
|
31
32
|
* }
|
|
@@ -33,7 +34,7 @@ const validator_js_1 = require("./validator.js");
|
|
|
33
34
|
*
|
|
34
35
|
* const schema = new MySchema()
|
|
35
36
|
* schema.assert('hello') // OK
|
|
36
|
-
* schema.assert(123) // Throws
|
|
37
|
+
* schema.assert(123) // Throws LexValidationError
|
|
37
38
|
* schema.matches('hello') // true
|
|
38
39
|
* schema.matches(123) // false
|
|
39
40
|
* ```
|
|
@@ -114,12 +115,12 @@ class Schema {
|
|
|
114
115
|
*
|
|
115
116
|
* Unlike {@link validate}, this method allows the schema to transform
|
|
116
117
|
* the input value (e.g., applying default values, type coercion).
|
|
117
|
-
* Throws a {@link
|
|
118
|
+
* Throws a {@link LexValidationError} if the input is invalid.
|
|
118
119
|
*
|
|
119
120
|
* @param input - The value to parse
|
|
120
121
|
* @param options - Optional parsing configuration
|
|
121
122
|
* @returns The parsed and potentially transformed value
|
|
122
|
-
* @throws {
|
|
123
|
+
* @throws {LexValidationError} If the input fails validation
|
|
123
124
|
*
|
|
124
125
|
* @example
|
|
125
126
|
* ```typescript
|
|
@@ -164,13 +165,13 @@ class Schema {
|
|
|
164
165
|
*
|
|
165
166
|
* Unlike {@link parse}, this method requires the input to exactly match
|
|
166
167
|
* the schema without any transformations (no defaults applied, no coercion).
|
|
167
|
-
* Throws a {@link
|
|
168
|
+
* Throws a {@link LexValidationError} if the input is invalid or would require transformation.
|
|
168
169
|
*
|
|
169
170
|
* @typeParam I - The input type (preserved in the return type)
|
|
170
171
|
* @param input - The value to validate
|
|
171
172
|
* @param options - Optional validation configuration
|
|
172
173
|
* @returns The validated input with narrowed type
|
|
173
|
-
* @throws {
|
|
174
|
+
* @throws {LexValidationError} If the input fails validation or requires transformation
|
|
174
175
|
*
|
|
175
176
|
* @example
|
|
176
177
|
* ```typescript
|
|
@@ -213,90 +214,103 @@ class Schema {
|
|
|
213
214
|
}
|
|
214
215
|
// @NOTE Dollar-prefixed aliases
|
|
215
216
|
//
|
|
216
|
-
// The
|
|
217
|
-
// the schema's methods without the need to specify ".main."
|
|
218
|
-
// namespace. This
|
|
219
|
-
// like "app.bsky.feed.post.<utility>()" instead of
|
|
220
|
-
// "app.bsky.feed.post.main.<utility>()".
|
|
221
|
-
// conflict with other schemas (e.g. if there is a lexicon definition at
|
|
222
|
-
// "#<utility>"), those exported utilities will be prefixed with "$". In order
|
|
223
|
-
// to be able to consistently call the utilities, when using the "main" and
|
|
224
|
-
// non "main" definitions, we also expose the same methods with a "$" prefix.
|
|
225
|
-
// Thanks to this, both of the following call will be possible:
|
|
217
|
+
// The `lex-builder` lib generates namespaced utility functions that allow
|
|
218
|
+
// accessing the schema's methods without the need to specify the ".main."
|
|
219
|
+
// part of the namespace. This allows utilities for a particular record type
|
|
220
|
+
// to be called like "app.bsky.feed.post.<utility>()" instead of
|
|
221
|
+
// "app.bsky.feed.post.main.<utility>()".
|
|
226
222
|
//
|
|
227
|
-
//
|
|
228
|
-
//
|
|
223
|
+
// Because those utilities could conflict with other schemas (e.g. if there is
|
|
224
|
+
// a lexicon definition with the same name as the "<utility>"), those exported
|
|
225
|
+
// utilities will be prefixed with "$".
|
|
226
|
+
//
|
|
227
|
+
// Similarly, since those utilities are defined as simple "const", they are
|
|
228
|
+
// also bound (using JS's .bind) to the schema instance, so that they can be
|
|
229
|
+
// used without worrying about the context (e.g. "app.bsky.feed.post.$parse()"
|
|
230
|
+
// will work regardless of how it is imported or called).
|
|
231
|
+
//
|
|
232
|
+
// In order to provide the same functionalities for non-main definitions, we
|
|
233
|
+
// also define those aliases directly on the schema instance, so that they can
|
|
234
|
+
// be used in the same way as the utilities generated by "lex-builder". For
|
|
235
|
+
// example, if there is a non-main definition "app.bsky.feed.defs.postView",
|
|
236
|
+
// it will also be possible to call "app.bsky.feed.defs.postView.$parse()".
|
|
237
|
+
//
|
|
238
|
+
// These methods are also "bound" to the instance so that they can be used
|
|
239
|
+
// exactly like the utilities generated by "lex-builder", without worrying
|
|
240
|
+
// about the context.
|
|
241
|
+
//
|
|
242
|
+
// There are two ways we could "bind" those methods to the instance:
|
|
243
|
+
// 1. Define them as getters that return the bound method (e.g. get $parse() {
|
|
244
|
+
// return this.parse.bind(this) })
|
|
245
|
+
// 2. Define them as properties that are initialized in the constructor (e.g.
|
|
246
|
+
// this.$parse = this.parse.bind(this))
|
|
247
|
+
//
|
|
248
|
+
// Since a **lot** of those methods would end-up being created in systems that
|
|
249
|
+
// contains many schemas (e.g. the appview), we choose the first approach
|
|
250
|
+
// (getters) in order to avoid the overhead of creating all those bound
|
|
251
|
+
// functions upfront when instantiating the schemas.
|
|
229
252
|
/**
|
|
230
|
-
*
|
|
231
|
-
*
|
|
253
|
+
* Bound alias for {@link assert} for compatibility with generated utilities.
|
|
232
254
|
* @see {@link assert}
|
|
233
255
|
*/
|
|
234
|
-
$assert(
|
|
235
|
-
return this.assert(
|
|
256
|
+
get $assert() {
|
|
257
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$assert', this.assert.bind(this));
|
|
236
258
|
}
|
|
237
259
|
/**
|
|
238
|
-
*
|
|
239
|
-
*
|
|
260
|
+
* Bound alias for {@link check} for compatibility with generated utilities.
|
|
240
261
|
* @see {@link check}
|
|
241
262
|
*/
|
|
242
|
-
$check(
|
|
243
|
-
return this.check(
|
|
263
|
+
get $check() {
|
|
264
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$check', this.check.bind(this));
|
|
244
265
|
}
|
|
245
266
|
/**
|
|
246
|
-
*
|
|
247
|
-
*
|
|
267
|
+
* Bound alias for {@link cast} for compatibility with generated utilities.
|
|
248
268
|
* @see {@link cast}
|
|
249
269
|
*/
|
|
250
|
-
$cast(
|
|
251
|
-
return this.cast(
|
|
270
|
+
get $cast() {
|
|
271
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$cast', this.cast.bind(this));
|
|
252
272
|
}
|
|
253
273
|
/**
|
|
254
|
-
*
|
|
255
|
-
*
|
|
274
|
+
* Bound alias for {@link matches} for compatibility with generated utilities.
|
|
256
275
|
* @see {@link matches}
|
|
257
276
|
*/
|
|
258
|
-
$matches(
|
|
259
|
-
return this.matches(
|
|
277
|
+
get $matches() {
|
|
278
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$matches', this.matches.bind(this));
|
|
260
279
|
}
|
|
261
280
|
/**
|
|
262
|
-
*
|
|
263
|
-
*
|
|
281
|
+
* Bound alias for {@link ifMatches} for compatibility with generated utilities.
|
|
264
282
|
* @see {@link ifMatches}
|
|
265
283
|
*/
|
|
266
|
-
$ifMatches(
|
|
267
|
-
return this.ifMatches(
|
|
284
|
+
get $ifMatches() {
|
|
285
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$ifMatches', this.ifMatches.bind(this));
|
|
268
286
|
}
|
|
269
287
|
/**
|
|
270
|
-
*
|
|
271
|
-
*
|
|
288
|
+
* Bound alias for {@link parse} for compatibility with generated utilities.
|
|
272
289
|
* @see {@link parse}
|
|
273
290
|
*/
|
|
274
|
-
$parse(
|
|
275
|
-
return this.parse(
|
|
291
|
+
get $parse() {
|
|
292
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$parse', this.parse.bind(this));
|
|
276
293
|
}
|
|
277
294
|
/**
|
|
278
|
-
*
|
|
279
|
-
*
|
|
295
|
+
* Bound alias for {@link safeParse} for compatibility with generated utilities.
|
|
280
296
|
* @see {@link safeParse}
|
|
281
297
|
*/
|
|
282
|
-
$safeParse(
|
|
283
|
-
return this.safeParse(
|
|
298
|
+
get $safeParse() {
|
|
299
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$safeParse', this.safeParse.bind(this));
|
|
284
300
|
}
|
|
285
301
|
/**
|
|
286
|
-
*
|
|
287
|
-
*
|
|
302
|
+
* Bound alias for {@link validate} for compatibility with generated utilities.
|
|
288
303
|
* @see {@link validate}
|
|
289
304
|
*/
|
|
290
|
-
$validate(
|
|
291
|
-
return this.validate(
|
|
305
|
+
get $validate() {
|
|
306
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$validate', this.validate.bind(this));
|
|
292
307
|
}
|
|
293
308
|
/**
|
|
294
|
-
*
|
|
295
|
-
*
|
|
309
|
+
* Bound alias for {@link safeValidate} for compatibility with generated utilities.
|
|
296
310
|
* @see {@link safeValidate}
|
|
297
311
|
*/
|
|
298
|
-
$safeValidate(
|
|
299
|
-
return this.safeValidate(
|
|
312
|
+
get $safeValidate() {
|
|
313
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$safeValidate', this.safeValidate.bind(this));
|
|
300
314
|
}
|
|
301
315
|
}
|
|
302
316
|
exports.Schema = Schema;
|
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;AA8BvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAsB,MAAM;IAmC1B;;;;;OAKG;IACH,MAAM,CAAC,KAAc;QACnB,MAAM,MAAM,GAAG,gCAAiB,CAAC,QAAQ,CAAC,KAAK,EAAE,I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AED;;;;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;AAhUD,wBAgUC","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 */\ntype ParseOptions = Omit<ValidationOptions, 'mode'>\n\n/**\n * Options for validation operations.\n * Excludes the `mode` option as it is implicitly set to `\"validate\"`.\n */\ntype 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.issueInvalidType(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 /**\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
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":";;;AAAA,+DAAuD;AACvD,iDAOuB;AA8BvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAsB,MAAM;IAyC1B;;;;;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;YAAE,OAAO,MAAM,CAAC,KAAK,CAAA;QACvC,MAAM,MAAM,CAAC,MAAM,CAAA;IACrB,CAAC;IAED;;;;;;;;;;;;;OAaG;IACH,OAAO,CAAI,KAAQ;QACjB,MAAM,MAAM,GAAG,gCAAiB,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;QACtD,OAAO,MAAM,CAAC,OAAO,CAAA;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,SAAS,CAAI,KAAQ;QACnB,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;IAChD,CAAC;IAED;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AAY,EAAC,IAAI,EAAE,YAAY,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IACpE,CAAC;IAED;;;OAGG;IACH,IAAI,SAAS;QACX,OAAO,IAAA,+BAAY,EAAC,IAAI,EAAE,WAAW,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IAClE,CAAC;IAED;;;OAGG;IACH,IAAI,aAAa;QACf,OAAO,IAAA,+BAAY,EAAC,IAAI,EAAE,eAAe,EAAE,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IAC1E,CAAC;CACF;AA7UD,wBA6UC","sourcesContent":["import { lazyProperty } from '../util/lazy-property.js'\nimport {\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 LexValidationError\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 LexValidationError} 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 {LexValidationError} 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 LexValidationError} 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 {LexValidationError} 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 `lex-builder` lib generates namespaced utility functions that allow\n // accessing the schema's methods without the need to specify the \".main.\"\n // part of the namespace. This allows utilities for a particular record type\n // to be called like \"app.bsky.feed.post.<utility>()\" instead of\n // \"app.bsky.feed.post.main.<utility>()\".\n //\n // Because those utilities could conflict with other schemas (e.g. if there is\n // a lexicon definition with the same name as the \"<utility>\"), those exported\n // utilities will be prefixed with \"$\".\n //\n // Similarly, since those utilities are defined as simple \"const\", they are\n // also bound (using JS's .bind) to the schema instance, so that they can be\n // used without worrying about the context (e.g. \"app.bsky.feed.post.$parse()\"\n // will work regardless of how it is imported or called).\n //\n // In order to provide the same functionalities for non-main definitions, we\n // also define those aliases directly on the schema instance, so that they can\n // be used in the same way as the utilities generated by \"lex-builder\". For\n // example, if there is a non-main definition \"app.bsky.feed.defs.postView\",\n // it will also be possible to call \"app.bsky.feed.defs.postView.$parse()\".\n //\n // These methods are also \"bound\" to the instance so that they can be used\n // exactly like the utilities generated by \"lex-builder\", without worrying\n // about the context.\n //\n // There are two ways we could \"bind\" those methods to the instance:\n // 1. Define them as getters that return the bound method (e.g. get $parse() {\n // return this.parse.bind(this) })\n // 2. Define them as properties that are initialized in the constructor (e.g.\n // this.$parse = this.parse.bind(this))\n //\n // Since a **lot** of those methods would end-up being created in systems that\n // contains many schemas (e.g. the appview), we choose the first approach\n // (getters) in order to avoid the overhead of creating all those bound\n // functions upfront when instantiating the schemas.\n\n /**\n * Bound alias for {@link assert} for compatibility with generated utilities.\n * @see {@link assert}\n */\n get $assert(): typeof this.assert {\n return lazyProperty(this, '$assert', this.assert.bind(this))\n }\n\n /**\n * Bound alias for {@link check} for compatibility with generated utilities.\n * @see {@link check}\n */\n get $check(): typeof this.check {\n return lazyProperty(this, '$check', this.check.bind(this))\n }\n\n /**\n * Bound alias for {@link cast} for compatibility with generated utilities.\n * @see {@link cast}\n */\n get $cast(): typeof this.cast {\n return lazyProperty(this, '$cast', this.cast.bind(this))\n }\n\n /**\n * Bound alias for {@link matches} for compatibility with generated utilities.\n * @see {@link matches}\n */\n get $matches(): typeof this.matches {\n return lazyProperty(this, '$matches', this.matches.bind(this))\n }\n\n /**\n * Bound alias for {@link ifMatches} for compatibility with generated utilities.\n * @see {@link ifMatches}\n */\n get $ifMatches(): typeof this.ifMatches {\n return lazyProperty(this, '$ifMatches', this.ifMatches.bind(this))\n }\n\n /**\n * Bound alias for {@link parse} for compatibility with generated utilities.\n * @see {@link parse}\n */\n get $parse(): typeof this.parse {\n return lazyProperty(this, '$parse', this.parse.bind(this))\n }\n\n /**\n * Bound alias for {@link safeParse} for compatibility with generated utilities.\n * @see {@link safeParse}\n */\n get $safeParse(): typeof this.safeParse {\n return lazyProperty(this, '$safeParse', this.safeParse.bind(this))\n }\n\n /**\n * Bound alias for {@link validate} for compatibility with generated utilities.\n * @see {@link validate}\n */\n get $validate(): typeof this.validate {\n return lazyProperty(this, '$validate', this.validate.bind(this))\n }\n\n /**\n * Bound alias for {@link safeValidate} for compatibility with generated utilities.\n * @see {@link safeValidate}\n */\n get $safeValidate(): typeof this.safeValidate {\n return lazyProperty(this, '$safeValidate', this.safeValidate.bind(this))\n }\n}\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
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
|
+
export { type DatetimeString, asDatetimeString, currentDatetimeString, ifDatetimeString, isDatetimeString, toDatetimeString, } from '@atproto/syntax';
|
|
3
4
|
/**
|
|
4
5
|
* Type guard that checks if a value is a valid AT identifier (DID or handle).
|
|
5
6
|
*
|
|
@@ -43,20 +44,6 @@ export declare const isCidString: CheckFn<CidString>;
|
|
|
43
44
|
* @example `"bafyreig..."`
|
|
44
45
|
*/
|
|
45
46
|
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
|
-
*/
|
|
52
|
-
export declare const isDatetimeString: CheckFn<DatetimeString>;
|
|
53
|
-
export type {
|
|
54
|
-
/**
|
|
55
|
-
* An ISO 8601 datetime string.
|
|
56
|
-
*
|
|
57
|
-
* @example `"2024-01-15T12:30:00.000Z"`
|
|
58
|
-
*/
|
|
59
|
-
DatetimeString, };
|
|
60
47
|
/**
|
|
61
48
|
* Type guard that checks if a value is a valid DID string.
|
|
62
49
|
*
|
|
@@ -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;AAUnD,OAAO,EACL,KAAK,cAAc,EACnB,gBAAgB,EAChB,qBAAqB,EACrB,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,iBAAiB,CAAA;AAExB;;;;;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;;;;;GAK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ED;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,EAEtB,SAAS,YAAY,EAAE,CAAA"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.STRING_FORMATS = exports.isUriString = exports.isTidString = exports.isRecordKeyString = exports.isNsidString = exports.isLanguageString = exports.isHandleString = exports.isDidString = exports.isDatetimeString = exports.
|
|
3
|
+
exports.STRING_FORMATS = exports.isUriString = exports.isTidString = exports.isRecordKeyString = exports.isNsidString = exports.isLanguageString = exports.isHandleString = exports.isDidString = exports.isCidString = exports.isAtUriString = exports.isAtIdentifierString = exports.toDatetimeString = exports.isDatetimeString = exports.ifDatetimeString = exports.currentDatetimeString = exports.asDatetimeString = void 0;
|
|
4
4
|
exports.isStringFormat = isStringFormat;
|
|
5
5
|
exports.assertStringFormat = assertStringFormat;
|
|
6
6
|
exports.asStringFormat = asStringFormat;
|
|
@@ -10,6 +10,16 @@ const syntax_1 = require("@atproto/syntax");
|
|
|
10
10
|
// -----------------------------------------------------------------------------
|
|
11
11
|
// Individual string format types and type guards
|
|
12
12
|
// -----------------------------------------------------------------------------
|
|
13
|
+
// Re-exporting from @atproto/syntax without modification to preserve types and
|
|
14
|
+
// documentation for types and utilities that are already well-defined there.
|
|
15
|
+
// @TODO rework other string formats in @atproto/syntax to follow this pattern
|
|
16
|
+
// and re-export here, e.g. language tags, NSIDs, record keys, etc.
|
|
17
|
+
var syntax_2 = require("@atproto/syntax");
|
|
18
|
+
Object.defineProperty(exports, "asDatetimeString", { enumerable: true, get: function () { return syntax_2.asDatetimeString; } });
|
|
19
|
+
Object.defineProperty(exports, "currentDatetimeString", { enumerable: true, get: function () { return syntax_2.currentDatetimeString; } });
|
|
20
|
+
Object.defineProperty(exports, "ifDatetimeString", { enumerable: true, get: function () { return syntax_2.ifDatetimeString; } });
|
|
21
|
+
Object.defineProperty(exports, "isDatetimeString", { enumerable: true, get: function () { return syntax_2.isDatetimeString; } });
|
|
22
|
+
Object.defineProperty(exports, "toDatetimeString", { enumerable: true, get: function () { return syntax_2.toDatetimeString; } });
|
|
13
23
|
/**
|
|
14
24
|
* Type guard that checks if a value is a valid AT identifier (DID or handle).
|
|
15
25
|
*
|
|
@@ -31,13 +41,6 @@ exports.isAtUriString = syntax_1.isValidAtUri;
|
|
|
31
41
|
* @returns `true` if the value is a valid CID string
|
|
32
42
|
*/
|
|
33
43
|
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
|
-
*/
|
|
40
|
-
exports.isDatetimeString = syntax_1.isValidDatetime;
|
|
41
44
|
/**
|
|
42
45
|
* Type guard that checks if a value is a valid DID string.
|
|
43
46
|
*
|
|
@@ -92,7 +95,7 @@ const stringFormatVerifiers = /*#__PURE__*/ Object.freeze({
|
|
|
92
95
|
'at-identifier': exports.isAtIdentifierString,
|
|
93
96
|
'at-uri': exports.isAtUriString,
|
|
94
97
|
cid: exports.isCidString,
|
|
95
|
-
datetime:
|
|
98
|
+
datetime: syntax_1.isDatetimeString,
|
|
96
99
|
did: exports.isDidString,
|
|
97
100
|
handle: exports.isHandleString,
|
|
98
101
|
language: exports.isLanguageString,
|