@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
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
import { ifCid, isLegacyBlobRef, isPlainObject } from '@atproto/lex-data'
|
|
2
|
-
|
|
3
|
-
const STRING_PREVIEW_MAX_LENGTH = 256
|
|
4
|
-
const STRING_PREVIEW_TRUNCATED_SUFFIX = '…'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Abstract base class for all validation issues.
|
|
8
|
-
*
|
|
9
|
-
* An issue represents a single validation failure, containing:
|
|
10
|
-
* - A code identifying the type of issue
|
|
11
|
-
* - The path to the invalid value in the data structure
|
|
12
|
-
* - The actual input value that failed validation
|
|
13
|
-
*
|
|
14
|
-
* Subclasses add specific properties relevant to each issue type and implement
|
|
15
|
-
* the {@link message} property for human-readable error messages (that don't
|
|
16
|
-
* contain the error path)
|
|
17
|
-
*/
|
|
18
|
-
export abstract class Issue {
|
|
19
|
-
abstract readonly message: string
|
|
20
|
-
|
|
21
|
-
constructor(
|
|
22
|
-
readonly code: string,
|
|
23
|
-
readonly path: readonly PropertyKey[],
|
|
24
|
-
readonly input: unknown,
|
|
25
|
-
) {}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Returns a human-readable description of the validation issue.
|
|
29
|
-
*/
|
|
30
|
-
toString() {
|
|
31
|
-
return `${this.message}${stringifyPath(this.path)}`
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Converts the issue to a JSON-serializable object.
|
|
36
|
-
*
|
|
37
|
-
* @returns An object containing the issue code, path, and message
|
|
38
|
-
*/
|
|
39
|
-
toJSON() {
|
|
40
|
-
return {
|
|
41
|
-
code: this.code,
|
|
42
|
-
path: this.path,
|
|
43
|
-
message: this.message,
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* A custom validation issue with a user-defined message.
|
|
50
|
-
*
|
|
51
|
-
* Use this for validation rules that don't fit into the standard issue categories.
|
|
52
|
-
*/
|
|
53
|
-
export class IssueCustom extends Issue {
|
|
54
|
-
constructor(
|
|
55
|
-
readonly path: readonly PropertyKey[],
|
|
56
|
-
readonly input: unknown,
|
|
57
|
-
readonly message: string,
|
|
58
|
-
) {
|
|
59
|
-
super('custom', path, input)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Issue for string values that don't match an expected format.
|
|
65
|
-
*
|
|
66
|
-
* Used for AT Protocol specific formats like DID, handle, NSID, AT-URI, etc.
|
|
67
|
-
*/
|
|
68
|
-
export class IssueInvalidFormat extends Issue {
|
|
69
|
-
constructor(
|
|
70
|
-
path: readonly PropertyKey[],
|
|
71
|
-
input: unknown,
|
|
72
|
-
readonly format: string,
|
|
73
|
-
readonly detail?: string,
|
|
74
|
-
) {
|
|
75
|
-
super('invalid_format', path, input)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
override get message(): string {
|
|
79
|
-
return `Invalid ${this.formatDescription}${this.detail ? ` (${this.detail}, ` : ' ('}got ${stringifyValue(this.input)})`
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** Returns a human-readable description of the expected format. */
|
|
83
|
-
get formatDescription(): string {
|
|
84
|
-
switch (this.format) {
|
|
85
|
-
case 'at-identifier':
|
|
86
|
-
return `AT identifier`
|
|
87
|
-
case 'did':
|
|
88
|
-
return `DID`
|
|
89
|
-
case 'nsid':
|
|
90
|
-
return `NSID`
|
|
91
|
-
case 'cid':
|
|
92
|
-
return `CID string`
|
|
93
|
-
case 'tid':
|
|
94
|
-
return `TID string`
|
|
95
|
-
case 'record-key':
|
|
96
|
-
return `record key`
|
|
97
|
-
default:
|
|
98
|
-
return this.format
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
toJSON() {
|
|
103
|
-
return {
|
|
104
|
-
...super.toJSON(),
|
|
105
|
-
format: this.format,
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Issue for values that have an unexpected type.
|
|
112
|
-
*
|
|
113
|
-
* This is one of the most common validation issues, occurring when the
|
|
114
|
-
* runtime type of a value doesn't match the expected schema type.
|
|
115
|
-
*/
|
|
116
|
-
export class IssueInvalidType extends Issue {
|
|
117
|
-
constructor(
|
|
118
|
-
path: readonly PropertyKey[],
|
|
119
|
-
input: unknown,
|
|
120
|
-
readonly expected: readonly string[],
|
|
121
|
-
) {
|
|
122
|
-
super('invalid_type', path, input)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
override get message(): string {
|
|
126
|
-
return `Expected ${oneOf(this.expected.map(stringifyExpectedType))} value type (got ${stringifyValue(this.input)})`
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
toJSON() {
|
|
130
|
-
return {
|
|
131
|
-
...super.toJSON(),
|
|
132
|
-
expected: this.expected,
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Issue for values that don't match any of the expected literal values.
|
|
139
|
-
*
|
|
140
|
-
* Used when a value must be one of a specific set of allowed values
|
|
141
|
-
* (e.g., enum-like constraints).
|
|
142
|
-
*/
|
|
143
|
-
export class IssueInvalidValue extends Issue {
|
|
144
|
-
constructor(
|
|
145
|
-
path: readonly PropertyKey[],
|
|
146
|
-
input: unknown,
|
|
147
|
-
readonly values: readonly unknown[],
|
|
148
|
-
) {
|
|
149
|
-
super('invalid_value', path, input)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
override get message(): string {
|
|
153
|
-
return `Expected ${oneOf(this.values.map(stringifyValue))} (got ${stringifyValue(this.input)})`
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
toJSON() {
|
|
157
|
-
return {
|
|
158
|
-
...super.toJSON(),
|
|
159
|
-
values: this.values,
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Issue for missing required object properties.
|
|
166
|
-
*/
|
|
167
|
-
export class IssueRequiredKey extends Issue {
|
|
168
|
-
constructor(
|
|
169
|
-
path: readonly PropertyKey[],
|
|
170
|
-
input: unknown,
|
|
171
|
-
readonly key: PropertyKey,
|
|
172
|
-
) {
|
|
173
|
-
super('required_key', path, input)
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
override get message(): string {
|
|
177
|
-
return `Missing required key "${String(this.key)}"`
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
toJSON() {
|
|
181
|
-
return {
|
|
182
|
-
...super.toJSON(),
|
|
183
|
-
key: this.key,
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* The type of measurement for size constraint issues.
|
|
190
|
-
*
|
|
191
|
-
* - `'array'` - Array length
|
|
192
|
-
* - `'string'` - String length in characters
|
|
193
|
-
* - `'integer'` - Numeric value
|
|
194
|
-
* - `'grapheme'` - String length in grapheme clusters
|
|
195
|
-
* - `'bytes'` - Byte length
|
|
196
|
-
* - `'blob'` - Blob size
|
|
197
|
-
*/
|
|
198
|
-
export type MeasurableType =
|
|
199
|
-
| 'array'
|
|
200
|
-
| 'string'
|
|
201
|
-
| 'integer'
|
|
202
|
-
| 'grapheme'
|
|
203
|
-
| 'bytes'
|
|
204
|
-
| 'blob'
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Issue for values that exceed a maximum constraint.
|
|
208
|
-
*/
|
|
209
|
-
export class IssueTooBig extends Issue {
|
|
210
|
-
constructor(
|
|
211
|
-
path: readonly PropertyKey[],
|
|
212
|
-
input: unknown,
|
|
213
|
-
readonly maximum: number,
|
|
214
|
-
readonly type: MeasurableType,
|
|
215
|
-
readonly actual: number,
|
|
216
|
-
) {
|
|
217
|
-
super('too_big', path, input)
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
override get message(): string {
|
|
221
|
-
return `${this.type} too big (maximum ${this.maximum}, got ${this.actual})`
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
toJSON() {
|
|
225
|
-
return {
|
|
226
|
-
...super.toJSON(),
|
|
227
|
-
type: this.type,
|
|
228
|
-
maximum: this.maximum,
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Issue for values that are below a minimum constraint.
|
|
235
|
-
*/
|
|
236
|
-
export class IssueTooSmall extends Issue {
|
|
237
|
-
constructor(
|
|
238
|
-
path: readonly PropertyKey[],
|
|
239
|
-
input: unknown,
|
|
240
|
-
readonly minimum: number,
|
|
241
|
-
readonly type: MeasurableType,
|
|
242
|
-
readonly actual: number,
|
|
243
|
-
) {
|
|
244
|
-
super('too_small', path, input)
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
override get message(): string {
|
|
248
|
-
return `${this.type} too small (minimum ${this.minimum}, got ${this.actual})`
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
toJSON() {
|
|
252
|
-
return {
|
|
253
|
-
...super.toJSON(),
|
|
254
|
-
type: this.type,
|
|
255
|
-
minimum: this.minimum,
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// -----------------------------------------------------------------------------
|
|
261
|
-
// Helper functions for formatting error messages
|
|
262
|
-
// -----------------------------------------------------------------------------
|
|
263
|
-
|
|
264
|
-
function stringifyExpectedType(expected: string): string {
|
|
265
|
-
if (expected === '$typed') {
|
|
266
|
-
return 'an object which includes the "$type" property'
|
|
267
|
-
}
|
|
268
|
-
return expected
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
function stringifyPath(path: readonly PropertyKey[]) {
|
|
272
|
-
return ` at ${buildJsonPath(path)}`
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
function buildJsonPath(path: readonly PropertyKey[]): string {
|
|
276
|
-
return `$${path.map(toJsonPathSegment).join('')}`
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
function toJsonPathSegment(segment: PropertyKey): string {
|
|
280
|
-
if (typeof segment === 'number' || typeof segment === 'symbol') {
|
|
281
|
-
return `[${String(segment)}]`
|
|
282
|
-
} else if (/^[a-zA-Z_$][a-zA-Z0-9_]*$/.test(segment)) {
|
|
283
|
-
return `.${segment}`
|
|
284
|
-
} else {
|
|
285
|
-
return `[${JSON.stringify(segment)}]`
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
function oneOf(arr: readonly string[]): string {
|
|
290
|
-
if (arr.length === 0) return ''
|
|
291
|
-
if (arr.length === 1) return arr[0]
|
|
292
|
-
return `one of ${arr.slice(0, -1).join(', ')} or ${arr.at(-1)}`
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function stringifyValue(value: unknown): string {
|
|
296
|
-
switch (typeof value) {
|
|
297
|
-
case 'bigint':
|
|
298
|
-
return `${value}n`
|
|
299
|
-
case 'number':
|
|
300
|
-
case 'boolean':
|
|
301
|
-
return String(value)
|
|
302
|
-
case 'string':
|
|
303
|
-
return JSON.stringify(
|
|
304
|
-
value.length < STRING_PREVIEW_MAX_LENGTH
|
|
305
|
-
? value
|
|
306
|
-
: `${value.slice(0, STRING_PREVIEW_MAX_LENGTH - STRING_PREVIEW_TRUNCATED_SUFFIX.length)}${STRING_PREVIEW_TRUNCATED_SUFFIX}`,
|
|
307
|
-
)
|
|
308
|
-
case 'object':
|
|
309
|
-
if (value === null) return 'null'
|
|
310
|
-
if (Array.isArray(value)) {
|
|
311
|
-
return `[${stringifyArray(value, stringifyValue)}]`
|
|
312
|
-
}
|
|
313
|
-
if (isPlainObject(value)) {
|
|
314
|
-
return `{${stringifyArray(Object.entries(value), stringifyObjectEntry)}}`
|
|
315
|
-
}
|
|
316
|
-
if (ifCid(value)) return 'cid'
|
|
317
|
-
if (isLegacyBlobRef(value)) return 'legacy-blob'
|
|
318
|
-
if (value instanceof Date) return 'date'
|
|
319
|
-
if (value instanceof RegExp) return 'regexp'
|
|
320
|
-
if (value instanceof Map) return 'map'
|
|
321
|
-
if (value instanceof Set) return 'set'
|
|
322
|
-
return 'object'
|
|
323
|
-
default:
|
|
324
|
-
return typeof value
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
329
|
-
function stringifyObjectEntry([key, _value]: [PropertyKey, unknown]): string {
|
|
330
|
-
return `${JSON.stringify(key)}: ...`
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/*@__NO_SIDE_EFFECTS__*/
|
|
334
|
-
function stringifyArray<T>(
|
|
335
|
-
arr: readonly T[],
|
|
336
|
-
fn: (item: T) => string,
|
|
337
|
-
n = 2,
|
|
338
|
-
): string {
|
|
339
|
-
return arr.slice(0, n).map(fn).join(', ') + (arr.length > n ? ', ...' : '')
|
|
340
|
-
}
|