forked from auth0/lock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
208 lines (169 loc) · 5.25 KB
/
index.js
File metadata and controls
208 lines (169 loc) · 5.25 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React from 'react';
import { Map } from 'immutable';
import trim from 'trim';
import OptionSelectionPane from './option_selection_pane';
import * as l from '../core/index';
const minMax = (value, min, max) => value.length >= min && value.length <= max;
const getDefaultValidator = field => {
switch (field) {
case 'family_name':
case 'given_name':
return str => minMax(trim(str), 1, 150);
case 'name':
return str => minMax(trim(str), 1, 300);
case 'nickname':
return str => minMax(trim(str), 1, 300);
default:
return str => trim(str).length > 0;
}
};
export function setField(m, field, value, validator = getDefaultValidator(field), ...args) {
const prevValue = m.getIn(['field', field, 'value']);
const prevShowInvalid = m.getIn(['field', field, 'showInvalid'], false);
const validation = validate(validator, value, ...args);
return m.mergeIn(
['field', field],
validation.merge({
value: value,
showInvalid: prevShowInvalid && prevValue === value
})
);
}
function validate(validator, value, ...args) {
if (typeof validator != 'function') return Map({ valid: true });
const validation = validator(value, ...args);
return validation && typeof validation === 'object'
? Map({ valid: validation.valid, invalidHint: validation.hint })
: Map({ valid: !!validation });
}
// TODO: this should handle icons, and everything.
// TODO: also there should be a similar fn for regular fields.
export function registerOptionField(m, field, options, initialValue) {
let valid = true,
hasInitial = !initialValue,
initialOption;
options.forEach(x => {
valid =
valid &&
x.get('label') &&
typeof x.get('label') === 'string' &&
x.get('value') &&
typeof x.get('value') === 'string';
if (!hasInitial && x.get('value') === initialValue) {
initialOption = x;
hasInitial = true;
}
});
if (!valid || !options.size) {
const stopError = new Error(
`The options provided for the "${field}" field are invalid, they must have the following format: {label: "non-empty string", value: "non-empty string"} and there has to be at least one option.`
);
stopError.code = 'invalid_select_field';
// TODO: in the future we might want to return the result of the
// operation along with the model insteand of stopping the
// rendering, like [false, m] in the case of failure and [true, m]
// in the case of success.
return l.stop(m, stopError);
}
if (!initialOption) initialOption = Map({});
return m.mergeIn(
['field', field],
initialOption,
Map({
options: options,
showInvalid: false,
valid: !initialOption.isEmpty()
})
);
}
export function setOptionField(m, field, option) {
return m.mergeIn(
['field', field],
option.merge(
Map({
valid: true,
showInvalid: false
})
)
);
}
export function isFieldValid(m, field) {
return m.getIn(['field', field, 'valid']);
}
export function getFieldInvalidHint(m, field) {
return m.getIn(['field', field, 'invalidHint'], '');
}
export function isFieldVisiblyInvalid(m, field) {
return m.getIn(['field', field, 'showInvalid'], false) && !m.getIn(['field', field, 'valid']);
}
export function showInvalidField(m, field) {
return m.setIn(['field', field, 'showInvalid'], !isFieldValid(m, field));
}
export function hideInvalidFields(m) {
return m.update('field', fields => {
return fields && fields.map(field => field.set('showInvalid', false));
});
}
// TODO: only used in passwordless, when we update it to use
// validateAndSubmit this won't be needed anymore.
export function setFieldShowInvalid(m, field, value) {
return m.setIn(['field', field, 'showInvalid'], value);
}
export function clearFields(m, fields) {
let keyPaths;
if (!fields || fields.length === 0) {
keyPaths = [['field']];
} else {
keyPaths = fields.map(x => ['field', x]);
}
return keyPaths.reduce((r, v) => r.removeIn(v), m);
}
export function getField(m, field, notFound = new Map({})) {
return m.getIn(['field', field], notFound);
}
export function getFieldValue(m, field, notFound = '') {
return getField(m, field).get('value', notFound);
}
export function getFieldLabel(m, field, notFound = '') {
return getField(m, field).get('label', notFound);
}
// phone number
export function phoneNumber(lock) {
return lock.getIn(['field', 'phoneNumber', 'value'], '');
}
// email
export function email(m) {
return getFieldValue(m, 'email');
}
// vcode
export function vcode(m) {
return getFieldValue(m, 'vcode');
}
// password
export function password(m) {
return getFieldValue(m, 'password');
}
// username
export function username(m) {
return getFieldValue(m, 'username');
}
// mfa_code
export function mfaCode(m) {
return getFieldValue(m, 'mfa_code');
}
// select field options
export function isSelecting(m) {
return !!m.getIn(['field', 'selecting']);
}
export function renderOptionSelection(m) {
const name = m.getIn(['field', 'selecting', 'name']);
return isSelecting(m) ? (
<OptionSelectionPane
model={m}
name={name}
icon={m.getIn(['field', 'selecting', 'icon'])}
iconUrl={m.getIn(['field', 'selecting', 'iconUrl'])}
items={m.getIn(['field', name, 'options'])}
/>
) : null;
}