1
|
|
|
import { |
2
|
|
|
T, |
3
|
|
|
__, |
4
|
|
|
all, |
5
|
|
|
allPass, |
6
|
|
|
applyTo, |
7
|
|
|
clamp, |
8
|
|
|
complement, |
9
|
|
|
cond, |
10
|
|
|
contains, |
11
|
|
|
curry, |
12
|
|
|
equals, |
13
|
|
|
evolve, |
14
|
|
|
flip, |
15
|
|
|
gte, |
16
|
|
|
identity, |
17
|
|
|
is, |
18
|
|
|
length, |
19
|
|
|
lte, |
20
|
|
|
map, |
21
|
|
|
none, |
22
|
|
|
propEq, |
23
|
|
|
propOr, |
24
|
|
|
replace, |
25
|
|
|
split, |
26
|
|
|
take, |
27
|
|
|
tryCatch, |
28
|
|
|
unary, |
29
|
|
|
unless, |
30
|
|
|
values |
31
|
|
|
} from 'ramda'; |
32
|
|
|
import { isEmail, isHexColor, isURL } from 'validator'; |
33
|
|
|
import { components } from './input'; |
34
|
|
|
|
35
|
|
|
export const Integer = { name: 'Integer' }; |
36
|
|
|
export const Double = { name: 'Double' }; |
37
|
|
|
export const DateTime = { name: 'DateTime' }; |
38
|
|
|
export const Natural = { name: 'Natural' }; |
39
|
|
|
export const Char = length => ({ name: 'Char', length }); |
40
|
|
|
export const IntegerRange = (from, to) => ({ name: 'IntegerRange', from, to }); |
41
|
|
|
export const DoubleRange = (from, to) => ({ name: 'DoubleRange', from, to }); |
42
|
|
|
export const Money = { name: 'Money' }; |
43
|
|
|
export const String = { name: 'String' }; |
|
|
|
|
44
|
|
|
export const Color = { name: 'Color' }; |
45
|
|
|
export const Email = { name: 'Email' }; |
46
|
|
|
export const Checkbox = { name: 'Checkbox' }; |
47
|
|
|
export const OneOf = values => ({ name: 'OneOf', values }); |
48
|
|
|
export const Url = { name: 'Url' }; |
49
|
|
|
export const IntegerMultiRange = (from, to) => ({ name: 'IntegerMultiRange', from, to }); |
50
|
|
|
export const Calendar = { name: 'Calendar' }; |
51
|
|
|
export const AutoComplete = { name: 'AutoComplete' }; |
52
|
|
|
export const Location = { name: 'Location' }; |
53
|
|
|
export const SelectBox = values => ({ name: 'SelectBox', values }); |
54
|
|
|
export const MultiSelectBox = values => ({ name: 'MultiSelectBox', values }); |
55
|
|
|
export const File = ({ name: 'File' }); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the human-readable name of a type |
59
|
|
|
* |
60
|
|
|
* @author Marcelo Haskell Camargo |
61
|
|
|
* @param {String} type {anonymous} |
62
|
|
|
* @return {String} {anonymous} |
63
|
|
|
*/ |
64
|
|
|
export const getTypeName = cond([ |
65
|
|
|
[propEq('name', 'Char'), t => `Char(${t.length})`], |
66
|
|
|
[propEq('name', 'IntegerRange'), t => `IntegerRange(${t.from}, ${t.to})`], |
67
|
|
|
[propEq('name', 'DoubleRange'), t => `DoubleRange(${t.from}, ${t.to})`], |
68
|
|
|
[propEq('name', 'OneOf'), t => `OneOf([${t.values.join(', ')}])`], |
69
|
|
|
[propEq('name', 'SelectBox'), t => `SelectBox(${JSON.stringify(t.values)})`], |
70
|
|
|
[propEq('name', 'IntegerMultiRange'), t => `IntegerMultiRange(${t.from}, ${t.to})`], |
71
|
|
|
[propEq('name', 'MultiSelectBox'), t => `MultiSelectBox(${JSON.stringify(t.values)})`], |
72
|
|
|
[T, _.name] |
|
|
|
|
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
export const validator = { |
76
|
|
|
Color: isHexColor, |
77
|
|
|
Double: complement(isNaN), |
|
|
|
|
78
|
|
|
Email: unary(isEmail), |
79
|
|
|
Integer: complement(isNaN), |
80
|
|
|
IntegerMultiRange: (from, to) => allPass([ |
81
|
|
|
length & equals(2), |
82
|
|
|
none(isNaN), |
|
|
|
|
83
|
|
|
evolve([gte(__, from), lte(__, to)]) & values & all(identity)]), |
84
|
|
|
Money: complement(isNaN), |
85
|
|
|
Natural: lte(0), |
86
|
|
|
OneOf: flip(contains), |
87
|
|
|
Range: (from, to) => clamp(from, to, _) === _, |
|
|
|
|
88
|
|
|
Url: unary(isURL) |
89
|
|
|
}; |
90
|
|
|
|
91
|
|
|
export const validate = curry((type, value) => |
92
|
|
|
propOr(~{}, type.name, components) |
93
|
|
|
| applyTo(type) |
94
|
|
|
| propOr(~true, 'validate') |
95
|
|
|
| applyTo(value)); |
96
|
|
|
|
97
|
|
|
export const filter = { |
98
|
|
|
Calendar: date => new Date(date), |
99
|
|
|
Char: take, |
100
|
|
|
Double: parseFloat, |
|
|
|
|
101
|
|
|
Integer: parseInt(_, 10), |
|
|
|
|
102
|
|
|
IntegerMultiRange: unless(is(Array), split(' ') & map(parseInt(_, 10))), |
103
|
|
|
Money: tryCatch(replace(',', '.') & parseFloat, ~NaN) |
104
|
|
|
}; |
105
|
|
|
|