on master help sign in
feat: implement maximum point length
commit9e648227e58227069402869cf99cc183fbbde689 authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateTue 9 Jul 2019, 11:32 (7 years ago) parentf7f7c0c
3 files changed+47−14
src/components/Inputs.js+6−1 src/lib/lib.js+6 src/lib/validators.js+35−13
src/components/Inputs.js+6−1
@@ -15,6 +15,7 @@ import {
1515 validateLength,
1616 validateHexString,
1717 validateOneOf,
18+ validateMaximumPatpByteLength,
1819} from 'lib/validators';
1920import { prependSig } from 'lib/transformers';
2021
@@ -167,7 +168,11 @@ export function useTicketInput({ validators = [], deriving = false, ...rest }) {
167168 );
168169}
169170
170−const kPointValidators = [validatePoint, validateNotEmpty];
171+const kPointValidators = [
172+ validatePoint,
173+ validateMaximumPatpByteLength(4),
174+ validateNotEmpty,
175+];
171176const kPointTransformers = [prependSig];
172177export function usePointInput(rest) {
173178 const [lastValidPoint, setLastValidPoint] = useState('');
src/lib/lib.js+6
@@ -34,6 +34,11 @@ const randomPatq = len => {
3434 return ob.hex2patq(hex);
3535};
3636
37+// TODO: is there a function for this calculation in urbit-ob or similar?
38+// 3 characters per byte, plus a tilde and hypen prefix per pair of bytes
39+const patpStringLength = byteLength =>
40+ byteLength * 3 + Math.ceil(byteLength / 2);
41+
3742export {
3843 compose,
3944 allFalse,
@@ -44,4 +49,5 @@ export {
4449 fill,
4550 isValidGalaxy,
4651 randomPatq,
52+ patpStringLength,
4753};
src/lib/validators.js+35−13
@@ -4,6 +4,7 @@ import { identity, includes } from 'lodash';
44
55import { isValidAddress } from './wallet';
66import patp2dec from './patp2dec';
7+import { patpStringLength } from './lib';
78
89// NOTE: do not use the /g modifier for these regexes
910// https://stackoverflow.com/a/21373261
@@ -136,19 +137,6 @@ export const validateShard = m =>
136137 error: 'This is not a valid shard',
137138 });
138139
139−export const validateLength = l => m =>
140− simpleValidatorWrapper({
141− prevMessage: m,
142− validator: d => {
143− try {
144− return d.length === l;
145− } catch {
146− return false;
147− }
148− },
149− error: `Must be exactly ${l} characters`,
150− });
151−
152140export const validateOneOf = (options = []) => m =>
153141 simpleValidatorWrapper({
154142 prevMessage: m,
@@ -215,3 +203,37 @@ export const validateExactly = (value, error) => m =>
215203 validator: d => d === value,
216204 error,
217205 });
206+
207+export const validateLength = l => m =>
208+ simpleValidatorWrapper({
209+ prevMessage: m,
210+ validator: d => {
211+ try {
212+ return d.length === l;
213+ } catch {
214+ return false;
215+ }
216+ },
217+ error: `Must be exactly ${l} characters`,
218+ });
219+
220+export const validateMaximumLength = l => m =>
221+ simpleValidatorWrapper({
222+ prevMessage: m,
223+ validator: d => {
224+ try {
225+ return d.length <= l;
226+ } catch {
227+ return false;
228+ }
229+ },
230+ error: `Must be ${l} characters or fewer.`,
231+ });
232+
233+export const validatePatpByteLength = byteLength => {
234+ return validateLength(patpStringLength(byteLength));
235+};
236+
237+export const validateMaximumPatpByteLength = byteLength => {
238+ return validateMaximumLength(patpStringLength(byteLength));
239+};