-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Arbitrary.ts
580 lines (551 loc) · 17.9 KB
/
Arbitrary.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/**
* @since 0.67.0
*/
import * as Arr from "effect/Array"
import * as Option from "effect/Option"
import * as Predicate from "effect/Predicate"
import * as AST from "./AST.js"
import * as FastCheck from "./FastCheck.js"
import * as errors_ from "./internal/errors.js"
import * as filters_ from "./internal/filters.js"
import * as util_ from "./internal/util.js"
import type * as Schema from "./Schema.js"
/**
* @category model
* @since 0.67.0
*/
export interface LazyArbitrary<A> {
(fc: typeof FastCheck): FastCheck.Arbitrary<A>
}
/**
* @category hooks
* @since 0.67.0
*/
export const ArbitraryHookId: unique symbol = Symbol.for("@effect/schema/ArbitraryHookId")
/**
* @category hooks
* @since 0.67.0
*/
export type ArbitraryHookId = typeof ArbitraryHookId
/**
* @category hooks
* @since 0.72.3
*/
export interface GenerationContext {
readonly depthIdentifier?: string
readonly maxDepth: number
}
/**
* @category hooks
* @since 0.72.3
*/
export type ArbitraryAnnotation<A> = (
...args: [...ReadonlyArray<LazyArbitrary<any>>, GenerationContext]
) => LazyArbitrary<A>
/**
* @category annotations
* @since 0.67.0
*/
export const arbitrary =
<A>(annotation: ArbitraryAnnotation<A>) => <I, R>(self: Schema.Schema<A, I, R>): Schema.Schema<A, I, R> =>
self.annotations({ [ArbitraryHookId]: annotation })
/**
* Returns a LazyArbitrary for the `A` type of the provided schema.
*
* @category arbitrary
* @since 0.67.0
*/
export const makeLazy = <A, I, R>(schema: Schema.Schema<A, I, R>): LazyArbitrary<A> =>
go(schema.ast, { maxDepth: 2 }, [])
/**
* Returns a fast-check Arbitrary for the `A` type of the provided schema.
*
* @category arbitrary
* @since 0.67.0
*/
export const make = <A, I, R>(schema: Schema.Schema<A, I, R>): FastCheck.Arbitrary<A> => makeLazy(schema)(FastCheck)
const getHook = AST.getAnnotation<ArbitraryAnnotation<any>>(ArbitraryHookId)
const getRefinementFromArbitrary = (
ast: AST.Refinement,
ctx: Context,
path: ReadonlyArray<PropertyKey>
) => {
const constraints = combineConstraints(ctx.constraints, getConstraints(ast))
return go(ast.from, constraints ? { ...ctx, constraints } : ctx, path)
}
const getSuspendedContext = (
ctx: Context,
ast: AST.Suspend
): Context => {
if (ctx.depthIdentifier !== undefined) {
return ctx
}
const depthIdentifier = AST.getIdentifierAnnotation(ast).pipe(
Option.orElse(() => AST.getIdentifierAnnotation(ast.f())),
Option.getOrElse(() => "SuspendDefaultDepthIdentifier")
)
return { ...ctx, depthIdentifier }
}
const getSuspendedArray = (
fc: typeof FastCheck,
depthIdentifier: string,
maxDepth: number,
item: FastCheck.Arbitrary<any>,
constraints?: FastCheck.ArrayConstraints
) => {
let minLength = 1
let maxLength = 2
if (constraints && constraints.minLength !== undefined && constraints.minLength > minLength) {
minLength = constraints.minLength
if (minLength > maxLength) {
maxLength = minLength
}
}
return fc.oneof(
{ maxDepth, depthIdentifier },
fc.constant([]),
fc.array(item, { minLength, maxLength })
)
}
interface Context extends GenerationContext {
readonly constraints?: Constraints
}
const go = (
ast: AST.AST,
ctx: Context,
path: ReadonlyArray<PropertyKey>
): LazyArbitrary<any> => {
const hook = getHook(ast)
if (Option.isSome(hook)) {
switch (ast._tag) {
case "Declaration":
return hook.value(...ast.typeParameters.map((p) => go(p, ctx, path)), ctx)
case "Refinement":
return hook.value(getRefinementFromArbitrary(ast, ctx, path), ctx)
default:
return hook.value(ctx)
}
}
switch (ast._tag) {
case "Declaration": {
throw new Error(errors_.getArbitraryMissingAnnotationErrorMessage(path, ast))
}
case "Literal":
return (fc) => fc.constant(ast.literal)
case "UniqueSymbol":
return (fc) => fc.constant(ast.symbol)
case "UndefinedKeyword":
return (fc) => fc.constant(undefined)
case "NeverKeyword":
return () => {
throw new Error(errors_.getArbitraryUnsupportedErrorMessage(path, ast))
}
case "UnknownKeyword":
case "AnyKeyword":
case "VoidKeyword":
return (fc) => fc.anything()
case "StringKeyword":
return (fc) => {
if (ctx.constraints) {
switch (ctx.constraints._tag) {
case "StringConstraints":
return fc.string(ctx.constraints.constraints)
}
}
return fc.string()
}
case "NumberKeyword":
return (fc) => {
if (ctx.constraints) {
switch (ctx.constraints._tag) {
case "NumberConstraints":
return fc.float(ctx.constraints.constraints)
case "IntegerConstraints":
return fc.integer(ctx.constraints.constraints)
}
}
return fc.float()
}
case "BooleanKeyword":
return (fc) => fc.boolean()
case "BigIntKeyword":
return (fc) => {
if (ctx.constraints) {
switch (ctx.constraints._tag) {
case "BigIntConstraints":
return fc.bigInt(ctx.constraints.constraints)
}
}
return fc.bigInt()
}
case "SymbolKeyword":
return (fc) => fc.string().map((s) => Symbol.for(s))
case "ObjectKeyword":
return (fc) => fc.oneof(fc.object(), fc.array(fc.anything()))
case "TemplateLiteral": {
return (fc) => {
const string = fc.string({ maxLength: 5 })
const number = fc.float({ noDefaultInfinity: true }).filter((n) => !Number.isNaN(n))
const components: Array<FastCheck.Arbitrary<string | number>> = [fc.constant(ast.head)]
for (const span of ast.spans) {
if (AST.isStringKeyword(span.type)) {
components.push(string)
} else {
components.push(number)
}
components.push(fc.constant(span.literal))
}
return fc.tuple(...components).map((spans) => spans.join(""))
}
}
case "TupleType": {
const elements: Array<LazyArbitrary<any>> = []
let hasOptionals = false
let i = 0
for (const element of ast.elements) {
elements.push(go(element.type, ctx, path.concat(i++)))
if (element.isOptional) {
hasOptionals = true
}
}
const rest = ast.rest.map((annotatedAST) => go(annotatedAST.type, ctx, path))
return (fc) => {
// ---------------------------------------------
// handle elements
// ---------------------------------------------
let output = fc.tuple(...elements.map((arb) => arb(fc)))
if (hasOptionals) {
const indexes = fc.tuple(
...ast.elements.map((element) => element.isOptional ? fc.boolean() : fc.constant(true))
)
output = output.chain((tuple) =>
indexes.map((booleans) => {
for (const [i, b] of booleans.reverse().entries()) {
if (!b) {
tuple.splice(booleans.length - i, 1)
}
}
return tuple
})
)
}
// ---------------------------------------------
// handle rest element
// ---------------------------------------------
if (Arr.isNonEmptyReadonlyArray(rest)) {
const [head, ...tail] = rest
const item = head(fc)
const constraints: FastCheck.ArrayConstraints | undefined =
ctx.constraints && ctx.constraints._tag === "ArrayConstraints"
? ctx.constraints.constraints
: undefined
output = output.chain((as) => {
return (ctx.depthIdentifier !== undefined
? getSuspendedArray(fc, ctx.depthIdentifier, ctx.maxDepth, item, constraints)
: fc.array(item, constraints)).map((rest) => [...as, ...rest])
})
// ---------------------------------------------
// handle post rest elements
// ---------------------------------------------
for (let j = 0; j < tail.length; j++) {
output = output.chain((as) => tail[j](fc).map((a) => [...as, a]))
}
}
return output
}
}
case "TypeLiteral": {
const propertySignaturesTypes = ast.propertySignatures.map((ps) => go(ps.type, ctx, path.concat(ps.name)))
const indexSignatures = ast.indexSignatures.map((is) =>
[go(is.parameter, ctx, path), go(is.type, ctx, path)] as const
)
return (fc) => {
const arbs: any = {}
const requiredKeys: Array<PropertyKey> = []
// ---------------------------------------------
// handle property signatures
// ---------------------------------------------
for (let i = 0; i < propertySignaturesTypes.length; i++) {
const ps = ast.propertySignatures[i]
const name = ps.name
if (!ps.isOptional) {
requiredKeys.push(name)
}
arbs[name] = propertySignaturesTypes[i](fc)
}
let output = fc.record<any, any>(arbs, { requiredKeys })
// ---------------------------------------------
// handle index signatures
// ---------------------------------------------
for (let i = 0; i < indexSignatures.length; i++) {
const key = indexSignatures[i][0](fc)
const value = indexSignatures[i][1](fc)
output = output.chain((o) => {
const item = fc.tuple(key, value)
const arr = ctx.depthIdentifier !== undefined ?
getSuspendedArray(fc, ctx.depthIdentifier, ctx.maxDepth, item) :
fc.array(item)
return arr.map((tuples) => ({ ...Object.fromEntries(tuples), ...o }))
})
}
return output
}
}
case "Union": {
const types = ast.types.map((member) => go(member, ctx, path))
return (fc) => fc.oneof(...types.map((arb) => arb(fc)))
}
case "Enums": {
if (ast.enums.length === 0) {
throw new Error(errors_.getArbitraryEmptyEnumErrorMessage(path))
}
return (fc) => fc.oneof(...ast.enums.map(([_, value]) => fc.constant(value)))
}
case "Refinement": {
const from = getRefinementFromArbitrary(ast, ctx, path)
return (fc) => from(fc).filter((a) => Option.isNone(ast.filter(a, AST.defaultParseOption, ast)))
}
case "Suspend": {
const get = util_.memoizeThunk(() => {
return go(ast.f(), getSuspendedContext(ctx, ast), path)
})
return (fc) => fc.constant(null).chain(() => get()(fc))
}
case "Transformation":
return go(ast.to, ctx, path)
}
}
/** @internal */
export class NumberConstraints {
readonly _tag = "NumberConstraints"
readonly constraints: FastCheck.FloatConstraints
constructor(options: {
readonly min?: number | undefined
readonly max?: number | undefined
readonly noNaN?: boolean | undefined
readonly noDefaultInfinity?: boolean | undefined
}) {
this.constraints = {}
if (Predicate.isNumber(options.min)) {
this.constraints.min = Math.fround(options.min)
}
if (Predicate.isNumber(options.max)) {
this.constraints.max = Math.fround(options.max)
}
if (Predicate.isBoolean(options.noNaN)) {
this.constraints.noNaN = options.noNaN
}
if (Predicate.isBoolean(options.noDefaultInfinity)) {
this.constraints.noDefaultInfinity = options.noDefaultInfinity
}
}
}
/** @internal */
export class StringConstraints {
readonly _tag = "StringConstraints"
readonly constraints: FastCheck.StringSharedConstraints
constructor(options: {
readonly minLength?: number | undefined
readonly maxLength?: number | undefined
}) {
this.constraints = {}
if (Predicate.isNumber(options.minLength)) {
this.constraints.minLength = options.minLength
}
if (Predicate.isNumber(options.maxLength)) {
this.constraints.maxLength = options.maxLength
}
}
}
/** @internal */
export class IntegerConstraints {
readonly _tag = "IntegerConstraints"
readonly constraints: FastCheck.IntegerConstraints
constructor(options: {
readonly min?: number | undefined
readonly max?: number | undefined
}) {
this.constraints = {}
if (Predicate.isNumber(options.min)) {
this.constraints.min = options.min
}
if (Predicate.isNumber(options.max)) {
this.constraints.max = options.max
}
}
}
/** @internal */
export class ArrayConstraints {
readonly _tag = "ArrayConstraints"
readonly constraints: FastCheck.ArrayConstraints
constructor(options: {
readonly minLength?: number | undefined
readonly maxLength?: number | undefined
}) {
this.constraints = {}
if (Predicate.isNumber(options.minLength)) {
this.constraints.minLength = options.minLength
}
if (Predicate.isNumber(options.maxLength)) {
this.constraints.maxLength = options.maxLength
}
}
}
/** @internal */
export class BigIntConstraints {
readonly _tag = "BigIntConstraints"
readonly constraints: FastCheck.BigIntConstraints
constructor(options: {
readonly min?: bigint | undefined
readonly max?: bigint | undefined
}) {
this.constraints = {}
if (Predicate.isBigInt(options.min)) {
this.constraints.min = options.min
}
if (Predicate.isBigInt(options.max)) {
this.constraints.max = options.max
}
}
}
/** @internal */
export type Constraints =
| NumberConstraints
| StringConstraints
| IntegerConstraints
| ArrayConstraints
| BigIntConstraints
/** @internal */
export const getConstraints = (ast: AST.Refinement): Constraints | undefined => {
const TypeAnnotationId = ast.annotations[AST.TypeAnnotationId]
const jsonSchema: any = ast.annotations[AST.JSONSchemaAnnotationId]
switch (TypeAnnotationId) {
// int
case filters_.IntTypeId:
return new IntegerConstraints({})
// number
case filters_.GreaterThanTypeId:
case filters_.GreaterThanOrEqualToTypeId:
case filters_.LessThanTypeId:
case filters_.LessThanOrEqualToTypeId:
case filters_.BetweenTypeId:
return new NumberConstraints({
min: jsonSchema.exclusiveMinimum ?? jsonSchema.minimum,
max: jsonSchema.exclusiveMaximum ?? jsonSchema.maximum
})
// bigint
case filters_.GreaterThanBigintTypeId:
case filters_.GreaterThanOrEqualToBigIntTypeId:
case filters_.LessThanBigIntTypeId:
case filters_.LessThanOrEqualToBigIntTypeId:
case filters_.BetweenBigintTypeId: {
const constraints: any = ast.annotations[TypeAnnotationId]
return new BigIntConstraints(constraints)
}
// string
case filters_.MinLengthTypeId:
case filters_.MaxLengthTypeId:
case filters_.LengthTypeId:
return new StringConstraints(jsonSchema)
// array
case filters_.MinItemsTypeId:
case filters_.MaxItemsTypeId:
case filters_.ItemsCountTypeId:
return new ArrayConstraints({
minLength: jsonSchema.minItems,
maxLength: jsonSchema.maxItems
})
}
}
/** @internal */
export const combineConstraints = (
c1: Constraints | undefined,
c2: Constraints | undefined
): Constraints | undefined => {
if (c1 === undefined) {
return c2
}
if (c2 === undefined) {
return c1
}
switch (c1._tag) {
case "ArrayConstraints": {
switch (c2._tag) {
case "ArrayConstraints":
return new ArrayConstraints({
minLength: getMax(c1.constraints.minLength, c2.constraints.minLength),
maxLength: getMin(c1.constraints.maxLength, c2.constraints.maxLength)
})
}
break
}
case "NumberConstraints": {
switch (c2._tag) {
case "NumberConstraints":
return new NumberConstraints({
min: getMax(c1.constraints.min, c2.constraints.min),
max: getMin(c1.constraints.max, c2.constraints.max),
noNaN: getOr(c1.constraints.noNaN, c2.constraints.noNaN),
noDefaultInfinity: getOr(c1.constraints.noDefaultInfinity, c2.constraints.noDefaultInfinity)
})
case "IntegerConstraints":
return new IntegerConstraints({
min: getMax(c1.constraints.min, c2.constraints.min),
max: getMin(c1.constraints.max, c2.constraints.max)
})
}
break
}
case "BigIntConstraints": {
switch (c2._tag) {
case "BigIntConstraints":
return new BigIntConstraints({
min: getMax(c1.constraints.min, c2.constraints.min),
max: getMin(c1.constraints.max, c2.constraints.max)
})
}
break
}
case "StringConstraints": {
switch (c2._tag) {
case "StringConstraints":
return new StringConstraints({
minLength: getMax(c1.constraints.minLength, c2.constraints.minLength),
maxLength: getMin(c1.constraints.maxLength, c2.constraints.maxLength)
})
}
break
}
case "IntegerConstraints": {
switch (c2._tag) {
case "NumberConstraints":
case "IntegerConstraints": {
return new IntegerConstraints({
min: getMax(c1.constraints.min, c2.constraints.min),
max: getMin(c1.constraints.max, c2.constraints.max)
})
}
}
break
}
}
}
const getOr = (a: boolean | undefined, b: boolean | undefined): boolean | undefined => {
return a === undefined ? b : b === undefined ? a : a || b
}
function getMax(n1: bigint | undefined, n2: bigint | undefined): bigint | undefined
function getMax(n1: number | undefined, n2: number | undefined): number | undefined
function getMax(
n1: bigint | number | undefined,
n2: bigint | number | undefined
): bigint | number | undefined {
return n1 === undefined ? n2 : n2 === undefined ? n1 : n1 <= n2 ? n2 : n1
}
function getMin(n1: bigint | undefined, n2: bigint | undefined): bigint | undefined
function getMin(n1: number | undefined, n2: number | undefined): number | undefined
function getMin(
n1: bigint | number | undefined,
n2: bigint | number | undefined
): bigint | number | undefined {
return n1 === undefined ? n2 : n2 === undefined ? n1 : n1 <= n2 ? n1 : n2
}