-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindings.ts
677 lines (619 loc) · 20.4 KB
/
bindings.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
import { type Constructor, format } from "./deps.ts";
import { type Expectation, type Validator } from "./types.ts";
import { count as getCount } from "./iter_utils.ts";
import {
createInst,
Expectable,
print,
shouldBe,
shouldBeBut,
} from "./utils.ts";
import { EnumValidator } from "./validators/enum.ts";
import { RangeValidator } from "./validators/range.ts";
import { FixedArrayValidator } from "./validators/array/fixed_array.ts";
import { PropertiesValidator } from "./validators/object/properties.ts";
import { OptionalValidator } from "./validators/object/optional.ts";
import { PropertyValueValidator } from "./validators/object/property_value.ts";
import { PropertyKeyValidator } from "./validators/object/property_key.ts";
import { NullishValidator } from "./validators/nullish.ts";
import { FloatValidator } from "./validators/number/float.ts";
import { IntegerValidator } from "./validators/number/integer.ts";
import { PositiveNumberValidator } from "./validators/numeric/positive_number.ts";
import { NegativeNumberValidator } from "./validators/numeric/negative_number.ts";
import { NonNegativeNumberValidator } from "./validators/numeric/non_negative_number.ts";
import { NonPositiveNumberValidator } from "./validators/numeric/non_positive_number.ts";
import { PatternValidator } from "./validators/string/pattern.ts";
import { CountValidator } from "./validators/iterable/count.ts";
import { EmptyValidator } from "./validators/iterable/empty.ts";
import { ItemValidator } from "./validators/iterable/item.ts";
import { UniqueValidator } from "./validators/iterable/unique.ts";
import { MaxCountValidator } from "./validators/iterable/max_count.ts";
import { MinCountValidator } from "./validators/iterable/min_count.ts";
import { NonEmptyValidator } from "./validators/iterable/non_empty.ts";
import { SingleValidator } from "./validators/iterable/single.ts";
import { EqualityValidator } from "./validators/operators/equality.ts";
import { LessThanValidator } from "./validators/operators/less_than.ts";
import { LessThanOrEqualValidator } from "./validators/operators/less_than_or_equal.ts";
import { GreaterThanValidator } from "./validators/operators/greater_than.ts";
import { GreaterThanOrEqualValidator } from "./validators/operators/greater_than_or_equal.ts";
import { InequalityValidator } from "./validators/operators/inequality.ts";
import { InstanceValidator } from "./validators/operators/instanceof.ts";
import { AndValidator } from "./validators/operators/and.ts";
import { NotValidator } from "./validators/operators/not.ts";
import { OrValidator } from "./validators/operators/or.ts";
import { type TypeStr, TypeValidator } from "./validators/operators/typeof.ts";
import { InValidator } from "./validators/operators/in.ts";
import { ValidDateValidator } from "./validators/date/valid_date.ts";
import { Error } from "./constants.ts";
const $TypeValidator = /* @__PURE__ */ Expectable(TypeValidator);
const $InstanceValidator = /* @__PURE__ */ Expectable(InstanceValidator);
const $AndValidator = /* @__PURE__ */ Expectable(AndValidator);
const $EnumValidator = /* @__PURE__ */ Expectable(EnumValidator);
const $NullishValidator = /* @__PURE__ */ Expectable(NullishValidator);
const $PatternValidator = /* @__PURE__ */ Expectable(PatternValidator);
const $RangeValidator = /* @__PURE__ */ Expectable(RangeValidator);
const $UniqueValidator = /* @__PURE__ */ Expectable(UniqueValidator);
const $EqualityValidator = /* @__PURE__ */ Expectable(EqualityValidator);
const $NegativeNumberValidator = /* @__PURE__ */ Expectable(
NegativeNumberValidator,
);
const $NonNegativeNumberValidator = /* @__PURE__ */ Expectable(
NonNegativeNumberValidator,
);
const $NonPositiveNumberValidator = /* @__PURE__ */ Expectable(
NonPositiveNumberValidator,
);
const $PositiveNumberValidator = /* @__PURE__ */ Expectable(
PositiveNumberValidator,
);
const $LessThanValidator = /* @__PURE__ */ Expectable(LessThanValidator);
const $LessThanOrEqualValidator = /* @__PURE__ */ Expectable(
LessThanOrEqualValidator,
);
const $GreaterThanValidator = /* @__PURE__ */ Expectable(GreaterThanValidator);
const $GreaterThanOrEqualValidator = /* @__PURE__ */ Expectable(
GreaterThanOrEqualValidator,
);
const $InequalityValidator = /* @__PURE__ */ Expectable(InequalityValidator);
const $NotValidator = /* @__PURE__ */ Expectable(NotValidator);
const $OrValidator = /* @__PURE__ */ Expectable(OrValidator);
const $InValidator = /* @__PURE__ */ Expectable(InValidator);
const $ValidDateValidator = /* @__PURE__ */ Expectable(ValidDateValidator);
const $CountValidator = /* @__PURE__ */ Expectable(CountValidator);
const $EmptyValidator = /* @__PURE__ */ Expectable(EmptyValidator);
const $MaxCountValidator = /* @__PURE__ */ Expectable(MaxCountValidator);
const $MinCountValidator = /* @__PURE__ */ Expectable(MinCountValidator);
const $NonEmptyValidator = /* @__PURE__ */ Expectable(NonEmptyValidator);
const $SingleValidator = /* @__PURE__ */ Expectable(SingleValidator);
const $FloatValidator = /* @__PURE__ */ Expectable(FloatValidator);
const $IntegerValidator = /* @__PURE__ */ Expectable(IntegerValidator);
/** Validator factory for JavaScript data type.
* The difference with `typeof` operator is that `"object"` does not match `null`.
*
* @example
* ```ts
* import { type } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = type("object")
* ```
*/
export function type<T extends TypeStr>(of: T) {
const validator = new $TypeValidator(of);
return validator.expect(({ input }) =>
format(Error.ShouldBeBut, [print(validator), typeof input])
);
}
/** String validator. */
export const string = /* @__PURE__ */ type("string");
/** Number validator. */
export const number = /* @__PURE__ */ type("number");
/** Bigint validator. */
export const bigint = /* @__PURE__ */ type("bigint");
/** Boolean validator. */
export const boolean = /* @__PURE__ */ type("boolean");
/** Symbol validator. */
export const symbol = /* @__PURE__ */ type("symbol");
/** Factory for enumerator validator.
*
* @example
* ```ts
* import { enumerator } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = enumerator(0, 1, 2, 3);
* const validator2 = enumerator("Red", "Yellow", "Green");
* ```
*/
export function enumerator<const T>(
v1: T,
v2: T,
...values: readonly T[]
) {
return new $EnumValidator(v1, v2, ...values).expect(shouldBeBut);
}
/** Validator factory equivalent to the `instanceof` operator.
*
* @example
* ```ts
* import { instance } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = instance(Array);
* ```
*/
export function instance<T extends Constructor>(of: T) {
const validator = new $InstanceValidator(of);
return validator.expect(({ input }) =>
format(Error.ShouldBeBut, [
print(validator),
input?.constructor.name ?? input,
])
);
}
/** Factory for properties validator.
*
* @example
* ```ts
* import {
* number,
* props,
* string,
* } from "https://deno.land/x/abstruct@$VERSION/mod.ts";
* const User = props({ name: string, age: number });
* ```
*/
export const props = /* @__PURE__ */ createInst(PropertiesValidator);
/** Factory for optional properties validator.
*
* @example
* ```ts
* import {
* number,
* optional,
* string,
* } from "https://deno.land/x/abstruct@$VERSION/mod.ts";
* const Profile = optional({ greeting: string, hobby: string });
* ```
*/
export const optional = /* @__PURE__ */ createInst(OptionalValidator);
/** Nullish(`null` or `undefined`) validator. */
export const nullish = /* @__PURE__ */ new $NullishValidator().expect(
shouldBeBut,
);
/** Validator factory equivalent to strict equality(`===`) operator.
*
* @example
* ```ts
* import { eq } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = eq(0);
* ```
*/
export function eq<const RIn = unknown>(value: RIn) {
return new $EqualityValidator(value).expect(shouldBeBut);
}
/** Factory for validator equivalent to less than(`>`) operator.
*
* @example
* ```ts
* import { lt } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = lt(256);
* ```
*/
export function lt<In>(base: In) {
return new $LessThanValidator(base).expect(shouldBeBut);
}
/** Factory for validator equivalent to less than or equal to (`>=`) operator.
*
* @example
* ```ts
* import { lte } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = lte(255);
* ```
*/
export function lte<In>(base: In) {
return new $LessThanOrEqualValidator(base).expect(shouldBeBut);
}
/** Factory for validator equivalent to greater than(`<`) operator.
*
* @example
* ```ts
* import { gt } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = gt(8);
* ```
*/
export function gt<In>(base: In) {
return new $GreaterThanValidator(base).expect(shouldBeBut);
}
/** Factory for validator equivalent to greater than or equal(`<=`) operator.
*
* @example
* ```ts
* import { gte } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = gte(8);
* ```
*/
export function gte<In>(base: In) {
return new $GreaterThanOrEqualValidator(base).expect(shouldBeBut);
}
/** Factory for validator equivalent to strict inequality(`!==`) operator.
*
* @example
* ```ts
* import { ne } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = ne(0);
* ```
*/
export function ne(value: unknown) {
return new $InequalityValidator(value).expect(shouldBeBut);
}
/** Factory for validator inversion.
*
* @example
* ```ts
* import {
* not,
* type Validator,
* } from "https://deno.land/x/abstruct@$VERSION/mod.ts";
*
* declare const validator: Validator;
* const inversionValidator = not(validator);
* ```
*/
export function not<In, RIn extends In = In>(
validator: Readonly<Validator<In, RIn>>,
) {
new NotValidator(validator);
return new $NotValidator(validator).expect(shouldBeBut);
}
/** Factory for validator composer like Logical OR.
*
* @example
* ```ts
* import {
* or,
* type Validator,
* } from "https://deno.land/x/abstruct@$VERSION/mod.ts";
* import { assertType, type Has } from "https://deno.land/std/testing/types.ts";
* declare const v1: Validator;
* declare const v2: Validator;
* declare const v3: Validator;
*
* const validator = or(v1, v2, v3);
* ```
*
* If the validators are not type-compatible with each other, generics must be
* specified.
*
* @example
* ```ts
* import {
* or,
* type Validator,
* } from "https://deno.land/x/abstruct@$VERSION/mod.ts";
* import { assertType, type Has } from "https://deno.land/std/testing/types.ts";
* declare const v1: Validator<unknown, string>;
* declare const v2: Validator<unknown, number>;
* const validator = or<unknown, string | number>(v1, v2);
*
* assertType<Has<typeof validator, Validator<unknown, string | number>>>(true);
* ```
*
* For more information, see
* [Specifying Type Arguments](https://www.typescriptlang.org/docs/handbook/2/functions.html#specifying-type-arguments).
*/
export function or<In = unknown, RIn extends In = In>(
v1: Readonly<Validator<In, RIn>>,
v2: Readonly<Validator<In, RIn>>,
...validations: Readonly<Validator<In, RIn>>[]
) {
new OrValidator(v1, v2);
return new $OrValidator(v1, v2, ...validations).expect(shouldBe);
}
/** Factory for validator composer like Logical AND.
* `and` composes multiple validators and creates a new validator. The composed
* validator executes the validator from left to right, just like the Logical AND
* operator. If the validation fails en route, the evaluation stops there.
*
* @example
*
* ```ts
* import {
* and,
* between,
* gte,
* int,
* lte,
* } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const _int8 = and(int, lte(128), gte(-127));
* const __int8 = and(int, between(-127, 128));
* ```
*
* ## Type-narrowing
* Composition of `and` is type-safe.
*
* Each validator is corrected to satisfy the narrowed type from the previous
* validators.
*
* @example
*
* ```ts
* import {
* and,
* type Validator,
* } from "https://deno.land/x/abstruct@$VERSION/mod.ts";
* import { assertType, type Has } from "https://deno.land/std/testing/types.ts";
* declare const v1: Validator<unknown, string>;
* declare const v2: Validator<string, "a" | "b">;
* declare const v3: Validator<"a" | "b" | "c", "a">;
* const validator = and(v1, v2, v3);
*
* assertType<Has<typeof validator, Validator<unknown, "a">>>(true);
* ```
*
* ## Limit the number of arguments
* The number of arguments is limited by overloading to achieve this.
* Currently it accepts up to 5 arguments.
* This limit is based on the strict `Function.bind` type signature.
* If more than that is needed, you must nest `and`.
*/
export function and<
In,
RIn extends In,
In2,
RIn2 extends In2,
>(
v1: Readonly<Validator<In, RIn>>,
v2: Readonly<Validator<In2 | RIn, RIn2>>,
): AndValidator<In, RIn & RIn2> & Expectation<{ input: In }>;
export function and<
In,
RIn extends In,
In2,
RIn2 extends In2,
In3,
RIn3 extends In3,
>(
v1: Readonly<Validator<In, RIn>>,
v2: Readonly<Validator<In2 | RIn, RIn2>>,
v3: Readonly<Validator<In3 | RIn & RIn2, RIn3>>,
): AndValidator<In, RIn & RIn2 & RIn3> & Expectation<{ input: In }>;
export function and<
In,
RIn extends In,
In2,
RIn2 extends In2,
In3,
RIn3 extends In3,
In4,
RIn4 extends In4,
>(
v1: Readonly<Validator<In, RIn>>,
v2: Readonly<Validator<In2 | RIn, RIn2>>,
v3: Readonly<Validator<In3 | RIn & RIn2, RIn3>>,
v4: Readonly<Validator<In4 | RIn & RIn2 & RIn3, RIn4>>,
): AndValidator<In, RIn & RIn2 & RIn3 & RIn4> & Expectation<{ input: In }>;
export function and<
In,
RIn extends In,
In2,
RIn2 extends In2,
In3,
RIn3 extends In3,
In4,
RIn4 extends In4,
In5,
RIn5 extends In5,
>(
v1: Readonly<Validator<In, RIn>>,
v2: Readonly<Validator<In2 | RIn, RIn2>>,
v3: Readonly<Validator<In3 | RIn & RIn2, RIn3>>,
v4: Readonly<Validator<In4 | RIn & RIn2 & RIn3, RIn4>>,
v5: Readonly<Validator<In5 | RIn & RIn2 & RIn3 & RIn4, RIn5>>,
):
& AndValidator<In, RIn & RIn2 & RIn3 & RIn4 & RIn5>
& Expectation<{ input: In }>;
export function and(
...validators: readonly [Readonly<Validator>, Readonly<Validator>]
) {
return new $AndValidator(validators);
}
/** Factory for existence of property validator.
*
* @example
* ```ts
* import { has } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = has("prop");
* ```
*/
export function has<const K extends PropertyKey>(key: K) {
return new $InValidator(key).expect(() => `should has ${print(key)}`);
}
/** Factory for range validator.
*
* @example
* ```ts
* import { between } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const numberRangeValidator = between(0, 255);
* const dateRangeValidator = between(new Date("1970/1/1"), new Date("2038/1/19"));
* ```
*/
export function between<In>(min: In, max: In) {
return new $RangeValidator(min, max).expect(shouldBeBut);
}
// Non-nullish
/** Factory for property key validator.
*
* @example
* ```ts
* import {
* propKey,
* type Validator,
* } from "https://deno.land/x/abstruct@$VERSION/mod.ts";
* declare const validator: Validator<string>;
* const keyValidator = propKey(validator);
* ```
*/
export const propKey = /* @__PURE__ */ createInst(PropertyKeyValidator);
/** Factory for property value validator.
*
* @example
* ```ts
* import {
* type Validator,
* propValue,
* } from "https://deno.land/x/abstruct@$VERSION/mod.ts";
* declare const validator: Validator;
* const valueValidator = propValue(validator);
* ```
*/
export const propValue = /* @__PURE__ */ createInst(PropertyValueValidator);
// Array
/** Factory for fixed array validator. It checks each item passes each {@link Validator}.
*
* @example
* ```ts
* import {
* fixedArray,
* type Validator,
* } from "https://deno.land/x/abstruct@$VERSION/mod.ts";
* declare const v1: Validator;
* declare const v2: Validator;
* const validator = fixedArray(v1, v2);
* ```
*/
export const fixedArray = /* @__PURE__ */ createInst(FixedArrayValidator);
// Date
/** Valid `Date` validator. */
export const validDate = /* @__PURE__ */ new $ValidDateValidator().expect(
shouldBe,
);
// iterable
/** Factory for count validator. It checks count(size, length) of items.
*
* @example
* ```ts
* import { count } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = count(5);
* ```
*/
export function count(of: number) {
const validator = new $CountValidator(of);
return validator.expect(({ input }) =>
format(Error.ShouldBeBut, [print(validator), getCount(input)])
);
}
/** Empty validator. It checks the items is empty.
*
* @example
*/
export const empty = /* @__PURE__ */ new $EmptyValidator().expect(shouldBe);
/** Factory for item validator.
* It checks each item of items.
*
* @example
* ```ts
* import {
* item,
* type Validator,
* } from "https://deno.land/x/abstruct@$VERSION/mod.ts";
* declare const validator: Validator;
* const itemValidator = item(validator);
* ```
*/
export const item = /* @__PURE__ */ createInst(ItemValidator);
/** Factory for max count validator. It checks items count is less than or equal to {@link limit}.
*
* @example
* ```ts
* import { maxCount } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* declare const limit: number;
* const validator = maxCount(limit);
* ```
*/
export function maxCount(limit: number) {
return new $MaxCountValidator(limit).expect(({ input }) =>
format(Error.MaxCount, [limit, getCount(input)])
);
}
/** Factory for min count validator. It checks items count is greater than or equal to {@link limit}.
*
* @example
* ```ts
* import { minCount } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* declare const limit: number;
* const validator = minCount(limit);
* ```
*/
export function minCount(limit: number) {
return new $MinCountValidator(limit).expect(({ input }) =>
format(Error.MinCount, [limit, getCount(input)])
);
}
/** Non-Empty validator. It checks items is non-empty. */
export const nonEmpty = /* @__PURE__ */ new $NonEmptyValidator()
.expect(shouldBe);
/** Single validator. It checks items is single. */
export const single = /* @__PURE__ */ new $SingleValidator().expect(shouldBe);
/** Unique validator. It checks the each item is unique. */
export const unique = /* @__PURE__ */ new $UniqueValidator().expect(shouldBe);
// number
/** Float validator. */
export const float = /* @__PURE__ */ new $FloatValidator().expect(shouldBeBut);
/** Integer validator. */
export const int = /* @__PURE__ */ new $IntegerValidator().expect(shouldBeBut);
/** Integer in the range -127 ~ 128 validator. */
export const int8 = /* @__PURE__ */ and(
int,
/* @__PURE__ */ between(-127, 128),
);
/** Integer in the range -32768 ~ 32767 validator. */
export const int16 = /* @__PURE__ */ and(
int,
/* @__PURE__ */ between(-32768, 32767),
);
/** Integer in the range -2147483648 ~ 2147483647 validator. */
export const int32 = /* @__PURE__ */ and(
int,
/* @__PURE__ */ between(-2147483648, 2147483647),
);
/** Integer in the range 0 ~ 255 validator. */
export const uint8 = /* @__PURE__ */ and(
int,
/* @__PURE__ */ between(0, 255),
);
/** Integer in the range 0 ~ 65535 validator. */
export const uint16 = /* @__PURE__ */ and(
int,
/* @__PURE__ */ between(0, 65535),
);
/** Integer in the range 0 ~ 4294967295 validator. */
export const uint32 = /* @__PURE__ */ and(
int,
/* @__PURE__ */ between(0, 4294967295),
);
/** Negative number validator. */
export const negative = /* @__PURE__ */ new $NegativeNumberValidator()
.expect(shouldBeBut);
/** Non-negative number validator. */
export const nonNegative = /* @__PURE__ */ new $NonNegativeNumberValidator()
.expect(shouldBeBut);
/** Non-positive number validator. */
export const nonPositive = /* @__PURE__ */ new $NonPositiveNumberValidator()
.expect(shouldBeBut);
/** Positive number validator. */
export const positive = /* @__PURE__ */ new $PositiveNumberValidator()
.expect(shouldBeBut);
// string
/** Factory for regex pattern validator.
*
* @example
* ```ts
* import { pattern } from "https://deno.land/x/abstruct@$VERSION/bindings.ts";
* const validator = pattern(/^\d*$/);
* ```
*/
export function pattern(
pattern: Readonly<RegExp>,
): PatternValidator & Expectation<{ input: string }> {
const validator = new $PatternValidator(pattern);
return validator.expect(({ input }) =>
format(Error.ShouldBeBut, [`match ${validator}`, print(input)])
);
}