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
|
const ValidateJS = require('validate.js')
// HACK(steve): wierd typescript situation because of strange typings
const Validate: any = ValidateJS.default ? ValidateJS.default : ValidateJS
/**
* Validates that 1 attribute doesn't appear in another's attributes content.
*/
Validate.validators.excludes = function custom(value, options, key, attributes) {
const list = attributes[options.attribute] || []
if (value && list.includes(value)) {
return options.message || `${value} is in the list`
}
}
/**
* Validates that another attribute isn't true.
*/
Validate.validators.tripped = function custom(value, options, key, attributes) {
if (value && attributes[options.attribute] === true) {
return options.message || `${options.attribute} is true`
}
}
/**
* Defines the rules for validating.
*
* Example:
* ```ts
* const RULES = {
* favoriteBand: {
* inclusion: { ['Weezer', 'Other'], message: 'Pick wisely.' }
* },
* name: {
* presence: { message: 'A developer has no name?' }
* }
* }
* validate(RULES, {})
* ```
*
* See https://validatejs.org/#validators for more examples.
*
*/
export interface ValidationRules {
[key: string]: Record<string, unknown>
}
/**
* An object containing any errors found.
*
* Example:
* ```js
* {
* email: ['Invalid email address.'],
* password: [
* 'Password must be 6 characters.',
* 'Password must have at least 1 digit.'
* ]
* }
* ```
*/
export interface ValidationErrors {
[key: string]: string[]
}
/**
* Runs the given rules against the data object.
*
* @param rules The rules to apply.
* @param data The object to validate.
*/
export function validate(rules: ValidationRules, data: Record<string, unknown>): ValidationErrors {
if (typeof data !== 'object') {
return {} as ValidationErrors
}
return Validate(data, rules, { fullMessages: false }) || {}
}
|