@atproto/lex-schema 0.0.13 → 0.0.15
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 +39 -0
- package/dist/core/schema.d.ts +27 -35
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +73 -54
- package/dist/core/schema.js.map +1 -1
- package/dist/core/standard-schema.d.ts +14 -0
- package/dist/core/standard-schema.d.ts.map +1 -0
- package/dist/core/standard-schema.js +27 -0
- package/dist/core/standard-schema.js.map +1 -0
- package/dist/core/string-format.d.ts +4 -28
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +23 -17
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/validation-error.d.ts +14 -6
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +18 -8
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.d.ts +15 -15
- package/dist/core/validation-issue.d.ts.map +1 -1
- package/dist/core/validation-issue.js +33 -29
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +21 -18
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +5 -4
- package/dist/core/validator.js.map +1 -1
- package/dist/core.d.ts +0 -1
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +0 -1
- package/dist/core.js.map +1 -1
- package/dist/schema/custom.d.ts +1 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/never.d.ts +1 -1
- package/dist/schema/nullable.d.ts +1 -1
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +4 -1
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/record.d.ts +12 -6
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +21 -12
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +1 -1
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +1 -1
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/typed-object.d.ts +12 -4
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +21 -12
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +1 -1
- package/dist/schema/typed-union.d.ts +1 -1
- package/dist/schema/union.d.ts +2 -2
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +1 -1
- package/dist/schema/union.js.map +1 -1
- package/package.json +4 -3
- package/src/core/schema.ts +78 -69
- package/src/core/standard-schema.test.ts +124 -0
- package/src/core/standard-schema.ts +31 -0
- package/src/core/string-format.ts +26 -33
- package/src/core/validation-error.ts +25 -10
- package/src/core/validation-issue.ts +32 -32
- package/src/core/validator.ts +16 -12
- package/src/core.ts +0 -1
- package/src/schema/array.test.ts +2 -2
- package/src/schema/custom.ts +1 -7
- package/src/schema/params.test.ts +18 -2
- package/src/schema/params.ts +5 -2
- package/src/schema/record.ts +27 -22
- package/src/schema/ref.ts +1 -5
- package/src/schema/refine.ts +0 -1
- package/src/schema/typed-object.test.ts +38 -0
- package/src/schema/typed-object.ts +29 -24
- package/src/schema/union.ts +2 -2
- package/dist/core/property-key.d.ts +0 -2
- package/dist/core/property-key.d.ts.map +0 -1
- package/dist/core/property-key.js +0 -3
- package/dist/core/property-key.js.map +0 -1
- package/src/core/property-key.ts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
# @atproto/lex-schema
|
|
2
2
|
|
|
3
|
+
## 0.0.15
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4734](https://github.com/bluesky-social/atproto/pull/4734) [`3dc3791`](https://github.com/bluesky-social/atproto/commit/3dc37915436dec7e18c7dc9dcf01b72cad53fdbe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Rename `IssueInvalidFormat`'s `message` property to `detail`
|
|
8
|
+
|
|
9
|
+
- [#4734](https://github.com/bluesky-social/atproto/pull/4734) [`3dc3791`](https://github.com/bluesky-social/atproto/commit/3dc37915436dec7e18c7dc9dcf01b72cad53fdbe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add a read-only `message` property to the `Issue` class
|
|
10
|
+
|
|
11
|
+
- [#4734](https://github.com/bluesky-social/atproto/pull/4734) [`3dc3791`](https://github.com/bluesky-social/atproto/commit/3dc37915436dec7e18c7dc9dcf01b72cad53fdbe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - `LexValidationError` class now implements `ResultFailure` allowing it to be used as validation return value directly (without the need to be wrapped)
|
|
12
|
+
|
|
13
|
+
- [#4734](https://github.com/bluesky-social/atproto/pull/4734) [`3dc3791`](https://github.com/bluesky-social/atproto/commit/3dc37915436dec7e18c7dc9dcf01b72cad53fdbe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Update stringification of issues (`toString()`) to consistently display the error path at the end.
|
|
14
|
+
|
|
15
|
+
- [`112b159`](https://github.com/bluesky-social/atproto/commit/112b159ec293a5c3fff41237474a3788fc47f9ca) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Export more string format type assertion utilities
|
|
16
|
+
|
|
17
|
+
- [#4734](https://github.com/bluesky-social/atproto/pull/4734) [`3dc3791`](https://github.com/bluesky-social/atproto/commit/3dc37915436dec7e18c7dc9dcf01b72cad53fdbe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - `PropertyKey` is no longer exported. Use the global value instead.
|
|
18
|
+
|
|
19
|
+
- [#4734](https://github.com/bluesky-social/atproto/pull/4734) [`3dc3791`](https://github.com/bluesky-social/atproto/commit/3dc37915436dec7e18c7dc9dcf01b72cad53fdbe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add [Standard Schema](https://standardschema.dev/) compatibility
|
|
20
|
+
|
|
21
|
+
- Updated dependencies [[`67eb0c1`](https://github.com/bluesky-social/atproto/commit/67eb0c19ac415e762e221b2ccda9f0bcf7b3dd6f)]:
|
|
22
|
+
- @atproto/syntax@0.5.1
|
|
23
|
+
|
|
24
|
+
## 0.0.14
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- [#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
|
|
29
|
+
|
|
30
|
+
- [#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`
|
|
31
|
+
|
|
32
|
+
- [#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
|
|
33
|
+
|
|
34
|
+
- [#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
|
|
35
|
+
|
|
36
|
+
- [#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
|
|
37
|
+
|
|
38
|
+
- 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)]:
|
|
39
|
+
- @atproto/syntax@0.5.0
|
|
40
|
+
- @atproto/lex-data@0.0.13
|
|
41
|
+
|
|
3
42
|
## 0.0.13
|
|
4
43
|
|
|
5
44
|
### Patch Changes
|
package/dist/core/schema.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
1
2
|
import { InferInput, InferOutput, ValidationContext, ValidationOptions, ValidationResult, Validator } from './validator.js';
|
|
2
3
|
/**
|
|
3
4
|
* Options for parsing operations.
|
|
@@ -40,7 +41,6 @@ export interface SchemaInternals<out TInput = unknown, out TOutput = TInput> {
|
|
|
40
41
|
*
|
|
41
42
|
* @typeParam TInput - The type accepted as valid input during validation
|
|
42
43
|
* @typeParam TOutput - The type returned after parsing (may include transformations)
|
|
43
|
-
* @typeParam TInternals - Internal type structure for type inference
|
|
44
44
|
*
|
|
45
45
|
* @example
|
|
46
46
|
* ```typescript
|
|
@@ -55,19 +55,20 @@ 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
|
* ```
|
|
62
62
|
*/
|
|
63
|
-
export declare abstract class Schema<out TInput = unknown, out TOutput = TInput
|
|
63
|
+
export declare abstract class Schema<out TInput = unknown, out TOutput = TInput> implements Validator<TInput, TOutput>, StandardSchemaV1<TInput, TOutput> {
|
|
64
64
|
/**
|
|
65
65
|
* Internal phantom property for type inference.
|
|
66
66
|
* This property does not exist at runtime.
|
|
67
67
|
*
|
|
68
68
|
* @internal
|
|
69
69
|
*/
|
|
70
|
-
readonly ['__lex']:
|
|
70
|
+
readonly ['__lex']: SchemaInternals<TInput, TOutput>;
|
|
71
|
+
get '~standard'(): StandardSchemaV1.Props<TInput, TOutput>;
|
|
71
72
|
abstract readonly type: string;
|
|
72
73
|
/**
|
|
73
74
|
* Performs validation of the input value within a validation context.
|
|
@@ -142,12 +143,12 @@ export declare abstract class Schema<out TInput = unknown, out TOutput = TInput,
|
|
|
142
143
|
*
|
|
143
144
|
* Unlike {@link validate}, this method allows the schema to transform
|
|
144
145
|
* the input value (e.g., applying default values, type coercion).
|
|
145
|
-
* Throws a {@link
|
|
146
|
+
* Throws a {@link LexValidationError} if the input is invalid.
|
|
146
147
|
*
|
|
147
148
|
* @param input - The value to parse
|
|
148
149
|
* @param options - Optional parsing configuration
|
|
149
150
|
* @returns The parsed and potentially transformed value
|
|
150
|
-
* @throws {
|
|
151
|
+
* @throws {LexValidationError} If the input fails validation
|
|
151
152
|
*
|
|
152
153
|
* @example
|
|
153
154
|
* ```typescript
|
|
@@ -182,13 +183,13 @@ export declare abstract class Schema<out TInput = unknown, out TOutput = TInput,
|
|
|
182
183
|
*
|
|
183
184
|
* Unlike {@link parse}, this method requires the input to exactly match
|
|
184
185
|
* the schema without any transformations (no defaults applied, no coercion).
|
|
185
|
-
* Throws a {@link
|
|
186
|
+
* Throws a {@link LexValidationError} if the input is invalid or would require transformation.
|
|
186
187
|
*
|
|
187
188
|
* @typeParam I - The input type (preserved in the return type)
|
|
188
189
|
* @param input - The value to validate
|
|
189
190
|
* @param options - Optional validation configuration
|
|
190
191
|
* @returns The validated input with narrowed type
|
|
191
|
-
* @throws {
|
|
192
|
+
* @throws {LexValidationError} If the input fails validation or requires transformation
|
|
192
193
|
*
|
|
193
194
|
* @example
|
|
194
195
|
* ```typescript
|
|
@@ -220,58 +221,49 @@ export declare abstract class Schema<out TInput = unknown, out TOutput = TInput,
|
|
|
220
221
|
*/
|
|
221
222
|
safeValidate<I>(input: I, options?: ValidateOptions): ValidationResult<I & InferInput<this>>;
|
|
222
223
|
/**
|
|
223
|
-
*
|
|
224
|
-
*
|
|
224
|
+
* Bound alias for {@link assert} for compatibility with generated utilities.
|
|
225
225
|
* @see {@link assert}
|
|
226
226
|
*/
|
|
227
|
-
$assert(
|
|
227
|
+
get $assert(): typeof this.assert;
|
|
228
228
|
/**
|
|
229
|
-
*
|
|
230
|
-
*
|
|
229
|
+
* Bound alias for {@link check} for compatibility with generated utilities.
|
|
231
230
|
* @see {@link check}
|
|
232
231
|
*/
|
|
233
|
-
$check(
|
|
232
|
+
get $check(): typeof this.check;
|
|
234
233
|
/**
|
|
235
|
-
*
|
|
236
|
-
*
|
|
234
|
+
* Bound alias for {@link cast} for compatibility with generated utilities.
|
|
237
235
|
* @see {@link cast}
|
|
238
236
|
*/
|
|
239
|
-
$cast
|
|
237
|
+
get $cast(): typeof this.cast;
|
|
240
238
|
/**
|
|
241
|
-
*
|
|
242
|
-
*
|
|
239
|
+
* Bound alias for {@link matches} for compatibility with generated utilities.
|
|
243
240
|
* @see {@link matches}
|
|
244
241
|
*/
|
|
245
|
-
$matches(
|
|
242
|
+
get $matches(): typeof this.matches;
|
|
246
243
|
/**
|
|
247
|
-
*
|
|
248
|
-
*
|
|
244
|
+
* Bound alias for {@link ifMatches} for compatibility with generated utilities.
|
|
249
245
|
* @see {@link ifMatches}
|
|
250
246
|
*/
|
|
251
|
-
$ifMatches
|
|
247
|
+
get $ifMatches(): typeof this.ifMatches;
|
|
252
248
|
/**
|
|
253
|
-
*
|
|
254
|
-
*
|
|
249
|
+
* Bound alias for {@link parse} for compatibility with generated utilities.
|
|
255
250
|
* @see {@link parse}
|
|
256
251
|
*/
|
|
257
|
-
$parse(
|
|
252
|
+
get $parse(): typeof this.parse;
|
|
258
253
|
/**
|
|
259
|
-
*
|
|
260
|
-
*
|
|
254
|
+
* Bound alias for {@link safeParse} for compatibility with generated utilities.
|
|
261
255
|
* @see {@link safeParse}
|
|
262
256
|
*/
|
|
263
|
-
$safeParse(
|
|
257
|
+
get $safeParse(): typeof this.safeParse;
|
|
264
258
|
/**
|
|
265
|
-
*
|
|
266
|
-
*
|
|
259
|
+
* Bound alias for {@link validate} for compatibility with generated utilities.
|
|
267
260
|
* @see {@link validate}
|
|
268
261
|
*/
|
|
269
|
-
$validate
|
|
262
|
+
get $validate(): typeof this.validate;
|
|
270
263
|
/**
|
|
271
|
-
*
|
|
272
|
-
*
|
|
264
|
+
* Bound alias for {@link safeValidate} for compatibility with generated utilities.
|
|
273
265
|
* @see {@link safeValidate}
|
|
274
266
|
*/
|
|
275
|
-
$safeValidate
|
|
267
|
+
get $safeValidate(): typeof this.safeValidate;
|
|
276
268
|
}
|
|
277
269
|
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":"AAAA,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,iB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,KAAK,EAAE,CAAC,EACR,OAAO,CAAC,EAAE,eAAe,GACxB,gBAAgB,CAAC,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;IA6CzC;;;OAGG;IACH,IAAI,OAAO,IAAI,OAAO,IAAI,CAAC,MAAM,CAEhC;IAED;;;OAGG;IACH,IAAI,MAAM,IAAI,OAAO,IAAI,CAAC,KAAK,CAE9B;IAED;;;OAGG;IACH,IAAI,KAAK,IAAI,OAAO,IAAI,CAAC,IAAI,CAE5B;IAED;;;OAGG;IACH,IAAI,QAAQ,IAAI,OAAO,IAAI,CAAC,OAAO,CAElC;IAED;;;OAGG;IACH,IAAI,UAAU,IAAI,OAAO,IAAI,CAAC,SAAS,CAEtC;IAED;;;OAGG;IACH,IAAI,MAAM,IAAI,OAAO,IAAI,CAAC,KAAK,CAE9B;IAED;;;OAGG;IACH,IAAI,UAAU,IAAI,OAAO,IAAI,CAAC,SAAS,CAEtC;IAED;;;OAGG;IACH,IAAI,SAAS,IAAI,OAAO,IAAI,CAAC,QAAQ,CAEpC;IAED;;;OAGG;IACH,IAAI,aAAa,IAAI,OAAO,IAAI,CAAC,YAAY,CAE5C;CACF"}
|
package/dist/core/schema.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
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");
|
|
5
|
+
const standard_schema_js_1 = require("./standard-schema.js");
|
|
4
6
|
const validator_js_1 = require("./validator.js");
|
|
5
7
|
/**
|
|
6
8
|
* Abstract base class for all schema validators in the lexicon system.
|
|
@@ -18,7 +20,6 @@ const validator_js_1 = require("./validator.js");
|
|
|
18
20
|
*
|
|
19
21
|
* @typeParam TInput - The type accepted as valid input during validation
|
|
20
22
|
* @typeParam TOutput - The type returned after parsing (may include transformations)
|
|
21
|
-
* @typeParam TInternals - Internal type structure for type inference
|
|
22
23
|
*
|
|
23
24
|
* @example
|
|
24
25
|
* ```typescript
|
|
@@ -33,12 +34,17 @@ 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
|
* ```
|
|
40
41
|
*/
|
|
41
42
|
class Schema {
|
|
43
|
+
get '~standard'() {
|
|
44
|
+
// Lazily create, and cache, the Standard Schema adapter for this schema
|
|
45
|
+
// instance.
|
|
46
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '~standard', new standard_schema_js_1.StandardSchemaAdapter(this));
|
|
47
|
+
}
|
|
42
48
|
/**
|
|
43
49
|
* @note use {@link check}() instead of {@link assert}() if you encounter a
|
|
44
50
|
* `ts(2775)` error and you are not able to fully type the validator. This
|
|
@@ -114,12 +120,12 @@ class Schema {
|
|
|
114
120
|
*
|
|
115
121
|
* Unlike {@link validate}, this method allows the schema to transform
|
|
116
122
|
* the input value (e.g., applying default values, type coercion).
|
|
117
|
-
* Throws a {@link
|
|
123
|
+
* Throws a {@link LexValidationError} if the input is invalid.
|
|
118
124
|
*
|
|
119
125
|
* @param input - The value to parse
|
|
120
126
|
* @param options - Optional parsing configuration
|
|
121
127
|
* @returns The parsed and potentially transformed value
|
|
122
|
-
* @throws {
|
|
128
|
+
* @throws {LexValidationError} If the input fails validation
|
|
123
129
|
*
|
|
124
130
|
* @example
|
|
125
131
|
* ```typescript
|
|
@@ -164,13 +170,13 @@ class Schema {
|
|
|
164
170
|
*
|
|
165
171
|
* Unlike {@link parse}, this method requires the input to exactly match
|
|
166
172
|
* the schema without any transformations (no defaults applied, no coercion).
|
|
167
|
-
* Throws a {@link
|
|
173
|
+
* Throws a {@link LexValidationError} if the input is invalid or would require transformation.
|
|
168
174
|
*
|
|
169
175
|
* @typeParam I - The input type (preserved in the return type)
|
|
170
176
|
* @param input - The value to validate
|
|
171
177
|
* @param options - Optional validation configuration
|
|
172
178
|
* @returns The validated input with narrowed type
|
|
173
|
-
* @throws {
|
|
179
|
+
* @throws {LexValidationError} If the input fails validation or requires transformation
|
|
174
180
|
*
|
|
175
181
|
* @example
|
|
176
182
|
* ```typescript
|
|
@@ -213,90 +219,103 @@ class Schema {
|
|
|
213
219
|
}
|
|
214
220
|
// @NOTE Dollar-prefixed aliases
|
|
215
221
|
//
|
|
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
|
-
//
|
|
222
|
-
//
|
|
223
|
-
//
|
|
224
|
-
//
|
|
225
|
-
//
|
|
222
|
+
// The `lex-builder` lib generates namespaced utility functions that allow
|
|
223
|
+
// accessing the schema's methods without the need to specify the ".main."
|
|
224
|
+
// part of the namespace. This allows utilities for a particular record type
|
|
225
|
+
// to be called like "app.bsky.feed.post.<utility>()" instead of
|
|
226
|
+
// "app.bsky.feed.post.main.<utility>()".
|
|
227
|
+
//
|
|
228
|
+
// Because those utilities could conflict with other schemas (e.g. if there is
|
|
229
|
+
// a lexicon definition with the same name as the "<utility>"), those exported
|
|
230
|
+
// utilities will be prefixed with "$".
|
|
231
|
+
//
|
|
232
|
+
// Similarly, since those utilities are defined as simple "const", they are
|
|
233
|
+
// also bound (using JS's .bind) to the schema instance, so that they can be
|
|
234
|
+
// used without worrying about the context (e.g. "app.bsky.feed.post.$parse()"
|
|
235
|
+
// will work regardless of how it is imported or called).
|
|
236
|
+
//
|
|
237
|
+
// In order to provide the same functionalities for non-main definitions, we
|
|
238
|
+
// also define those aliases directly on the schema instance, so that they can
|
|
239
|
+
// be used in the same way as the utilities generated by "lex-builder". For
|
|
240
|
+
// example, if there is a non-main definition "app.bsky.feed.defs.postView",
|
|
241
|
+
// it will also be possible to call "app.bsky.feed.defs.postView.$parse()".
|
|
226
242
|
//
|
|
227
|
-
//
|
|
228
|
-
//
|
|
243
|
+
// These methods are also "bound" to the instance so that they can be used
|
|
244
|
+
// exactly like the utilities generated by "lex-builder", without worrying
|
|
245
|
+
// about the context.
|
|
246
|
+
//
|
|
247
|
+
// There are two ways we could "bind" those methods to the instance:
|
|
248
|
+
// 1. Define them as getters that return the bound method (e.g. get $parse() {
|
|
249
|
+
// return this.parse.bind(this) })
|
|
250
|
+
// 2. Define them as properties that are initialized in the constructor (e.g.
|
|
251
|
+
// this.$parse = this.parse.bind(this))
|
|
252
|
+
//
|
|
253
|
+
// Since a **lot** of those methods would end-up being created in systems that
|
|
254
|
+
// contains many schemas (e.g. the appview), we choose the first approach
|
|
255
|
+
// (getters) in order to avoid the overhead of creating all those bound
|
|
256
|
+
// functions upfront when instantiating the schemas.
|
|
229
257
|
/**
|
|
230
|
-
*
|
|
231
|
-
*
|
|
258
|
+
* Bound alias for {@link assert} for compatibility with generated utilities.
|
|
232
259
|
* @see {@link assert}
|
|
233
260
|
*/
|
|
234
|
-
$assert(
|
|
235
|
-
return this.assert(
|
|
261
|
+
get $assert() {
|
|
262
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$assert', this.assert.bind(this));
|
|
236
263
|
}
|
|
237
264
|
/**
|
|
238
|
-
*
|
|
239
|
-
*
|
|
265
|
+
* Bound alias for {@link check} for compatibility with generated utilities.
|
|
240
266
|
* @see {@link check}
|
|
241
267
|
*/
|
|
242
|
-
$check(
|
|
243
|
-
return this.check(
|
|
268
|
+
get $check() {
|
|
269
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$check', this.check.bind(this));
|
|
244
270
|
}
|
|
245
271
|
/**
|
|
246
|
-
*
|
|
247
|
-
*
|
|
272
|
+
* Bound alias for {@link cast} for compatibility with generated utilities.
|
|
248
273
|
* @see {@link cast}
|
|
249
274
|
*/
|
|
250
|
-
$cast(
|
|
251
|
-
return this.cast(
|
|
275
|
+
get $cast() {
|
|
276
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$cast', this.cast.bind(this));
|
|
252
277
|
}
|
|
253
278
|
/**
|
|
254
|
-
*
|
|
255
|
-
*
|
|
279
|
+
* Bound alias for {@link matches} for compatibility with generated utilities.
|
|
256
280
|
* @see {@link matches}
|
|
257
281
|
*/
|
|
258
|
-
$matches(
|
|
259
|
-
return this.matches(
|
|
282
|
+
get $matches() {
|
|
283
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$matches', this.matches.bind(this));
|
|
260
284
|
}
|
|
261
285
|
/**
|
|
262
|
-
*
|
|
263
|
-
*
|
|
286
|
+
* Bound alias for {@link ifMatches} for compatibility with generated utilities.
|
|
264
287
|
* @see {@link ifMatches}
|
|
265
288
|
*/
|
|
266
|
-
$ifMatches(
|
|
267
|
-
return this.ifMatches(
|
|
289
|
+
get $ifMatches() {
|
|
290
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$ifMatches', this.ifMatches.bind(this));
|
|
268
291
|
}
|
|
269
292
|
/**
|
|
270
|
-
*
|
|
271
|
-
*
|
|
293
|
+
* Bound alias for {@link parse} for compatibility with generated utilities.
|
|
272
294
|
* @see {@link parse}
|
|
273
295
|
*/
|
|
274
|
-
$parse(
|
|
275
|
-
return this.parse(
|
|
296
|
+
get $parse() {
|
|
297
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$parse', this.parse.bind(this));
|
|
276
298
|
}
|
|
277
299
|
/**
|
|
278
|
-
*
|
|
279
|
-
*
|
|
300
|
+
* Bound alias for {@link safeParse} for compatibility with generated utilities.
|
|
280
301
|
* @see {@link safeParse}
|
|
281
302
|
*/
|
|
282
|
-
$safeParse(
|
|
283
|
-
return this.safeParse(
|
|
303
|
+
get $safeParse() {
|
|
304
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$safeParse', this.safeParse.bind(this));
|
|
284
305
|
}
|
|
285
306
|
/**
|
|
286
|
-
*
|
|
287
|
-
*
|
|
307
|
+
* Bound alias for {@link validate} for compatibility with generated utilities.
|
|
288
308
|
* @see {@link validate}
|
|
289
309
|
*/
|
|
290
|
-
$validate(
|
|
291
|
-
return this.validate(
|
|
310
|
+
get $validate() {
|
|
311
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$validate', this.validate.bind(this));
|
|
292
312
|
}
|
|
293
313
|
/**
|
|
294
|
-
*
|
|
295
|
-
*
|
|
314
|
+
* Bound alias for {@link safeValidate} for compatibility with generated utilities.
|
|
296
315
|
* @see {@link safeValidate}
|
|
297
316
|
*/
|
|
298
|
-
$safeValidate(
|
|
299
|
-
return this.safeValidate(
|
|
317
|
+
get $safeValidate() {
|
|
318
|
+
return (0, lazy_property_js_1.lazyProperty)(this, '$safeValidate', this.safeValidate.bind(this));
|
|
300
319
|
}
|
|
301
320
|
}
|
|
302
321
|
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;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,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C,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
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":";;;AACA,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,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 { StandardSchemaV1 } from '@standard-schema/spec'\nimport { lazyProperty } from '../util/lazy-property.js'\nimport { StandardSchemaAdapter } from './standard-schema.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 *\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<out TInput = unknown, out TOutput = TInput>\n implements Validator<TInput, TOutput>, StandardSchemaV1<TInput, TOutput>\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']: SchemaInternals<TInput, TOutput>\n\n get '~standard'(): StandardSchemaV1.Props<TInput, TOutput> {\n // Lazily create, and cache, the Standard Schema adapter for this schema\n // instance.\n return lazyProperty(this, '~standard', new StandardSchemaAdapter(this))\n }\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"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { Validator } from './validator.js';
|
|
3
|
+
/**
|
|
4
|
+
* The Standard Schema adapter for {@link Validator} instances.
|
|
5
|
+
*/
|
|
6
|
+
export declare class StandardSchemaAdapter<TInput, TOutput> implements StandardSchemaV1.Props<TInput, TOutput> {
|
|
7
|
+
private readonly validator;
|
|
8
|
+
readonly version = 1;
|
|
9
|
+
readonly vendor = "@atproto/lex-schema";
|
|
10
|
+
readonly types: StandardSchemaV1.Types<TInput, TOutput>;
|
|
11
|
+
constructor(validator: Validator<TInput, TOutput>);
|
|
12
|
+
validate(value: unknown, options?: StandardSchemaV1.Options): StandardSchemaV1.Result<TOutput>;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=standard-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard-schema.d.ts","sourceRoot":"","sources":["../../src/core/standard-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAqB,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE7D;;GAEG;AACH,qBAAa,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAChD,YAAW,gBAAgB,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC;IAQtC,OAAO,CAAC,QAAQ,CAAC,SAAS;IANtC,QAAQ,CAAC,OAAO,KAAI;IAEpB,QAAQ,CAAC,MAAM,yBAAwB;IAEvC,SAAiB,KAAK,EAAE,gBAAgB,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;gBAElC,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC;IAElE,QAAQ,CACN,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,gBAAgB,CAAC,OAAO,GACjC,gBAAgB,CAAC,MAAM,CAAC,OAAO,CAAC;CAUpC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StandardSchemaAdapter = void 0;
|
|
4
|
+
const validator_js_1 = require("./validator.js");
|
|
5
|
+
/**
|
|
6
|
+
* The Standard Schema adapter for {@link Validator} instances.
|
|
7
|
+
*/
|
|
8
|
+
class StandardSchemaAdapter {
|
|
9
|
+
validator;
|
|
10
|
+
version = 1;
|
|
11
|
+
vendor = '@atproto/lex-schema';
|
|
12
|
+
constructor(validator) {
|
|
13
|
+
this.validator = validator;
|
|
14
|
+
}
|
|
15
|
+
validate(value, options) {
|
|
16
|
+
// Perform validation in "parse" mode to ensure transformations (defaults,
|
|
17
|
+
// coercions, etc.) are applied. Also ensures that the output type is
|
|
18
|
+
// returned. Note that ValidationResult is compatible with
|
|
19
|
+
// StandardSchemaV1.Result :-)
|
|
20
|
+
return validator_js_1.ValidationContext.validate(value, this.validator, {
|
|
21
|
+
...options?.libraryOptions,
|
|
22
|
+
mode: 'parse',
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.StandardSchemaAdapter = StandardSchemaAdapter;
|
|
27
|
+
//# sourceMappingURL=standard-schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard-schema.js","sourceRoot":"","sources":["../../src/core/standard-schema.ts"],"names":[],"mappings":";;;AACA,iDAA6D;AAE7D;;GAEG;AACH,MAAa,qBAAqB;IASH;IANpB,OAAO,GAAG,CAAC,CAAA;IAEX,MAAM,GAAG,qBAAqB,CAAA;IAIvC,YAA6B,SAAqC;QAArC,cAAS,GAAT,SAAS,CAA4B;IAAG,CAAC;IAEtE,QAAQ,CACN,KAAc,EACd,OAAkC;QAElC,0EAA0E;QAC1E,qEAAqE;QACrE,0DAA0D;QAC1D,8BAA8B;QAC9B,OAAO,gCAAiB,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,EAAE;YACvD,GAAG,OAAO,EAAE,cAAc;YAC1B,IAAI,EAAE,OAAO;SACd,CAAC,CAAA;IACJ,CAAC;CACF;AAxBD,sDAwBC","sourcesContent":["import { StandardSchemaV1 } from '@standard-schema/spec'\nimport { ValidationContext, Validator } from './validator.js'\n\n/**\n * The Standard Schema adapter for {@link Validator} instances.\n */\nexport class StandardSchemaAdapter<TInput, TOutput>\n implements StandardSchemaV1.Props<TInput, TOutput>\n{\n readonly version = 1\n\n readonly vendor = '@atproto/lex-schema'\n\n declare readonly types: StandardSchemaV1.Types<TInput, TOutput>\n\n constructor(private readonly validator: Validator<TInput, TOutput>) {}\n\n validate(\n value: unknown,\n options?: StandardSchemaV1.Options,\n ): StandardSchemaV1.Result<TOutput> {\n // Perform validation in \"parse\" mode to ensure transformations (defaults,\n // coercions, etc.) are applied. Also ensures that the output type is\n // returned. Note that ValidationResult is compatible with\n // StandardSchemaV1.Result :-)\n return ValidationContext.validate(value, this.validator, {\n ...options?.libraryOptions,\n mode: 'parse',\n })\n }\n}\n"]}
|
|
@@ -1,19 +1,9 @@
|
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* @returns `true` if the value is a valid AT identifier
|
|
8
|
-
*/
|
|
9
|
-
export declare const isAtIdentifierString: CheckFn<AtIdentifierString>;
|
|
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, };
|
|
3
|
+
export { type AtIdentifierString, asAtIdentifierString, ifAtIdentifierString, isAtIdentifierString, } from '@atproto/syntax';
|
|
4
|
+
export { isDidIdentifier, isHandleIdentifier } from '@atproto/syntax';
|
|
5
|
+
export { type DatetimeString, asDatetimeString, ifDatetimeString, isDatetimeString, } from '@atproto/syntax';
|
|
6
|
+
export { currentDatetimeString, toDatetimeString } from '@atproto/syntax';
|
|
17
7
|
/**
|
|
18
8
|
* Type guard that checks if a value is a valid AT URI.
|
|
19
9
|
*
|
|
@@ -43,20 +33,6 @@ export declare const isCidString: CheckFn<CidString>;
|
|
|
43
33
|
* @example `"bafyreig..."`
|
|
44
34
|
*/
|
|
45
35
|
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
36
|
/**
|
|
61
37
|
* Type guard that checks if a value is a valid DID string.
|
|
62
38
|
*
|