@atproto/lex-schema 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/dist/core/$type.d.ts +149 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +44 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +44 -0
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js +30 -0
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/result.d.ts +85 -4
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +60 -4
- package/dist/core/result.js.map +1 -1
- package/dist/core/schema.d.ts +229 -2
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +197 -4
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +244 -11
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +150 -0
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.d.ts +90 -3
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/validation-error.d.ts +61 -1
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +60 -0
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.d.ts +61 -0
- package/dist/core/validation-issue.d.ts.map +1 -1
- package/dist/core/validation-issue.js +51 -0
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +347 -10
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +184 -3
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +13 -25
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +2 -2
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +45 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +14 -0
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +46 -0
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +39 -0
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +28 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +28 -0
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +38 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +32 -0
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +38 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +33 -0
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +66 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +54 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +44 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +44 -0
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +58 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +45 -0
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +48 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +48 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +42 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +36 -0
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +54 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +49 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +44 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +44 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +42 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +42 -0
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +29 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +29 -0
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +41 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +41 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +56 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +51 -0
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +42 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +42 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +89 -7
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +84 -10
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +111 -15
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +70 -0
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +58 -0
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +50 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +42 -0
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +39 -0
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +64 -0
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +64 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +55 -0
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +55 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +63 -8
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +20 -0
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +50 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +17 -0
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +58 -9
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +44 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +44 -0
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +50 -0
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +41 -0
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +72 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +59 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +47 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +47 -0
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +62 -8
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +18 -0
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +53 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +15 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +50 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +50 -1
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +45 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +40 -0
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +34 -0
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +31 -0
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +33 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +33 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +44 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +44 -0
- package/dist/schema/with-default.js.map +1 -1
- package/package.json +4 -4
- package/src/core/$type.ts +150 -18
- package/src/core/record-key.ts +44 -0
- package/src/core/result.ts +86 -4
- package/src/core/schema.ts +236 -7
- package/src/core/string-format.ts +259 -13
- package/src/core/types.ts +91 -3
- package/src/core/validation-error.ts +60 -0
- package/src/core/validation-issue.ts +65 -0
- package/src/core/validator.ts +351 -10
- package/src/helpers.test.ts +110 -29
- package/src/helpers.ts +14 -14
- package/src/schema/array.test.ts +94 -79
- package/src/schema/array.ts +45 -0
- package/src/schema/blob.ts +46 -0
- package/src/schema/boolean.ts +28 -0
- package/src/schema/bytes.ts +38 -0
- package/src/schema/cid.ts +38 -0
- package/src/schema/custom.ts +66 -1
- package/src/schema/dict.ts +44 -0
- package/src/schema/discriminated-union.ts +58 -0
- package/src/schema/enum.ts +48 -0
- package/src/schema/integer.ts +42 -0
- package/src/schema/intersection.ts +54 -0
- package/src/schema/literal.ts +44 -0
- package/src/schema/never.ts +42 -0
- package/src/schema/null.ts +29 -0
- package/src/schema/nullable.ts +41 -0
- package/src/schema/object.ts +56 -0
- package/src/schema/optional.ts +42 -0
- package/src/schema/params.test.ts +58 -2
- package/src/schema/params.ts +124 -16
- package/src/schema/payload.test.ts +3 -3
- package/src/schema/payload.ts +142 -38
- package/src/schema/permission-set.ts +58 -0
- package/src/schema/permission.ts +42 -0
- package/src/schema/procedure.ts +64 -0
- package/src/schema/query.ts +55 -0
- package/src/schema/record.ts +63 -8
- package/src/schema/ref.ts +50 -0
- package/src/schema/refine.ts +58 -9
- package/src/schema/regexp.ts +44 -0
- package/src/schema/string.ts +50 -0
- package/src/schema/subscription.ts +72 -2
- package/src/schema/token.ts +47 -0
- package/src/schema/typed-object.ts +62 -8
- package/src/schema/typed-ref.ts +53 -0
- package/src/schema/typed-union.ts +55 -2
- package/src/schema/union.ts +45 -0
- package/src/schema/unknown-object.ts +34 -0
- package/src/schema/unknown.ts +33 -0
- package/src/schema/with-default.ts +44 -0
|
@@ -2,8 +2,24 @@ import { LexMap, isLexMap } from '@atproto/lex-data'
|
|
|
2
2
|
import { Schema, ValidationContext } from '../core.js'
|
|
3
3
|
import { memoizedOptions } from '../util/memoize.js'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Type alias for a plain object with unknown values.
|
|
7
|
+
*/
|
|
5
8
|
export type UnknownObject = LexMap
|
|
6
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Schema that accepts any plain object without validating its properties.
|
|
12
|
+
*
|
|
13
|
+
* Validates that the input is a plain object (not an array, Date, or other
|
|
14
|
+
* special object type), but does not validate the object's properties.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const schema = new UnknownObjectSchema()
|
|
19
|
+
* schema.validate({ any: 'props' }) // success
|
|
20
|
+
* schema.validate([1, 2, 3]) // fails - arrays not accepted
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
7
23
|
export class UnknownObjectSchema extends Schema<UnknownObject> {
|
|
8
24
|
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
9
25
|
if (isLexMap(input)) {
|
|
@@ -14,6 +30,24 @@ export class UnknownObjectSchema extends Schema<UnknownObject> {
|
|
|
14
30
|
}
|
|
15
31
|
}
|
|
16
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Creates a schema that accepts any plain object.
|
|
35
|
+
*
|
|
36
|
+
* Unlike `l.unknown()` which accepts any value, this validates that the input
|
|
37
|
+
* is specifically a plain object (not an array, null, or primitive).
|
|
38
|
+
*
|
|
39
|
+
* @returns A new {@link UnknownObjectSchema} instance
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* // Accept any object shape
|
|
44
|
+
* const metadataSchema = l.unknownObject()
|
|
45
|
+
*
|
|
46
|
+
* metadataSchema.parse({ foo: 1, bar: 'baz' }) // success
|
|
47
|
+
* metadataSchema.parse([1, 2, 3]) // throws - not a plain object
|
|
48
|
+
* metadataSchema.parse(null) // throws
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
17
51
|
export const unknownObject = /*#__PURE__*/ memoizedOptions(function () {
|
|
18
52
|
return new UnknownObjectSchema()
|
|
19
53
|
})
|
package/src/schema/unknown.ts
CHANGED
|
@@ -1,12 +1,45 @@
|
|
|
1
1
|
import { Schema, ValidationContext } from '../core.js'
|
|
2
2
|
import { memoizedOptions } from '../util/memoize.js'
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Schema that accepts any value without validation.
|
|
6
|
+
*
|
|
7
|
+
* Passes through any input unchanged. Use sparingly as it bypasses
|
|
8
|
+
* type safety. Useful for dynamic data or when the schema is not
|
|
9
|
+
* known at compile time.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const schema = new UnknownSchema()
|
|
14
|
+
* schema.validate(anything) // always succeeds
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
4
17
|
export class UnknownSchema extends Schema<unknown> {
|
|
5
18
|
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
6
19
|
return ctx.success(input)
|
|
7
20
|
}
|
|
8
21
|
}
|
|
9
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Creates an unknown schema that accepts any value.
|
|
25
|
+
*
|
|
26
|
+
* The value passes through without any validation or transformation.
|
|
27
|
+
* Use this when you need to accept arbitrary data.
|
|
28
|
+
*
|
|
29
|
+
* @returns A new {@link UnknownSchema} instance
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* // Accept any value
|
|
34
|
+
* const anyDataSchema = l.unknown()
|
|
35
|
+
*
|
|
36
|
+
* // In an object with a dynamic field
|
|
37
|
+
* const flexibleSchema = l.object({
|
|
38
|
+
* type: l.string(),
|
|
39
|
+
* data: l.unknown(),
|
|
40
|
+
* })
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
10
43
|
export const unknown = /*#__PURE__*/ memoizedOptions(function () {
|
|
11
44
|
return new UnknownSchema()
|
|
12
45
|
})
|
|
@@ -6,6 +6,22 @@ import {
|
|
|
6
6
|
Validator,
|
|
7
7
|
} from '../core.js'
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Schema wrapper that provides a default value when the input is undefined.
|
|
11
|
+
*
|
|
12
|
+
* In parse mode, when the input is `undefined`, the default value is used
|
|
13
|
+
* instead. In validate mode, undefined values pass through unchanged (the
|
|
14
|
+
* default is not applied).
|
|
15
|
+
*
|
|
16
|
+
* @template TValidator - The wrapped validator type
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const schema = new WithDefaultSchema(l.integer(), 0)
|
|
21
|
+
* schema.parse(undefined) // 0
|
|
22
|
+
* schema.parse(42) // 42
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
9
25
|
export class WithDefaultSchema<
|
|
10
26
|
const TValidator extends Validator,
|
|
11
27
|
> extends Schema<InferInput<TValidator>, InferOutput<TValidator>> {
|
|
@@ -27,6 +43,34 @@ export class WithDefaultSchema<
|
|
|
27
43
|
}
|
|
28
44
|
}
|
|
29
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Creates a schema that applies a default value when the input is undefined.
|
|
48
|
+
*
|
|
49
|
+
* Commonly used with `optional()` to provide fallback values for missing
|
|
50
|
+
* properties. The default value is validated against the schema.
|
|
51
|
+
*
|
|
52
|
+
* @param validator - The validator for the value
|
|
53
|
+
* @param defaultValue - The default value to use when input is undefined
|
|
54
|
+
* @returns A new {@link WithDefaultSchema} instance
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* // Integer with default
|
|
59
|
+
* const countSchema = l.withDefault(l.integer(), 0)
|
|
60
|
+
* countSchema.parse(undefined) // 0
|
|
61
|
+
* countSchema.parse(5) // 5
|
|
62
|
+
*
|
|
63
|
+
* // Commonly combined with optional in objects
|
|
64
|
+
* const settingsSchema = l.object({
|
|
65
|
+
* theme: l.optional(l.withDefault(l.string(), 'light')),
|
|
66
|
+
* pageSize: l.optional(l.withDefault(l.integer(), 25)),
|
|
67
|
+
* })
|
|
68
|
+
* settingsSchema.parse({}) // { theme: 'light', pageSize: 25 }
|
|
69
|
+
*
|
|
70
|
+
* // Boolean with default
|
|
71
|
+
* const enabledSchema = l.withDefault(l.boolean(), false)
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
30
74
|
export function withDefault<const TValidator extends Validator>(
|
|
31
75
|
validator: TValidator,
|
|
32
76
|
defaultValue: InferInput<TValidator>,
|