-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathuuid.ts
101 lines (96 loc) · 2.17 KB
/
uuid.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
import { UUID_REGEX } from '../../regex.ts';
import type {
BaseIssue,
BaseValidation,
ErrorMessage,
} from '../../types/index.ts';
import { _addIssue } from '../../utils/index.ts';
/**
* UUID issue type.
*/
export interface UuidIssue<TInput extends string> extends BaseIssue<TInput> {
/**
* The issue kind.
*/
readonly kind: 'validation';
/**
* The issue type.
*/
readonly type: 'uuid';
/**
* The expected property.
*/
readonly expected: null;
/**
* The received property.
*/
readonly received: `"${string}"`;
/**
* The UUID regex.
*/
readonly requirement: RegExp;
}
/**
* UUID action type.
*/
export interface UuidAction<
TInput extends string,
TMessage extends ErrorMessage<UuidIssue<TInput>> | undefined,
> extends BaseValidation<TInput, TInput, UuidIssue<TInput>> {
/**
* The action type.
*/
readonly type: 'uuid';
/**
* The action reference.
*/
readonly reference: typeof uuid;
/**
* The expected property.
*/
readonly expects: null;
/**
* The UUID regex.
*/
readonly requirement: RegExp;
/**
* The error message.
*/
readonly message: TMessage;
}
/**
* Creates an [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) validation action.
*
* @returns An UUID action.
*/
export function uuid<TInput extends string>(): UuidAction<TInput, undefined>;
/**
* Creates an [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) validation action.
*
* @param message The error message.
*
* @returns An UUID action.
*/
export function uuid<
TInput extends string,
const TMessage extends ErrorMessage<UuidIssue<TInput>> | undefined,
>(message: TMessage): UuidAction<TInput, TMessage>;
export function uuid(
message?: ErrorMessage<UuidIssue<string>>
): UuidAction<string, ErrorMessage<UuidIssue<string>> | undefined> {
return {
kind: 'validation',
type: 'uuid',
reference: uuid,
async: false,
expects: null,
requirement: UUID_REGEX,
message,
'~run'(dataset, config) {
if (dataset.typed && !this.requirement.test(dataset.value)) {
_addIssue(this, 'UUID', dataset, config);
}
return dataset;
},
};
}