Repository navigation
Expand file tree
/
Copy pathDefaultTypes.ts
More file actions
171 lines (144 loc) · 4.12 KB
/
Copy pathDefaultTypes.ts
File metadata and controls
171 lines (144 loc) · 4.12 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
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
import validator from 'validator'
import zipcodes from 'zipcodes-regex'
import { Is } from './Is'
import { Types } from './Types'
import { TypeProperties } from './TypeProperties'
class $ {
public static readonly MaxDate: Date = new Date('2038-01-19 03:14:07')
public static readonly MinDate: Date = new Date('1970-01-01 00:00:01')
public static bounded(value: number, props?: TypeProperties): boolean {
return $.clamped(value, props) === value
}
public static clamped(value: number, props?: TypeProperties): number {
if (props && props.max && value > props.max) {
return props.max
}
if (props && props.min && value < props.min) {
return props.min
}
return value
}
public static consumed(value: any, props?: TypeProperties): boolean {
return !value && props && props.required ? false : true
}
public static timestamp(timestamp: number): boolean {
const date = new Date(timestamp)
const max = $.MaxDate
const min = $.MinDate
return date > min && date < max
}
}
const phone = (): RegExp =>
/^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i
Types.register({
default: 'max',
type: 'array',
typebase: 'Array',
validator: (value: any, props?: TypeProperties) => Is.array(value) && $.consumed(value, props),
})
Types.register({
type: 'boolean',
typebase: 'Boolean',
validator: (value: any, props?: TypeProperties) => Is.boolean(value) && $.consumed(value, props),
})
Types.register({
type: 'date',
typebase: 'Date',
validator: (value: any, props?: TypeProperties) => Is.date(value) && $.consumed(value, props),
})
Types.register({
default: 'max',
properties: {
max: 254,
},
type: 'email',
typebase: 'string',
validator: (value: any, props?: TypeProperties) =>
Is.string(value) && $.consumed(value, props) ? validator.isEmail(value) : false,
})
Types.register({
type: 'error',
typebase: 'Error',
validator: (value: any, props?: TypeProperties) => Is.error(value) && $.consumed(value, props),
})
Types.register({
default: 'max',
type: 'number',
typebase: 'Number',
validator: (value: any, props?: TypeProperties) =>
Is.number(value) && $.consumed(value, props) && $.bounded(value, props),
})
Types.register({
properties: {
required: true,
},
type: 'object',
typebase: 'Object',
validator: (value: any, props?: TypeProperties) => Is.object(value) && $.consumed(value, props),
})
Types.register({
properties: {
max: 64,
required: true,
},
type: 'phone',
typebase: 'string',
validator: (value: any, props?: TypeProperties) => phone().test(value) && $.consumed(value, props),
})
Types.register({
properties: {
max: 16,
min: 5,
required: true,
},
type: 'postalcode',
typebase: 'string',
validator: (value: any, props?: TypeProperties, country?: string): boolean => {
if (Is.string(value)) {
const pattern: string = zipcodes[country || 'US']
const regex: RegExp = new RegExp(pattern)
return regex.test(value) && $.consumed(value, props)
}
return false
},
})
Types.register({
default: 'max',
type: 'string',
typebase: 'String',
validator: (value: any, props?: TypeProperties): boolean => {
const valid: boolean = Is.string(value) && $.consumed(value, props)
if (valid === false && props && props.required) {
return false
}
if (!value) {
return valid
}
if (props && props.max && value.length > props.max) {
return false
} else if (props && props.min && value.length < props.min) {
return false
}
return valid
},
})
Types.register({
type: 'null',
typebase: 'null',
validator: (value: any) => value === null,
})
Types.register({
type: 'undefined',
typebase: 'undefined',
validator: (value: any) => value === undefined,
})
Types.register({
properties: {
max: 8640000000000000,
min: -8640000000000000,
required: true,
},
type: 'timestamp',
typebase: 'number',
validator: (value: any, props?: TypeProperties) => Is.number(value) && $.consumed(value, props) && $.timestamp(value),
})