-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathvalidation.ts
44 lines (42 loc) · 1.06 KB
/
validation.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
import * as url from 'url'
import { fromHexString } from '@eth-optimism/core-utils'
export const validators = {
isBoolean: (val: any): boolean => {
return typeof val === 'boolean'
},
isString: (val: any): boolean => {
return typeof val === 'string'
},
isHexString: (val: any): boolean => {
return (
validators.isString(val) &&
val.startsWith('0x') &&
fromHexString(val).length === (val.length - 2) / 2
)
},
isAddress: (val: any): boolean => {
return validators.isHexString(val) && val.length === 42
},
isInteger: (val: any): boolean => {
return Number.isInteger(val)
},
isUrl: (val: any): boolean => {
try {
const parsed = new url.URL(val)
return (
parsed.protocol === 'ws:' ||
parsed.protocol === 'http:' ||
parsed.protocol === 'https:'
)
} catch (err) {
return false
}
},
isJsonRpcProvider: (val: any): boolean => {
return val && val.ready !== undefined
},
isLevelUP: (val: any): boolean => {
// TODO: Fix?
return val && val.db
},
}