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
33 lines (28 loc) · 784 Bytes
/
domain.js
File metadata and controls
33 lines (28 loc) · 784 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
import { Kind, GraphQLError, GraphQLScalarType } from 'graphql'
import psl from 'psl'
const validate = (value) => {
if (typeof value !== typeof 'string') {
throw new TypeError(`Value is not a string: ${typeof value}`)
}
if (!psl.isValid(value)) {
throw new TypeError(`Value is not a valid domain: ${value}`)
}
return value.toLowerCase()
}
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,
}