@atproto/lex-schema 0.0.5 → 0.0.6
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 +21 -0
- package/dist/core/property-key.d.ts.map +1 -0
- package/dist/core/property-key.js.map +1 -0
- package/dist/core/schema.d.ts.map +1 -0
- package/dist/core/schema.js.map +1 -0
- package/dist/core/validation-error.d.ts +19 -0
- package/dist/core/validation-error.d.ts.map +1 -0
- package/dist/{validation → core}/validation-error.js +12 -5
- package/dist/core/validation-error.js.map +1 -0
- package/dist/{validation → core}/validation-issue.d.ts +43 -0
- package/dist/core/validation-issue.d.ts.map +1 -0
- package/dist/{validation → core}/validation-issue.js +45 -0
- package/dist/core/validation-issue.js.map +1 -0
- package/dist/{validation → core}/validator.d.ts +1 -1
- package/dist/core/validator.d.ts.map +1 -0
- package/dist/{validation → core}/validator.js +4 -4
- package/dist/core/validator.js.map +1 -0
- package/dist/core.d.ts +5 -0
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +5 -0
- package/dist/core.js.map +1 -1
- package/dist/external.d.ts +15 -16
- package/dist/external.d.ts.map +1 -1
- package/dist/external.js +38 -55
- package/dist/external.js.map +1 -1
- package/dist/helpers.d.ts +7 -2
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +10 -0
- package/dist/helpers.js.map +1 -1
- package/dist/schema/_parameters.d.ts +1 -1
- package/dist/schema/_parameters.d.ts.map +1 -1
- package/dist/schema/_parameters.js.map +1 -1
- package/dist/schema/array.d.ts +1 -1
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +2 -2
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +1 -1
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +2 -2
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +1 -1
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +2 -2
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +1 -1
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +2 -2
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +1 -1
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +2 -2
- package/dist/schema/cid.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 +3 -3
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +1 -1
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +2 -2
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +1 -1
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +2 -2
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +1 -1
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +2 -2
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +1 -1
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +4 -4
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +1 -2
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +2 -2
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +1 -1
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +2 -2
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +1 -1
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +2 -2
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +1 -1
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +2 -2
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +1 -1
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +2 -2
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +1 -2
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +2 -2
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +1 -1
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +2 -2
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +1 -2
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +2 -2
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +1 -1
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/record.d.ts +1 -2
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +2 -2
- 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 +2 -2
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +18 -1
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js +2 -2
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +1 -1
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +2 -2
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +1 -2
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +1 -2
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +1 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +1 -1
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +2 -2
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +1 -2
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +2 -2
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +1 -1
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +2 -2
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +2 -3
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +2 -2
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +1 -1
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +3 -3
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +1 -2
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +2 -2
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +1 -1
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +2 -2
- package/dist/schema/unknown.js.map +1 -1
- package/dist/util/memoize.d.ts +3 -0
- package/dist/util/memoize.d.ts.map +1 -0
- package/dist/util/memoize.js +52 -0
- package/dist/util/memoize.js.map +1 -0
- package/package.json +6 -6
- package/src/{validation → core}/validation-error.ts +11 -3
- package/src/{validation → core}/validation-issue.ts +52 -0
- package/src/{validation → core}/validator.ts +1 -1
- package/src/core.ts +5 -0
- package/src/external.ts +67 -48
- package/src/helpers.test.ts +1 -0
- package/src/helpers.ts +16 -2
- package/src/schema/_parameters.test.ts +1 -0
- package/src/schema/_parameters.ts +1 -1
- package/src/schema/array.test.ts +1 -0
- package/src/schema/array.ts +1 -1
- package/src/schema/blob.test.ts +1 -0
- package/src/schema/blob.ts +1 -1
- package/src/schema/boolean.test.ts +1 -0
- package/src/schema/boolean.ts +1 -1
- package/src/schema/bytes.test.ts +1 -0
- package/src/schema/bytes.ts +1 -1
- package/src/schema/cid.test.ts +1 -0
- package/src/schema/cid.ts +1 -1
- package/src/schema/custom.test.ts +3 -2
- package/src/schema/custom.ts +1 -1
- package/src/schema/dict.test.ts +1 -0
- package/src/schema/dict.ts +1 -1
- package/src/schema/discriminated-union.test.ts +1 -0
- package/src/schema/discriminated-union.ts +1 -1
- package/src/schema/enum.test.ts +1 -0
- package/src/schema/enum.ts +1 -1
- package/src/schema/integer.test.ts +1 -0
- package/src/schema/integer.ts +3 -3
- package/src/schema/intersection.test.ts +1 -0
- package/src/schema/intersection.ts +2 -2
- package/src/schema/literal.test.ts +1 -0
- package/src/schema/literal.ts +1 -1
- package/src/schema/never.test.ts +1 -0
- package/src/schema/never.ts +1 -1
- package/src/schema/null.test.ts +1 -0
- package/src/schema/null.ts +1 -1
- package/src/schema/nullable.test.ts +1 -0
- package/src/schema/nullable.ts +1 -1
- package/src/schema/object.test.ts +1 -0
- package/src/schema/object.ts +3 -3
- package/src/schema/optional.test.ts +1 -0
- package/src/schema/optional.ts +1 -1
- package/src/schema/params.test.ts +1 -0
- package/src/schema/params.ts +3 -3
- package/src/schema/payload.test.ts +1 -0
- package/src/schema/payload.ts +1 -1
- package/src/schema/permission-set.test.ts +1 -0
- package/src/schema/permission.test.ts +1 -0
- package/src/schema/procedure.test.ts +1 -0
- package/src/schema/query.test.ts +1 -0
- package/src/schema/record.test.ts +1 -0
- package/src/schema/record.ts +5 -2
- package/src/schema/ref.test.ts +2 -1
- package/src/schema/ref.ts +1 -1
- package/src/schema/refine.test.ts +1 -0
- package/src/schema/refine.ts +18 -1
- package/src/schema/regexp.test.ts +1 -0
- package/src/schema/regexp.ts +1 -1
- package/src/schema/string.test.ts +1 -0
- package/src/schema/string.ts +8 -2
- package/src/schema/subscription.test.ts +1 -0
- package/src/schema/subscription.ts +1 -2
- package/src/schema/token.test.ts +1 -0
- package/src/schema/token.ts +1 -1
- package/src/schema/typed-object.test.ts +1 -0
- package/src/schema/typed-object.ts +3 -2
- package/src/schema/typed-ref.test.ts +1 -0
- package/src/schema/typed-ref.ts +1 -1
- package/src/schema/typed-union.test.ts +1 -0
- package/src/schema/typed-union.ts +4 -4
- package/src/schema/union.test.ts +1 -0
- package/src/schema/union.ts +1 -1
- package/src/schema/unknown-object.test.ts +1 -0
- package/src/schema/unknown-object.ts +1 -2
- package/src/schema/unknown.test.ts +1 -0
- package/src/schema/unknown.ts +1 -1
- package/src/util/array-agg.test.ts +1 -0
- package/src/util/memoize.ts +57 -0
- package/tsconfig.tests.json +1 -1
- package/dist/validation/property-key.d.ts.map +0 -1
- package/dist/validation/property-key.js.map +0 -1
- package/dist/validation/schema.d.ts.map +0 -1
- package/dist/validation/schema.js.map +0 -1
- package/dist/validation/validation-error.d.ts +0 -9
- package/dist/validation/validation-error.d.ts.map +0 -1
- package/dist/validation/validation-error.js.map +0 -1
- package/dist/validation/validation-issue.d.ts.map +0 -1
- package/dist/validation/validation-issue.js.map +0 -1
- package/dist/validation/validator.d.ts.map +0 -1
- package/dist/validation/validator.js.map +0 -1
- package/dist/validation.d.ts +0 -6
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js +0 -9
- package/dist/validation.js.map +0 -1
- package/src/validation.ts +0 -5
- /package/dist/{validation → core}/property-key.d.ts +0 -0
- /package/dist/{validation → core}/property-key.js +0 -0
- /package/dist/{validation → core}/schema.d.ts +0 -0
- /package/dist/{validation → core}/schema.js +0 -0
- /package/src/{validation → core}/property-key.ts +0 -0
- /package/src/{validation → core}/schema.ts +0 -0
package/src/schema/dict.test.ts
CHANGED
package/src/schema/dict.ts
CHANGED
package/src/schema/enum.test.ts
CHANGED
package/src/schema/enum.ts
CHANGED
package/src/schema/integer.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Schema, ValidationResult, ValidatorContext } from '../
|
|
1
|
+
import { Schema, ValidationResult, ValidatorContext } from '../core.js'
|
|
2
2
|
|
|
3
3
|
export type IntegerSchemaOptions = {
|
|
4
4
|
default?: number
|
|
@@ -32,8 +32,8 @@ export class IntegerSchema extends Schema<number> {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
|
-
* Simple wrapper around {@link Number.
|
|
35
|
+
* Simple wrapper around {@link Number.isSafeInteger} that acts as a type guard.
|
|
36
36
|
*/
|
|
37
37
|
function isInteger(input: unknown): input is number {
|
|
38
|
-
return Number.
|
|
38
|
+
return Number.isSafeInteger(input)
|
|
39
39
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Simplify } from '../core.js'
|
|
2
1
|
import {
|
|
3
2
|
Infer,
|
|
4
3
|
Schema,
|
|
4
|
+
Simplify,
|
|
5
5
|
ValidationResult,
|
|
6
6
|
ValidatorContext,
|
|
7
|
-
} from '../
|
|
7
|
+
} from '../core.js'
|
|
8
8
|
import { DictSchema } from './dict.js'
|
|
9
9
|
import { ObjectSchema } from './object.js'
|
|
10
10
|
|
package/src/schema/literal.ts
CHANGED
package/src/schema/never.test.ts
CHANGED
package/src/schema/never.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Schema, ValidationFailure, ValidatorContext } from '../
|
|
1
|
+
import { Schema, ValidationFailure, ValidatorContext } from '../core.js'
|
|
2
2
|
|
|
3
3
|
export class NeverSchema extends Schema<never> {
|
|
4
4
|
validateInContext(input: unknown, ctx: ValidatorContext): ValidationFailure {
|
package/src/schema/null.test.ts
CHANGED
package/src/schema/null.ts
CHANGED
package/src/schema/nullable.ts
CHANGED
package/src/schema/object.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { isPlainObject } from '@atproto/lex-data'
|
|
2
|
-
import { WithOptionalProperties } from '../core.js'
|
|
3
|
-
import { lazyProperty } from '../util/lazy-property.js'
|
|
4
2
|
import {
|
|
5
3
|
Infer,
|
|
6
4
|
Schema,
|
|
7
5
|
ValidationResult,
|
|
8
6
|
Validator,
|
|
9
7
|
ValidatorContext,
|
|
10
|
-
|
|
8
|
+
WithOptionalProperties,
|
|
9
|
+
} from '../core.js'
|
|
10
|
+
import { lazyProperty } from '../util/lazy-property.js'
|
|
11
11
|
|
|
12
12
|
export type ObjectSchemaShape = Record<string, Validator>
|
|
13
13
|
|
package/src/schema/optional.ts
CHANGED
package/src/schema/params.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { isPlainObject } from '@atproto/lex-data'
|
|
2
|
-
import { WithOptionalProperties } from '../core.js'
|
|
3
|
-
import { lazyProperty } from '../util/lazy-property.js'
|
|
4
2
|
import {
|
|
5
3
|
Infer,
|
|
6
4
|
Schema,
|
|
7
5
|
ValidationResult,
|
|
8
6
|
Validator,
|
|
9
7
|
ValidatorContext,
|
|
10
|
-
|
|
8
|
+
WithOptionalProperties,
|
|
9
|
+
} from '../core.js'
|
|
10
|
+
import { lazyProperty } from '../util/lazy-property.js'
|
|
11
11
|
import { Param, ParamScalar, paramSchema } from './_parameters.js'
|
|
12
12
|
import { StringSchema } from './string.js'
|
|
13
13
|
|
package/src/schema/payload.ts
CHANGED
package/src/schema/query.test.ts
CHANGED
package/src/schema/record.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { LexiconRecordKey, NsidString, Simplify, TidString } from '../core.js'
|
|
2
1
|
import {
|
|
3
2
|
Infer,
|
|
3
|
+
LexiconRecordKey,
|
|
4
|
+
NsidString,
|
|
4
5
|
Schema,
|
|
6
|
+
Simplify,
|
|
7
|
+
TidString,
|
|
5
8
|
ValidationResult,
|
|
6
9
|
Validator,
|
|
7
10
|
ValidatorContext,
|
|
8
|
-
} from '../
|
|
11
|
+
} from '../core.js'
|
|
9
12
|
import { LiteralSchema } from './literal.js'
|
|
10
13
|
import { StringSchema } from './string.js'
|
|
11
14
|
import { TypedObject } from './typed-union.js'
|
package/src/schema/ref.test.ts
CHANGED
package/src/schema/ref.ts
CHANGED
package/src/schema/refine.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
ValidationResult,
|
|
6
6
|
Validator,
|
|
7
7
|
ValidatorContext,
|
|
8
|
-
} from '../
|
|
8
|
+
} from '../core.js'
|
|
9
9
|
import { CustomAssertionContext } from './custom.js'
|
|
10
10
|
|
|
11
11
|
export type RefinementCheck<T> = {
|
|
@@ -31,6 +31,23 @@ export type Refinement<T = any, Out extends T = T> =
|
|
|
31
31
|
| RefinementCheck<T>
|
|
32
32
|
| RefinementAssertion<T, Out>
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Create a refined schema based on an existing schema and a refinement check.
|
|
36
|
+
*
|
|
37
|
+
* @param schema - The base schema to refine.
|
|
38
|
+
* @param refinement - The refinement check to apply.
|
|
39
|
+
* @returns A new schema that includes the refinement.
|
|
40
|
+
* @example
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* const PositiveInt = refine(l.integer(), {
|
|
44
|
+
* check: (value) => value > 0,
|
|
45
|
+
* message: 'Value must be a positive integer',
|
|
46
|
+
* })
|
|
47
|
+
* const result = PositiveInt.validate(-5)
|
|
48
|
+
* // result.success === false
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
34
51
|
export function refine<S extends Validator, Out extends Infer<S>>(
|
|
35
52
|
schema: S,
|
|
36
53
|
refinement: RefinementAssertion<Infer<S>, Out>,
|
package/src/schema/regexp.ts
CHANGED
package/src/schema/string.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { asCid, graphemeLen, utf8Len } from '@atproto/lex-data'
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
InferStringFormat,
|
|
4
|
+
Schema,
|
|
5
|
+
StringFormat,
|
|
6
|
+
ValidationResult,
|
|
7
|
+
ValidatorContext,
|
|
8
|
+
assertStringFormat,
|
|
9
|
+
} from '../core.js'
|
|
4
10
|
import { TokenSchema } from './token.js'
|
|
5
11
|
|
|
6
12
|
export type StringSchemaOptions = {
|
package/src/schema/token.test.ts
CHANGED
package/src/schema/token.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { isPlainObject } from '@atproto/lex-data'
|
|
2
|
-
import { $Type, Simplify } from '../core.js'
|
|
3
2
|
import {
|
|
3
|
+
$Type,
|
|
4
4
|
Infer,
|
|
5
5
|
Schema,
|
|
6
|
+
Simplify,
|
|
6
7
|
ValidationResult,
|
|
7
8
|
Validator,
|
|
8
9
|
ValidatorContext,
|
|
9
|
-
} from '../
|
|
10
|
+
} from '../core.js'
|
|
10
11
|
import { TypedObject } from './typed-union.js'
|
|
11
12
|
|
|
12
13
|
export type TypedObjectSchemaOutput<
|
package/src/schema/typed-ref.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { isPlainObject } from '@atproto/lex-data'
|
|
2
|
-
import { Restricted, UnknownString } from '../core.js'
|
|
3
|
-
import { lazyProperty } from '../util/lazy-property.js'
|
|
4
2
|
import {
|
|
5
3
|
Infer,
|
|
4
|
+
Restricted,
|
|
6
5
|
Schema,
|
|
7
6
|
ValidationResult,
|
|
8
7
|
ValidatorContext,
|
|
9
|
-
} from '../
|
|
8
|
+
} from '../core.js'
|
|
9
|
+
import { lazyProperty } from '../util/lazy-property.js'
|
|
10
10
|
import { TypedRefSchema, TypedRefSchemaOutput } from './typed-ref.js'
|
|
11
11
|
|
|
12
12
|
export type TypedRef<T extends { $type?: string }> = TypedRefSchemaOutput<T>
|
|
13
13
|
|
|
14
|
-
export type TypedObject = { $type:
|
|
14
|
+
export type TypedObject = { $type: string } & {
|
|
15
15
|
// In order to prevent places that expect an open union from accepting an
|
|
16
16
|
// invalid version of the known typed objects, we need to prevent any other
|
|
17
17
|
// properties from being present.
|
package/src/schema/union.test.ts
CHANGED
package/src/schema/union.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
ValidationResult,
|
|
7
7
|
Validator,
|
|
8
8
|
ValidatorContext,
|
|
9
|
-
} from '../
|
|
9
|
+
} from '../core.js'
|
|
10
10
|
|
|
11
11
|
export type UnionSchemaValidators = readonly [Validator, ...Validator[]]
|
|
12
12
|
export type UnionSchemaOutput<V extends readonly Validator[]> = Infer<V[number]>
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { LexMap, isLexMap } from '@atproto/lex-data'
|
|
2
|
-
import { Schema, ValidationResult, ValidatorContext } from '../
|
|
2
|
+
import { Schema, ValidationResult, ValidatorContext } from '../core.js'
|
|
3
3
|
|
|
4
|
-
export type { LexMap }
|
|
5
4
|
export type UnknownObjectOutput = LexMap
|
|
6
5
|
|
|
7
6
|
export class UnknownObjectSchema extends Schema<UnknownObjectOutput> {
|
package/src/schema/unknown.ts
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
2
|
+
export function memoizedOptions<
|
|
3
|
+
F extends (options?: NonNullable<unknown>) => any,
|
|
4
|
+
>(
|
|
5
|
+
fn: F,
|
|
6
|
+
keyFn?: (
|
|
7
|
+
options: NonNullable<Parameters<F>[0]>,
|
|
8
|
+
) => string | number | boolean | null | void,
|
|
9
|
+
): F {
|
|
10
|
+
let emptyOptionsValue: ReturnType<F> | undefined
|
|
11
|
+
|
|
12
|
+
if (keyFn) {
|
|
13
|
+
const cache = new Map<string | number | boolean | null, ReturnType<F>>()
|
|
14
|
+
const fromCache = (
|
|
15
|
+
options: NonNullable<Parameters<F>[0]>,
|
|
16
|
+
): ReturnType<F> => {
|
|
17
|
+
const key = keyFn(options)
|
|
18
|
+
if (key !== undefined) {
|
|
19
|
+
const cached = cache.get(key)
|
|
20
|
+
if (cached) return cached
|
|
21
|
+
const result = fn(options) as ReturnType<F>
|
|
22
|
+
cache.set(key, result)
|
|
23
|
+
return result
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return fn(options)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return ((options: Parameters<F>[0]): ReturnType<F> => {
|
|
30
|
+
// Non-empty options case
|
|
31
|
+
if (options) for (const _ in options) return fromCache(options)
|
|
32
|
+
|
|
33
|
+
// Empty or missing options case
|
|
34
|
+
return (emptyOptionsValue ??= fn(options))
|
|
35
|
+
}) as F
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return ((options: Parameters<F>[0]): ReturnType<F> => {
|
|
39
|
+
// Non-empty options case
|
|
40
|
+
if (options) for (const _ in options) return fn(options)
|
|
41
|
+
|
|
42
|
+
// Empty or missing options case
|
|
43
|
+
return (emptyOptionsValue ??= fn(options))
|
|
44
|
+
}) as F
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
48
|
+
export function memoizedTransformer<F extends (arg: any) => any>(fn: F): F {
|
|
49
|
+
const cache = new WeakMap<object, ReturnType<F>>()
|
|
50
|
+
return ((arg: Parameters<F>[0]): ReturnType<F> => {
|
|
51
|
+
const cached = cache.get(arg)
|
|
52
|
+
if (cached) return cached
|
|
53
|
+
const result = fn(arg) as ReturnType<F>
|
|
54
|
+
cache.set(arg, result)
|
|
55
|
+
return result
|
|
56
|
+
}) as F
|
|
57
|
+
}
|
package/tsconfig.tests.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"property-key.d.ts","sourceRoot":"","sources":["../../src/validation/property-key.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"property-key.js","sourceRoot":"","sources":["../../src/validation/property-key.ts"],"names":[],"mappings":"","sourcesContent":["export type PropertyKey = string | number\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/validation/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EACjB,MAAM,gBAAgB,CAAA;AAEvB,8BAAsB,MAAM,CAAC,MAAM,GAAG,GAAG,CAAE,YAAW,SAAS,CAAC,MAAM,CAAC;IACrE,SAAiB,CAAC,OAAO,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,CAAA;IAE9C,QAAQ,CAAC,iBAAiB,CACxB,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AAM,MAAM,CAAC,MAAM,CAAA;QACxC,OAAO,MAAM,CAAC,KAAK,CAAA;IACrB,CAAC;IAUD,SAAS,CACP,KAAc,EACd,OAA2B;QAE3B,OAAO,+BAAgB,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;IACxD,CAAC;IAED,yEAAyE;IACzE,4EAA4E;IAC5E,6EAA6E;IAC7E,iEAAiE;IACjE,uEAAuE;IACvE,wEAAwE;IACxE,8EAA8E;IAC9E,2EAA2E;IAC3E,6EAA6E;IAC7E,+DAA+D;IAC/D,EAAE;IACF,wFAAwF;IACxF,qGAAqG;IAErG,OAAO,CAAC,KAAc;QACpB,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAC3B,CAAC;IAED,QAAQ,CAAC,KAAc;QACrB,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC5B,CAAC;IAED,UAAU,CAAI,KAAQ;QACpB,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;IAED,MAAM,CAAC,KAAc,EAAE,OAA2B;QAChD,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACnC,CAAC;IAED,UAAU,CACR,KAAc,EACd,OAA2B;QAE3B,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACvC,CAAC;CACF;AA7GD,wBA6GC","sourcesContent":["import {\n ValidationOptions,\n ValidationResult,\n Validator,\n ValidatorContext,\n} from './validator.js'\n\nexport abstract class Schema<Output = any> implements Validator<Output> {\n declare readonly ['__lex']: { output: Output }\n\n abstract validateInContext(\n input: unknown,\n ctx: ValidatorContext,\n ): ValidationResult<Output>\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 Output {\n const result = this.safeParse(input, { allowTransform: false })\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 * `allowTransform: false`.\n */\n cast<I>(input: I): I & Output {\n return this.parse(input, { allowTransform: false })\n }\n\n matches(input: unknown): input is Output {\n const result = this.safeParse(input, { allowTransform: false })\n return result.success\n }\n\n ifMatches<I>(input: I): (I & Output) | undefined {\n return this.matches(input) ? input : undefined\n }\n\n parse<I>(\n input: I,\n options: ValidationOptions & { allowTransform: false },\n ): I & Output\n parse(input: unknown, options?: ValidationOptions): Output\n parse(input: unknown, options?: ValidationOptions): Output {\n const result = this.safeParse(input, options)\n if (!result.success) throw result.reason\n return result.value\n }\n\n safeParse<I>(\n input: I,\n options: ValidationOptions & { allowTransform: false },\n ): ValidationResult<I & Output>\n safeParse(\n input: unknown,\n options?: ValidationOptions,\n ): ValidationResult<Output>\n safeParse(\n input: unknown,\n options?: ValidationOptions,\n ): ValidationResult<Output> {\n return ValidatorContext.validate(input, this, options)\n }\n\n // @NOTE The built lexicons namespaces will export utility functions that\n // allow accessing the schema's methods without the need to specify \".main.\"\n // as part of the namespace. This way, a utility for a particular record type\n // can be called like \"app.bsky.feed.post.<utility>()\" instead of\n // \"app.bsky.feed.post.main.<utility>()\". Because those utilities could\n // conflict with other schemas (e.g. if there is a lexicon definition at\n // \"#<utility>\"), those exported utilities will be prefixed with \"$\". In order\n // to be able to consistently call the utilities, when using the \"main\" and\n // non \"main\" definitions, we also expose the same methods with a \"$\" prefix.\n // Thanks to this, both of the following call will be possible:\n //\n // - \"app.bsky.feed.post.$parse(...)\" // calls a utility function created by \"lex build\"\n // - \"app.bsky.feed.defs.postView.$parse(...)\" // uses the alias defined below on the schema instance\n\n $assert(input: unknown): asserts input is Output {\n return this.assert(input)\n }\n\n $matches(input: unknown): input is Output {\n return this.matches(input)\n }\n\n $ifMatches<I>(input: I): (I & Output) | undefined {\n return this.ifMatches(input)\n }\n\n $parse(input: unknown, options?: ValidationOptions): Output {\n return this.parse(input, options)\n }\n\n $safeParse(\n input: unknown,\n options?: ValidationOptions,\n ): ValidationResult<Output> {\n return this.safeParse(input, options)\n }\n}\n"]}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { ResultFailure } from '../core.js';
|
|
2
|
-
import { Issue } from './validation-issue.js';
|
|
3
|
-
export declare class ValidationError extends Error {
|
|
4
|
-
name: string;
|
|
5
|
-
readonly issues: Issue[];
|
|
6
|
-
constructor(issues: Issue[], options?: ErrorOptions);
|
|
7
|
-
static fromFailures(failures: ResultFailure<ValidationError>[]): ValidationError;
|
|
8
|
-
}
|
|
9
|
-
//# sourceMappingURL=validation-error.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validation-error.d.ts","sourceRoot":"","sources":["../../src/validation/validation-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAiB,MAAM,YAAY,CAAA;AAEzD,OAAO,EACL,KAAK,EAGN,MAAM,uBAAuB,CAAA;AAE9B,qBAAa,eAAgB,SAAQ,KAAK;IACxC,IAAI,SAAoB;IAExB,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,CAAA;gBAEZ,MAAM,EAAE,KAAK,EAAE,EAAE,OAAO,CAAC,EAAE,YAAY;IAMnD,MAAM,CAAC,YAAY,CACjB,QAAQ,EAAE,aAAa,CAAC,eAAe,CAAC,EAAE,GACzC,eAAe;CAQnB"}
|