@@ -5,32 +5,43 @@ import { isValidAddress } from './wallet';
55
66const hexRegExp = /[0-9A-Fa-f]{64}/g;
77
8−// // Note that this validator also transforms data
9−// const validateBase64 = ({ data, pass, error }) => {
10−// if (pass !== true) return { data, pass, error }
11−//
12−// try {
13−// atob(data);
14−// return { data: atob(data), pass: true, error: '' }
15−// } catch(e) {
16−// return { data: data, pass: false, error: 'File is not valid base64.' }
17−// }
18−// };
19−//
20−// // Note that this validator also transforms data
21−// const validateJSON = ({ data, pass, error }) => {
22−// if (pass !== true) return { data, pass, error }
23−//
24−// try {
25−// JSON.parse(data)
26−// return { data: JSON.parse(data), pass: true, error: '' }
27−// } catch(e) {
28−// return { data: data, pass: false, error: 'File does not decode into valid JSON.' }
29−// }
30−// };
8+// Wraps single validation functions in a controlled and predictable way.
9+export const simpleValidatorWrapper = config => {
10+ // If a previous validation has already failed, skip this validation and
11+ // return the prev message to the next stage in the validation function chain.
12+ // Failed validations should drop all the way down the chain and drop out of
13+ // the output.
14+ if (!config.prevMessage.pass) {
15+ return config.prevMessage;
16+ }
17+
18+ // Run the validator and return the result.
19+ return config.validator(config.prevMessage.data)
20+ ? newMessage(config.prevMessage.data, true, '')
21+ : newMessage(config.prevMessage.data, false, config.errorMessage);
22+};
23+
24+// Validation message
25+// {
26+// data: ...
27+// pass: ...
28+// error: ...
29+// }
30+// Creates a new validation message in a uniform way.
31+const newMessage = (data, pass, error) => ({
32+ // The input data
33+ data,
34+ // Has the data passed validation?
35+ pass,
36+ // If data has failed a validator, the error message goes here.
37+ error,
38+});
39+
40+// A validator that always passes.
41+export const kDefaultValidator = data => newMessage(data, true, '');
3142
3243// Validates a bip39 mnemonic
33−const validateMnemonic = m =>
44+export const validateMnemonic = m =>
3445 simpleValidatorWrapper({
3546 prevMessage: m,
3647 validator: d => bip39.validateMnemonic(d),
@@ -38,7 +49,7 @@ const validateMnemonic = m =>
3849 });
3950
4051// Checks an empty field
41−const validateNotEmpty = m =>
52+export const validateNotEmpty = m =>
4253 simpleValidatorWrapper({
4354 prevMessage: m,
4455 validator: d => d.length > 1,
@@ -46,7 +57,7 @@ const validateNotEmpty = m =>
4657 });
4758
4859// Checks if a patp is a valid galaxy
49−const validateGalaxy = m =>
60+export const validateGalaxy = m =>
5061 simpleValidatorWrapper({
5162 prevMessage: m,
5263 validator: d => {
@@ -61,7 +72,7 @@ const validateGalaxy = m =>
6172 errorMessage: 'This is not a valid galaxy',
6273 });
6374
64−const validatePoint = m =>
75+export const validatePoint = m =>
6576 simpleValidatorWrapper({
6677 prevMessage: m,
6778 validator: d => {
@@ -74,7 +85,7 @@ const validatePoint = m =>
7485 errorMessage: 'This is not a valid point',
7586 });
7687
77−const validateTicket = m =>
88+export const validateTicket = m =>
7889 simpleValidatorWrapper({
7990 prevMessage: m,
8091 validator: d => {
@@ -87,7 +98,7 @@ const validateTicket = m =>
8798 errorMessage: 'This is not a valid ticket',
8899 });
89100
90−const validateShard = m =>
101+export const validateShard = m =>
91102 simpleValidatorWrapper({
92103 prevMessage: m,
93104 validator: d => {
@@ -100,21 +111,21 @@ const validateShard = m =>
100111 errorMessage: 'This is not a valid shard',
101112 });
102113
103−const validateLength = (m, l) =>
114+export const validateLength = (m, l) =>
104115 simpleValidatorWrapper({
105116 prevMessage: m,
106117 validator: d => d.length === l,
107118 errorMessage: 'This is of an invalid length',
108119 });
109120
110−const validateNetworkKey = m =>
121+export const validateNetworkKey = m =>
111122 simpleValidatorWrapper({
112123 prevMessage: m,
113124 validator: d => hexRegExp.test(d),
114125 errorMessage: 'This is not a valid network key',
115126 });
116127
117−const validateNetworkSeed = m =>
128+export const validateNetworkSeed = m =>
118129 simpleValidatorWrapper({
119130 prevMessage: m,
120131 validator: d => hexRegExp.test(d),
@@ -122,55 +133,9 @@ const validateNetworkSeed = m =>
122133 });
123134
124135// Checks if a string is a valid ethereum address
125−const validateEthereumAddress = m =>
136+export const validateEthereumAddress = m =>
126137 simpleValidatorWrapper({
127138 prevMessage: m,
128139 validator: d => isValidAddress(d),
129140 errorMessage: 'This is not a valid Ethereum address',
130141 });
131−
132−// Wraps single validation functions in a controlled and predictable way.
133−const simpleValidatorWrapper = config => {
134− // If a previous validation has already failed, skip this validation and
135− // return the prev message to the next stage in the validation function chain.
136− // Failed validations should drop all the way down the chain and drop out of
137− // the output.
138− if (config.prevMessage.pass !== true) return config.prevMessage;
139−
140− // Run the validator and return the result.
141− return config.validator(config.prevMessage.data)
142− ? newMessage(config.prevMessage.data, true, '')
143− : newMessage(config.prevMessage.data, false, config.errorMessage);
144−};
145−
146−// Validation message
147−// {
148−// data: ...
149−// pass: ...
150−// error: ...
151−// }
152−// Creates a new validation message in a uniform way.
153−const newMessage = (data, pass, error) => ({
154− // The input data
155− data,
156− // Has the data passed validation?
157− pass,
158− // If data has failed a validator, the error message goes here.
159− error,
160−});
161−
162−export {
163− // validateBase64,
164− // validateJSON,
165− simpleValidatorWrapper,
166− validateMnemonic,
167− validateNotEmpty,
168− validateGalaxy,
169− validateEthereumAddress,
170− validatePoint,
171− validateTicket,
172− validateShard,
173− validateLength,
174− validateNetworkKey,
175− validateNetworkSeed,
176−};