-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.ts
More file actions
167 lines (147 loc) · 4.23 KB
/
validator.ts
File metadata and controls
167 lines (147 loc) · 4.23 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Node.js GraphQL API Starter Kit
* https://github.com/kriasoft/nodejs-api-starter
* Copyright © 2016-present Kriasoft | MIT License
*/
import isEmail from 'validator/lib/isEmail';
import isLength from 'validator/lib/isLength';
import isURL from 'validator/lib/isURL';
import textTrim from 'validator/lib/trim';
import { fromGlobalId } from 'graphql-relay';
function isEmpty(value) {
return typeof value === 'undefined' || value === null || value === '';
}
/**
* A set of validation helper methods that are indended to reduce
* the amount of boilerpate code in GraphQL mutations. It's is based
* on the popular "validator" library that you can find at:
* https://github.com/chriso/validator.js
*/
export class Validator {
errors = [];
states = [];
constructor(input, mode, onError) {
this.input = input;
this.mode = mode || 'create';
this.onError = onError;
if (!(this.mode === 'create' || this.mode === 'update')) {
throw new Error(`The "${mode}" validation mode is not supported.`);
}
}
/**
* Initialized a new state for the field.
*/
field(key, { as, alias, trim, transform, type } = {}) {
const name = alias || key;
let value = this.input[key];
if (value && trim) {
value = textTrim(value, trim === true ? undefined : trim);
}
if (value && transform) {
value = transform(value);
}
if (value && type) {
const globalId = fromGlobalId(value);
if (globalId.type !== type) {
throw new Error(
`Expected an ID of type '${type}' but got '${globalId.type}'.`,
);
}
value = globalId.id;
}
this.state = {
key,
as,
name,
value,
promise: undefined,
addError: message =>
this.errors.push({
key,
message: message || `The ${name} field is invalid.`,
}),
};
this.states.push(this.state);
return this;
}
isRequired(message) {
if (
(((this.input.validateOnly === true || this.mode === 'update') &&
this.state.value !== undefined) ||
this.mode === 'create') &&
!this.state.value
) {
this.state.addError(
message || `The ${this.state.name} field cannot be empty.`,
);
}
return this;
}
isEmail(options, message) {
if (!isEmpty(this.state.value) && !isEmail(this.state.value, options)) {
this.state.addError(message || 'The email address is invalid.');
}
return this;
}
isLength(options, message) {
if (!isEmpty(this.state.value) && !isLength(this.state.value, options)) {
if (options && options.min && options.max) {
this.state.addError(
message ||
`The ${this.state.name} field must be between ${options.min} and ${options.max} characters long.`,
);
} else if (options && options.max) {
this.state.addError(
message ||
`The ${this.state.name} field must be up to ${options.max} characters long.`,
);
} else {
this.state.addError(message);
}
}
return this;
}
isURL(options, message) {
if (!isEmpty(this.state.value) && !isURL(this.state.value, options)) {
this.state.addError(message);
}
return this;
}
is(check, message) {
this.state.promise = (
this.state.promise || Promise.resolve(this.state)
).then(state =>
isEmpty(state.value)
? state
: Promise.resolve()
.then(() => check(state.value, message))
.then(isValid => {
if (!isValid) state.addError(message);
return state;
})
.catch(err => {
state.addError(err.message);
return Promise.resolve(state);
}),
);
return this;
}
validate() {
const done = states => {
if (this.errors.length && this.onError) {
this.onError(this.errors);
}
return states
.filter(x => x.value !== undefined)
.reduce((acc, state) => {
acc[state.as || state.key] = state.value;
return acc;
}, {});
};
return this.states.some(x => x.promise)
? Promise.all(this.states.map(x => x.promise || Promise.resolve(x))).then(
done,
)
: done(this.states);
}
}