-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathliteral.ts
More file actions
108 lines (102 loc) 路 2.3 KB
/
Copy pathliteral.ts
File metadata and controls
108 lines (102 loc) 路 2.3 KB
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
import type {
BaseIssue,
BaseSchema,
ErrorMessage,
OutputDataset,
} from '../../types/index.ts';
import { _addIssue, _getStandardProps, _stringify } from '../../utils/index.ts';
/**
* Literal type.
*/
export type Literal = bigint | boolean | number | string | symbol;
/**
* Literal issue interface.
*/
export interface LiteralIssue extends BaseIssue<unknown> {
/**
* The issue kind.
*/
readonly kind: 'schema';
/**
* The issue type.
*/
readonly type: 'literal';
/**
* The expected property.
*/
readonly expected: string;
}
/**
* Literal schema interface.
*/
export interface LiteralSchema<
TLiteral extends Literal,
TMessage extends ErrorMessage<LiteralIssue> | undefined,
> extends BaseSchema<TLiteral, TLiteral, LiteralIssue> {
/**
* The schema type.
*/
readonly type: 'literal';
/**
* The schema reference.
*/
readonly reference: typeof literal;
/**
* The literal value.
*/
readonly literal: TLiteral;
/**
* The error message.
*/
readonly message: TMessage;
}
/**
* Creates a literal schema.
*
* @param literal_ The literal value.
*
* @returns A literal schema.
*/
export function literal<const TLiteral extends Literal>(
literal_: TLiteral
): LiteralSchema<TLiteral, undefined>;
/**
* Creates a literal schema.
*
* @param literal_ The literal value.
* @param message The error message.
*
* @returns A literal schema.
*/
export function literal<
const TLiteral extends Literal,
const TMessage extends ErrorMessage<LiteralIssue> | undefined,
>(literal_: TLiteral, message: TMessage): LiteralSchema<TLiteral, TMessage>;
// @__NO_SIDE_EFFECTS__
export function literal(
literal_: Literal,
message?: ErrorMessage<LiteralIssue>
): LiteralSchema<Literal, ErrorMessage<LiteralIssue> | undefined> {
return {
kind: 'schema',
type: 'literal',
reference: literal,
expects: _stringify(literal_),
async: false,
literal: literal_,
message,
get '~standard'() {
return _getStandardProps(this);
},
'~run'(dataset, config) {
if (Object.is(dataset.value, this.literal)) {
// @ts-expect-error
dataset.typed = true;
} else {
_addIssue(this, 'type', dataset, config);
}
// @ts-expect-error
return dataset as OutputDataset<Literal, LiteralIssue>;
},
};
}