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 pathusPassport.js
57 lines (50 loc) · 1.67 KB
/
usPassport.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
import { hasYesOrNo, checkValue } from 'models/validate'
import name from 'models/shared/name'
import { passportPattern } from 'constants/patterns'
import { createDateFromObject } from 'helpers/date'
import { DEFAULT_LATEST } from 'constants/dateLimits'
const usPassport = {
HasPassports: {
presence: true,
hasValue: { validator: hasYesOrNo },
},
Name: (value, attributes) => (
checkValue(attributes.HasPassports, 'No')
? {}
: {
presence: true,
model: { validator: name },
}
),
Number: (value, attributes) => {
const { HasPassports, Issued } = attributes
if (checkValue(HasPassports, 'No')) return {}
const validations = {
presence: true,
hasValue: { validator: { format: passportPattern } },
}
// If issued in 1990 or after, must be 9 characters long
const issuedDate = Issued && createDateFromObject(Issued)
const dateThreshold = createDateFromObject({ day: 1, month: 1, year: 1990 })
if (issuedDate && (issuedDate >= dateThreshold)) {
validations.hasValue.validator.length = { is: 9 }
}
return validations
},
Issued: (value, attributes, attributeName, options = {}) => {
if (checkValue(attributes.HasPassports, 'No')) return {}
const { applicantBirthdate } = options
return {
presence: true,
date: { earliest: applicantBirthdate, latest: DEFAULT_LATEST },
}
},
Expiration: (value, attributes) => {
const { HasPassports, Issued } = attributes
if (checkValue(HasPassports, 'No')) return {}
const dateLimits = {}
if (Issued) dateLimits.earliest = Issued
return { presence: true, date: dateLimits }
},
}
export default usPassport