@atproto/lex-schema 0.1.4 → 0.1.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 +24 -0
- package/dist/core/$type.d.ts +2 -2
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +1 -1
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/schema.d.ts +3 -2
- 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/standard-schema.d.ts +2 -2
- package/dist/core/standard-schema.d.ts.map +1 -1
- package/dist/core/standard-schema.js.map +1 -1
- package/dist/core/string-format.d.ts +2 -2
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/validation-error.d.ts +1 -1
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +1 -1
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validator.d.ts +1 -1
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +1 -1
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +2 -2
- 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 +1 -1
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +1 -1
- 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.js +1 -1
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.js +1 -1
- 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 +3 -3
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.js +1 -1
- 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 +1 -1
- 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 -1
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.js +1 -1
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.js +1 -1
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +1 -1
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +3 -1
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/lex-map.d.ts +1 -1
- package/dist/schema/lex-map.d.ts.map +1 -1
- package/dist/schema/lex-map.js +1 -1
- package/dist/schema/lex-map.js.map +1 -1
- package/dist/schema/lex-value.d.ts +1 -1
- package/dist/schema/lex-value.d.ts.map +1 -1
- package/dist/schema/lex-value.js +1 -1
- package/dist/schema/lex-value.js.map +1 -1
- package/dist/schema/literal.js +1 -1
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.js +1 -1
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.js +1 -1
- 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 +1 -1
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +2 -1
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +1 -1
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +2 -1
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +2 -1
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +1 -1
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +1 -1
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +3 -2
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +2 -1
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +1 -1
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +1 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +1 -1
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +1 -1
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +2 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +1 -1
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +2 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +2 -2
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +1 -1
- 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 +1 -1
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +2 -2
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.js +1 -1
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +2 -2
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +1 -1
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +3 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +2 -0
- 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 +1 -1
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +2 -2
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +1 -1
- 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 +1 -1
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +1 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +3 -1
- 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 +1 -1
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown.js +1 -1
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +1 -1
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +1 -1
- package/dist/schema/with-default.js.map +1 -1
- package/package.json +6 -10
- package/src/core/$type.test.ts +0 -24
- package/src/core/$type.ts +0 -199
- package/src/core/record-key.ts +0 -85
- package/src/core/result.ts +0 -15
- package/src/core/schema.ts +0 -412
- package/src/core/standard-schema.test.ts +0 -124
- package/src/core/standard-schema.ts +0 -31
- package/src/core/string-format.ts +0 -411
- package/src/core/types.ts +0 -120
- package/src/core/validation-error.ts +0 -134
- package/src/core/validation-issue.ts +0 -340
- package/src/core/validator.ts +0 -636
- package/src/core.ts +0 -9
- package/src/external.ts +0 -3
- package/src/helpers.test.ts +0 -694
- package/src/helpers.ts +0 -222
- package/src/index.ts +0 -3
- package/src/schema/array.test.ts +0 -251
- package/src/schema/array.ts +0 -126
- package/src/schema/blob.test.ts +0 -733
- package/src/schema/blob.ts +0 -150
- package/src/schema/boolean.test.ts +0 -118
- package/src/schema/boolean.ts +0 -46
- package/src/schema/bytes.test.ts +0 -227
- package/src/schema/bytes.ts +0 -81
- package/src/schema/cid.test.ts +0 -125
- package/src/schema/cid.ts +0 -69
- package/src/schema/custom.test.ts +0 -414
- package/src/schema/custom.ts +0 -106
- package/src/schema/dict.test.ts +0 -181
- package/src/schema/dict.ts +0 -122
- package/src/schema/discriminated-union.test.ts +0 -676
- package/src/schema/discriminated-union.ts +0 -196
- package/src/schema/enum.test.ts +0 -398
- package/src/schema/enum.ts +0 -77
- package/src/schema/integer.test.ts +0 -314
- package/src/schema/integer.ts +0 -86
- package/src/schema/intersection.test.ts +0 -33
- package/src/schema/intersection.ts +0 -113
- package/src/schema/lex-map.test.ts +0 -593
- package/src/schema/lex-map.ts +0 -63
- package/src/schema/lex-value.test.ts +0 -81
- package/src/schema/lex-value.ts +0 -86
- package/src/schema/literal.test.ts +0 -533
- package/src/schema/literal.ts +0 -70
- package/src/schema/never.test.ts +0 -175
- package/src/schema/never.ts +0 -56
- package/src/schema/null.test.ts +0 -80
- package/src/schema/null.ts +0 -49
- package/src/schema/nullable.test.ts +0 -470
- package/src/schema/nullable.ts +0 -74
- package/src/schema/object.test.ts +0 -69
- package/src/schema/object.ts +0 -136
- package/src/schema/optional.test.ts +0 -479
- package/src/schema/optional.ts +0 -92
- package/src/schema/params.test.ts +0 -1118
- package/src/schema/params.ts +0 -371
- package/src/schema/payload.test.ts +0 -340
- package/src/schema/payload.ts +0 -204
- package/src/schema/permission-set.test.ts +0 -613
- package/src/schema/permission-set.ts +0 -86
- package/src/schema/permission.test.ts +0 -537
- package/src/schema/permission.ts +0 -63
- package/src/schema/procedure.test.ts +0 -324
- package/src/schema/procedure.ts +0 -98
- package/src/schema/query.test.ts +0 -348
- package/src/schema/query.ts +0 -86
- package/src/schema/record.test.ts +0 -812
- package/src/schema/record.ts +0 -217
- package/src/schema/ref.test.ts +0 -349
- package/src/schema/ref.ts +0 -103
- package/src/schema/refine.test.ts +0 -579
- package/src/schema/refine.ts +0 -153
- package/src/schema/regexp.test.ts +0 -577
- package/src/schema/regexp.ts +0 -82
- package/src/schema/string.test.ts +0 -773
- package/src/schema/string.ts +0 -229
- package/src/schema/subscription.test.ts +0 -499
- package/src/schema/subscription.ts +0 -108
- package/src/schema/token.test.ts +0 -152
- package/src/schema/token.ts +0 -103
- package/src/schema/typed-object.test.ts +0 -745
- package/src/schema/typed-object.ts +0 -181
- package/src/schema/typed-ref.test.ts +0 -796
- package/src/schema/typed-ref.ts +0 -126
- package/src/schema/typed-union.test.ts +0 -355
- package/src/schema/typed-union.ts +0 -130
- package/src/schema/union.test.ts +0 -191
- package/src/schema/union.ts +0 -89
- package/src/schema/unknown.test.ts +0 -313
- package/src/schema/unknown.ts +0 -47
- package/src/schema/with-default.ts +0 -81
- package/src/schema.ts +0 -43
- package/src/util/array-agg.test.ts +0 -42
- package/src/util/array-agg.ts +0 -44
- package/src/util/assertion-util.ts +0 -1
- package/src/util/if-any.ts +0 -3
- package/src/util/lazy-property.ts +0 -14
- package/src/util/memoize.ts +0 -37
- package/tsconfig.build.json +0 -12
- package/tsconfig.json +0 -7
- package/tsconfig.tests.json +0 -8
package/src/schema/refine.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
InferInput,
|
|
3
|
-
IssueCustom,
|
|
4
|
-
ValidationContext,
|
|
5
|
-
ValidationResult,
|
|
6
|
-
Validator,
|
|
7
|
-
} from '../core.js'
|
|
8
|
-
import { CustomAssertionContext } from './custom.js'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Configuration for a refinement check that validates a condition.
|
|
12
|
-
*
|
|
13
|
-
* @template T - The type being validated
|
|
14
|
-
* @property check - Function that returns true if the value passes the check
|
|
15
|
-
* @property message - Error message when the check fails
|
|
16
|
-
* @property path - Optional path to associate with the error
|
|
17
|
-
*/
|
|
18
|
-
export type RefinementCheck<T> = {
|
|
19
|
-
check: (value: T, ctx: CustomAssertionContext) => boolean
|
|
20
|
-
message: string
|
|
21
|
-
path?: PropertyKey | readonly PropertyKey[]
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Configuration for a refinement assertion that narrows the type.
|
|
26
|
-
*
|
|
27
|
-
* @template T - The input type being validated
|
|
28
|
-
* @template Out - The narrowed output type
|
|
29
|
-
* @property check - Type guard function that narrows the type
|
|
30
|
-
* @property message - Error message when the assertion fails
|
|
31
|
-
* @property path - Optional path to associate with the error
|
|
32
|
-
*/
|
|
33
|
-
export type RefinementAssertion<T, Out extends T> = {
|
|
34
|
-
check: (this: null, value: T, ctx: CustomAssertionContext) => value is Out
|
|
35
|
-
message: string
|
|
36
|
-
path?: PropertyKey | readonly PropertyKey[]
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Infers the input type from a refinement configuration.
|
|
41
|
-
*
|
|
42
|
-
* @template R - The refinement type
|
|
43
|
-
*/
|
|
44
|
-
export type InferRefinement<R> =
|
|
45
|
-
R extends RefinementCheck<infer T>
|
|
46
|
-
? T
|
|
47
|
-
: R extends RefinementAssertion<infer T, any>
|
|
48
|
-
? T
|
|
49
|
-
: never
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Union type of refinement check or assertion.
|
|
53
|
-
*
|
|
54
|
-
* @template T - The input type being validated
|
|
55
|
-
* @template Out - The output type (same as T for checks, narrowed for assertions)
|
|
56
|
-
*/
|
|
57
|
-
export type Refinement<T = any, Out extends T = T> =
|
|
58
|
-
| RefinementCheck<T>
|
|
59
|
-
| RefinementAssertion<T, Out>
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Creates a refined schema by adding additional validation constraints.
|
|
63
|
-
*
|
|
64
|
-
* Wraps an existing schema with an additional check function. The base schema
|
|
65
|
-
* is validated first, then the refinement check is applied to the result.
|
|
66
|
-
*
|
|
67
|
-
* @param schema - The base schema to refine
|
|
68
|
-
* @param refinement - The refinement check or assertion to apply
|
|
69
|
-
* @returns A new schema that includes the refinement
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```ts
|
|
73
|
-
* // Simple check refinement
|
|
74
|
-
* const positiveInt = l.refine(l.integer(), {
|
|
75
|
-
* check: (value) => value > 0,
|
|
76
|
-
* message: 'Value must be positive',
|
|
77
|
-
* })
|
|
78
|
-
*
|
|
79
|
-
* positiveInt.parse(5) // 5
|
|
80
|
-
* positiveInt.parse(-1) // throws
|
|
81
|
-
*
|
|
82
|
-
* // Type-narrowing assertion
|
|
83
|
-
* const nonEmptyString = l.refine(l.string(), {
|
|
84
|
-
* check: (value): value is string & { length: number } => value.length > 0,
|
|
85
|
-
* message: 'String must not be empty',
|
|
86
|
-
* })
|
|
87
|
-
*
|
|
88
|
-
* // With custom path for nested errors
|
|
89
|
-
* const validDateRange = l.refine(
|
|
90
|
-
* l.object({ start: l.string(), end: l.string() }),
|
|
91
|
-
* {
|
|
92
|
-
* check: (v) => new Date(v.start) < new Date(v.end),
|
|
93
|
-
* message: 'Start date must be before end date',
|
|
94
|
-
* path: ['end'],
|
|
95
|
-
* }
|
|
96
|
-
* )
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
export function refine<
|
|
100
|
-
const TValidator extends Validator,
|
|
101
|
-
TInput extends InferInput<TValidator>,
|
|
102
|
-
>(
|
|
103
|
-
schema: TValidator,
|
|
104
|
-
refinement: RefinementAssertion<InferInput<TValidator>, TInput>,
|
|
105
|
-
): TValidator & Validator<TInput>
|
|
106
|
-
export function refine<const TValidator extends Validator>(
|
|
107
|
-
schema: TValidator,
|
|
108
|
-
refinement: RefinementCheck<InferInput<TValidator>>,
|
|
109
|
-
): TValidator
|
|
110
|
-
export function refine<
|
|
111
|
-
TRefinement extends Refinement,
|
|
112
|
-
const TValidator extends Validator<InferRefinement<TRefinement>>,
|
|
113
|
-
>(schema: TValidator, refinement: TRefinement): TValidator
|
|
114
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
115
|
-
export function refine<const TValidator extends Validator>(
|
|
116
|
-
schema: TValidator,
|
|
117
|
-
refinement: Refinement<unknown>,
|
|
118
|
-
): TValidator {
|
|
119
|
-
// This is basically the same as monkey patching the "validateInContext"
|
|
120
|
-
// method to the schema, but done in a way that does not mutate the original
|
|
121
|
-
// schema. This is safe to do because Validators don't update their internal
|
|
122
|
-
// state over their lifetime.
|
|
123
|
-
return Object.create(schema, {
|
|
124
|
-
validateInContext: {
|
|
125
|
-
// We do not use an arrow function to avoid creating a closure
|
|
126
|
-
value: validateInContextUnbound.bind({ schema, refinement }),
|
|
127
|
-
enumerable: false,
|
|
128
|
-
writable: false,
|
|
129
|
-
configurable: true,
|
|
130
|
-
},
|
|
131
|
-
})
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
135
|
-
function validateInContextUnbound<S extends Validator>(
|
|
136
|
-
this: {
|
|
137
|
-
schema: S
|
|
138
|
-
refinement: Refinement<InferInput<S>>
|
|
139
|
-
},
|
|
140
|
-
input: unknown,
|
|
141
|
-
ctx: ValidationContext,
|
|
142
|
-
): ValidationResult<InferInput<S>> {
|
|
143
|
-
const result = ctx.validate(input, this.schema)
|
|
144
|
-
if (!result.success) return result
|
|
145
|
-
|
|
146
|
-
const checkResult = this.refinement.check.call(null, result.value, ctx)
|
|
147
|
-
if (!checkResult) {
|
|
148
|
-
const path = ctx.concatPath(this.refinement.path)
|
|
149
|
-
return ctx.issue(new IssueCustom(path, input, this.refinement.message))
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return result
|
|
153
|
-
}
|