forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector.js
More file actions
30 lines (26 loc) · 793 Bytes
/
selector.js
File metadata and controls
30 lines (26 loc) · 793 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
import { Kind, GraphQLError, GraphQLScalarType } from 'graphql'
const validate = (value) => {
const SLUG_REGEX = /\w+\._domainkey/
if (typeof value !== typeof 'string') {
throw new TypeError(`Value is not a string: ${typeof value}`)
}
if (!SLUG_REGEX.test(value)) {
throw new TypeError(`Value is not a valid selector: ${value}`)
}
return value
}
export const Selectors = new GraphQLScalarType({
name: 'Selector',
description:
'A field that conforms to a string, with strings ending in ._domainkey.',
serialize: validate,
parseValue: validate,
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(
`Can only validate strings as selectors but got a: ${ast.kind}`,
)
}
return validate(ast.value)
},
})