forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorganization-acronym.js
More file actions
31 lines (26 loc) · 816 Bytes
/
organization-acronym.js
File metadata and controls
31 lines (26 loc) · 816 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
import { Kind, GraphQLError, GraphQLScalarType } from 'graphql'
const validate = (value) => {
const ACRONYM_REGEX = /^[A-Z0-9_-]{1,50}$/
if (typeof value !== 'string') {
throw new TypeError(`Value is not string: ${typeof value}`)
}
if (!ACRONYM_REGEX.test(value)) {
throw new TypeError(`Value is not a valid acronym: ${value}`)
}
return value
}
export const Acronym = new GraphQLScalarType({
name: 'Acronym',
description:
'A field whose value is an upper case letter or an under score that has a length between 1 and 50.',
serialize: validate,
parseValue: validate,
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(
`Can only validate strings as acronyms but got a: ${ast.kind}`,
)
}
return validate(ast.value)
},
})