@atproto/lex-schema 0.0.12 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/dist/core/schema.d.ts +4 -4
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +1 -1
- package/dist/core/schema.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 +10 -2
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +21 -3
- 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 +84 -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 +13 -17
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +1 -0
- 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 +11 -15
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +2 -1
- 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 +1 -0
- 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 +2 -2
- package/src/core/schema.ts +9 -3
- package/src/core/validation-issue.ts +3 -2
- package/src/core/validator.ts +23 -3
- 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 +82 -43
- package/src/schema/params.ts +133 -39
- package/src/schema/payload.test.ts +2 -2
- package/src/schema/payload.ts +3 -4
- package/src/schema/record.ts +19 -8
- 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.ts +19 -8
- package/src/schema/typed-ref.ts +2 -0
- package/src/schema/typed-union.ts +3 -1
- package/src/schema/union.ts +2 -0
- 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
|
@@ -15,6 +15,13 @@ import {
|
|
|
15
15
|
Validator,
|
|
16
16
|
} from '../core.js'
|
|
17
17
|
|
|
18
|
+
export type MaybeTypedObject<
|
|
19
|
+
TType extends $Type,
|
|
20
|
+
TValue extends { $type?: unknown } = { $type?: unknown },
|
|
21
|
+
> = TValue extends { $type?: TType }
|
|
22
|
+
? TValue
|
|
23
|
+
: $TypedMaybe<Exclude<TValue, Unknown$TypedObject>, TType>
|
|
24
|
+
|
|
18
25
|
/**
|
|
19
26
|
* Schema for typed objects in Lexicon unions.
|
|
20
27
|
*
|
|
@@ -40,6 +47,8 @@ export class TypedObjectSchema<
|
|
|
40
47
|
$TypedMaybe<InferInput<TShape>, TType>,
|
|
41
48
|
$TypedMaybe<InferOutput<TShape>, TType>
|
|
42
49
|
> {
|
|
50
|
+
readonly type = 'typedObject' as const
|
|
51
|
+
|
|
43
52
|
constructor(
|
|
44
53
|
readonly $type: TType,
|
|
45
54
|
readonly schema: TShape,
|
|
@@ -47,11 +56,9 @@ export class TypedObjectSchema<
|
|
|
47
56
|
super()
|
|
48
57
|
}
|
|
49
58
|
|
|
50
|
-
isTypeOf<
|
|
51
|
-
value:
|
|
52
|
-
): value is
|
|
53
|
-
? X
|
|
54
|
-
: $TypedMaybe<Exclude<X, Unknown$TypedObject>, TType> {
|
|
59
|
+
isTypeOf<TValue extends Record<string, unknown>>(
|
|
60
|
+
value: TValue,
|
|
61
|
+
): value is MaybeTypedObject<TType, TValue> {
|
|
55
62
|
return value.$type === undefined || value.$type === this.$type
|
|
56
63
|
}
|
|
57
64
|
|
|
@@ -64,17 +71,21 @@ export class TypedObjectSchema<
|
|
|
64
71
|
>
|
|
65
72
|
}
|
|
66
73
|
|
|
67
|
-
$isTypeOf<
|
|
74
|
+
$isTypeOf<TValue extends Record<string, unknown>>(
|
|
75
|
+
value: TValue,
|
|
76
|
+
): value is MaybeTypedObject<TType, TValue> {
|
|
68
77
|
return this.isTypeOf(value)
|
|
69
78
|
}
|
|
70
79
|
|
|
71
|
-
$build(
|
|
80
|
+
$build(
|
|
81
|
+
input: Omit<InferInput<this>, '$type'>,
|
|
82
|
+
): $Typed<InferOutput<this>, TType> {
|
|
72
83
|
return this.build(input)
|
|
73
84
|
}
|
|
74
85
|
|
|
75
86
|
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
76
87
|
if (!isPlainObject(input)) {
|
|
77
|
-
return ctx.
|
|
88
|
+
return ctx.issueUnexpectedType(input, 'object')
|
|
78
89
|
}
|
|
79
90
|
|
|
80
91
|
if (
|
package/src/schema/typed-ref.ts
CHANGED
|
@@ -42,6 +42,8 @@ export class TypedUnionSchema<
|
|
|
42
42
|
? InferOutput<TValidators[number]>
|
|
43
43
|
: InferOutput<TValidators[number]> | Unknown$TypedObject
|
|
44
44
|
> {
|
|
45
|
+
readonly type = 'typedUnion' as const
|
|
46
|
+
|
|
45
47
|
constructor(
|
|
46
48
|
protected readonly validators: TValidators,
|
|
47
49
|
public readonly closed: TClosed,
|
|
@@ -67,7 +69,7 @@ export class TypedUnionSchema<
|
|
|
67
69
|
|
|
68
70
|
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
69
71
|
if (!isPlainObject(input) || !('$type' in input)) {
|
|
70
|
-
return ctx.
|
|
72
|
+
return ctx.issueUnexpectedType(input, '$typed')
|
|
71
73
|
}
|
|
72
74
|
|
|
73
75
|
const { $type } = input
|
package/src/schema/union.ts
CHANGED
package/src/schema/unknown.ts
CHANGED
|
@@ -15,6 +15,8 @@ import { memoizedOptions } from '../util/memoize.js'
|
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
17
|
export class UnknownSchema extends Schema<unknown> {
|
|
18
|
+
readonly type = 'unknown' as const
|
|
19
|
+
|
|
18
20
|
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
19
21
|
return ctx.success(input)
|
|
20
22
|
}
|
|
@@ -25,6 +25,8 @@ import {
|
|
|
25
25
|
export class WithDefaultSchema<
|
|
26
26
|
const TValidator extends Validator,
|
|
27
27
|
> extends Schema<InferInput<TValidator>, InferOutput<TValidator>> {
|
|
28
|
+
readonly type = 'withDefault' as const
|
|
29
|
+
|
|
28
30
|
constructor(
|
|
29
31
|
readonly validator: TValidator,
|
|
30
32
|
readonly defaultValue: InferInput<TValidator>,
|
package/src/schema.ts
CHANGED
|
@@ -7,13 +7,14 @@ export * from './schema/cid.js'
|
|
|
7
7
|
export * from './schema/dict.js'
|
|
8
8
|
export * from './schema/enum.js'
|
|
9
9
|
export * from './schema/integer.js'
|
|
10
|
+
export * from './schema/lex-map.js'
|
|
11
|
+
export * from './schema/lex-value.js'
|
|
10
12
|
export * from './schema/literal.js'
|
|
11
13
|
export * from './schema/never.js'
|
|
12
14
|
export * from './schema/null.js'
|
|
13
15
|
export * from './schema/object.js'
|
|
14
16
|
export * from './schema/regexp.js'
|
|
15
17
|
export * from './schema/string.js'
|
|
16
|
-
export * from './schema/unknown-object.js'
|
|
17
18
|
export * from './schema/unknown.js'
|
|
18
19
|
|
|
19
20
|
// Composite Types
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { LexMap } from '@atproto/lex-data';
|
|
2
|
-
import { Schema, ValidationContext } from '../core.js';
|
|
3
|
-
/**
|
|
4
|
-
* Type alias for a plain object with unknown values.
|
|
5
|
-
*/
|
|
6
|
-
export type UnknownObject = LexMap;
|
|
7
|
-
/**
|
|
8
|
-
* Schema that accepts any plain object without validating its properties.
|
|
9
|
-
*
|
|
10
|
-
* Validates that the input is a plain object (not an array, Date, or other
|
|
11
|
-
* special object type), but does not validate the object's properties.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* const schema = new UnknownObjectSchema()
|
|
16
|
-
* schema.validate({ any: 'props' }) // success
|
|
17
|
-
* schema.validate([1, 2, 3]) // fails - arrays not accepted
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export declare class UnknownObjectSchema extends Schema<UnknownObject> {
|
|
21
|
-
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<LexMap>;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Creates a schema that accepts any plain object.
|
|
25
|
-
*
|
|
26
|
-
* Unlike `l.unknown()` which accepts any value, this validates that the input
|
|
27
|
-
* is specifically a plain object (not an array, null, or primitive).
|
|
28
|
-
*
|
|
29
|
-
* @returns A new {@link UnknownObjectSchema} instance
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```ts
|
|
33
|
-
* // Accept any object shape
|
|
34
|
-
* const metadataSchema = l.unknownObject()
|
|
35
|
-
*
|
|
36
|
-
* metadataSchema.parse({ foo: 1, bar: 'baz' }) // success
|
|
37
|
-
* metadataSchema.parse([1, 2, 3]) // throws - not a plain object
|
|
38
|
-
* metadataSchema.parse(null) // throws
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
export declare const unknownObject: () => UnknownObjectSchema;
|
|
42
|
-
//# sourceMappingURL=unknown-object.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unknown-object.d.ts","sourceRoot":"","sources":["../../src/schema/unknown-object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAY,MAAM,mBAAmB,CAAA;AACpD,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAGtD;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAA;AAElC;;;;;;;;;;;;GAYG;AACH,qBAAa,mBAAoB,SAAQ,MAAM,CAAC,aAAa,CAAC;IAC5D,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAOzD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,aAAa,2BAExB,CAAA"}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.unknownObject = exports.UnknownObjectSchema = void 0;
|
|
4
|
-
const lex_data_1 = require("@atproto/lex-data");
|
|
5
|
-
const core_js_1 = require("../core.js");
|
|
6
|
-
const memoize_js_1 = require("../util/memoize.js");
|
|
7
|
-
/**
|
|
8
|
-
* Schema that accepts any plain object without validating its properties.
|
|
9
|
-
*
|
|
10
|
-
* Validates that the input is a plain object (not an array, Date, or other
|
|
11
|
-
* special object type), but does not validate the object's properties.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* const schema = new UnknownObjectSchema()
|
|
16
|
-
* schema.validate({ any: 'props' }) // success
|
|
17
|
-
* schema.validate([1, 2, 3]) // fails - arrays not accepted
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
class UnknownObjectSchema extends core_js_1.Schema {
|
|
21
|
-
validateInContext(input, ctx) {
|
|
22
|
-
if ((0, lex_data_1.isLexMap)(input)) {
|
|
23
|
-
return ctx.success(input);
|
|
24
|
-
}
|
|
25
|
-
return ctx.issueInvalidType(input, 'unknown');
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
exports.UnknownObjectSchema = UnknownObjectSchema;
|
|
29
|
-
/**
|
|
30
|
-
* Creates a schema that accepts any plain object.
|
|
31
|
-
*
|
|
32
|
-
* Unlike `l.unknown()` which accepts any value, this validates that the input
|
|
33
|
-
* is specifically a plain object (not an array, null, or primitive).
|
|
34
|
-
*
|
|
35
|
-
* @returns A new {@link UnknownObjectSchema} instance
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```ts
|
|
39
|
-
* // Accept any object shape
|
|
40
|
-
* const metadataSchema = l.unknownObject()
|
|
41
|
-
*
|
|
42
|
-
* metadataSchema.parse({ foo: 1, bar: 'baz' }) // success
|
|
43
|
-
* metadataSchema.parse([1, 2, 3]) // throws - not a plain object
|
|
44
|
-
* metadataSchema.parse(null) // throws
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
exports.unknownObject = (0, memoize_js_1.memoizedOptions)(function () {
|
|
48
|
-
return new UnknownObjectSchema();
|
|
49
|
-
});
|
|
50
|
-
//# sourceMappingURL=unknown-object.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unknown-object.js","sourceRoot":"","sources":["../../src/schema/unknown-object.ts"],"names":[],"mappings":";;;AAAA,gDAAoD;AACpD,wCAAsD;AACtD,mDAAoD;AAOpD;;;;;;;;;;;;GAYG;AACH,MAAa,mBAAoB,SAAQ,gBAAqB;IAC5D,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,IAAA,mBAAQ,EAAC,KAAK,CAAC,EAAE,CAAC;YACpB,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAC3B,CAAC;QAED,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;IAC/C,CAAC;CACF;AARD,kDAQC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACU,QAAA,aAAa,GAAiB,IAAA,4BAAe,EAAC;IACzD,OAAO,IAAI,mBAAmB,EAAE,CAAA;AAClC,CAAC,CAAC,CAAA","sourcesContent":["import { LexMap, isLexMap } from '@atproto/lex-data'\nimport { Schema, ValidationContext } from '../core.js'\nimport { memoizedOptions } from '../util/memoize.js'\n\n/**\n * Type alias for a plain object with unknown values.\n */\nexport type UnknownObject = LexMap\n\n/**\n * Schema that accepts any plain object without validating its properties.\n *\n * Validates that the input is a plain object (not an array, Date, or other\n * special object type), but does not validate the object's properties.\n *\n * @example\n * ```ts\n * const schema = new UnknownObjectSchema()\n * schema.validate({ any: 'props' }) // success\n * schema.validate([1, 2, 3]) // fails - arrays not accepted\n * ```\n */\nexport class UnknownObjectSchema extends Schema<UnknownObject> {\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (isLexMap(input)) {\n return ctx.success(input)\n }\n\n return ctx.issueInvalidType(input, 'unknown')\n }\n}\n\n/**\n * Creates a schema that accepts any plain object.\n *\n * Unlike `l.unknown()` which accepts any value, this validates that the input\n * is specifically a plain object (not an array, null, or primitive).\n *\n * @returns A new {@link UnknownObjectSchema} instance\n *\n * @example\n * ```ts\n * // Accept any object shape\n * const metadataSchema = l.unknownObject()\n *\n * metadataSchema.parse({ foo: 1, bar: 'baz' }) // success\n * metadataSchema.parse([1, 2, 3]) // throws - not a plain object\n * metadataSchema.parse(null) // throws\n * ```\n */\nexport const unknownObject = /*#__PURE__*/ memoizedOptions(function () {\n return new UnknownObjectSchema()\n})\n"]}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { LexMap, isLexMap } from '@atproto/lex-data'
|
|
2
|
-
import { Schema, ValidationContext } from '../core.js'
|
|
3
|
-
import { memoizedOptions } from '../util/memoize.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Type alias for a plain object with unknown values.
|
|
7
|
-
*/
|
|
8
|
-
export type UnknownObject = LexMap
|
|
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
|
-
*/
|
|
23
|
-
export class UnknownObjectSchema extends Schema<UnknownObject> {
|
|
24
|
-
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
25
|
-
if (isLexMap(input)) {
|
|
26
|
-
return ctx.success(input)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return ctx.issueInvalidType(input, 'unknown')
|
|
30
|
-
}
|
|
31
|
-
}
|
|
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
|
-
*/
|
|
51
|
-
export const unknownObject = /*#__PURE__*/ memoizedOptions(function () {
|
|
52
|
-
return new UnknownObjectSchema()
|
|
53
|
-
})
|