forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain.js
More file actions
35 lines (27 loc) · 867 Bytes
/
Copy pathdomain.js
File metadata and controls
35 lines (27 loc) · 867 Bytes
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
import { Kind, GraphQLError, GraphQLScalarType } from 'graphql'
const validate = (value) => {
if (typeof value !== typeof 'string') {
throw new TypeError(`Value is not a string: ${typeof value}`)
}
value = value.toLowerCase()
const DOMAIN_REGEX = /^((?=[a-z0-9-_]{1,63}\.)(xn--)?[a-z0-9_]+(-[a-z0-9_]+)*\.)+[a-z]{2,63}$/
if (!DOMAIN_REGEX.test(value)) {
throw new TypeError(`Value is not a valid domain: ${value}`)
}
return value
}
export const Domain = new GraphQLScalarType({
name: 'DomainScalar',
description: 'String that conforms to a domain structure.',
serialize: validate,
parseValue: validate,
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(`Can only validate strings as domains but got a: ${ast.kind}`)
}
return validate(ast.value)
},
})
module.exports = {
Domain,
}