This repository was archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathvalidate.js
61 lines (45 loc) · 1.55 KB
/
validate.js
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
/* eslint import/no-cycle: 0 */
import { YES, NO } from 'constants/values'
import validate from './validateConfig'
export const validateModel = (data = {}, model, options) => {
const errors = options
? validate(data, model, options)
: validate(data, model)
// console.log('validate model', data, model, errors)
if (!errors) return true
return errors
}
export default validateModel
// Misc helpers (might be moved later)
/** require Yes or No */
export const hasYesOrNo = {
inclusion: [YES, NO],
}
/** check the value of an attribute.value */
export const checkValue = (attribute, expected) => !!(attribute
&& attribute.value
&& attribute.value === expected)
export const checkValueIncluded = (attribute, expected) => attribute
&& attribute.value
&& expected.includes(attribute.value)
export const valueIsEmpty = (data = {}) => !data.value || validate.isEmpty(data.value)
export const nameIsEmpty = (data = {}) => {
const {
first, firstInitialOnly, middle, middleInitialOnly, last, suffix,
} = data
const fields = [
first, firstInitialOnly, middle, middleInitialOnly, last, suffix,
]
return fields.every(i => i === false || validate.isEmpty(i))
}
export const dateIsEmpty = (data = {}) => {
const { day, month, year } = data
const fields = [day, month, year]
return fields.every(i => validate.isEmpty(i))
}
export const locationIsEmpty = (data = {}) => {
const { country, city, state } = data
return (validate.isEmpty(country) || valueIsEmpty(country))
&& validate.isEmpty(city)
&& validate.isEmpty(state)
}