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
50 lines (42 loc) · 1.54 KB
/
Copy pathselector.js
File metadata and controls
50 lines (42 loc) · 1.54 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
import { Kind, GraphQLError, GraphQLScalarType } from 'graphql'
const validateSelector = (value) => {
if (typeof value !== typeof 'string') {
throw new TypeError(`Value is not a string: ${typeof value}`)
}
return value
}
export const Selectors = new GraphQLScalarType({
name: 'Selector',
description: 'A field that conforms to a DKIM selector',
serialize: validateSelector,
parseValue: validateSelector,
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(`Can only validate strings as selectors but got a: ${ast.kind}`)
}
return validateSelector(ast.value)
},
})
const validateSelectorInput = (value) => {
const SLUG_REGEX = /^(?:[a-zA-Z0-9](\.?[a-zA-Z0-9])*|\*)$/
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 SelectorsInput = new GraphQLScalarType({
name: 'SelectorInput',
description:
'A field that conforms to a DKIM selector for input. Must be either a single asterisk or a string where only alphanumeric characters and periods are allowed, string must also start and end with alphanumeric characters',
serialize: validateSelectorInput,
parseValue: validateSelectorInput,
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(`Can only validate strings as selectors but got a: ${ast.kind}`)
}
return validateSelectorInput(ast.value)
},
})