922e3e52706093929f04a44641e3276187cdbf78
[arvados-workbench2.git] / src / validators / max-length.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 export const ERROR_MESSAGE = 'Maximum string length of this field is: ';
6 export const DEFAULT_MAX_VALUE = 60;
7
8 interface MaxLengthProps {
9     maxLengthValue: number;  
10     defaultErrorMessage: string;
11 }
12
13 // TODO types for maxLength
14 export const maxLength: any = (maxLengthValue = DEFAULT_MAX_VALUE, errorMessage = ERROR_MESSAGE) => {
15     return (value: string) => {
16         if (value) {
17             return  value && value.length <= maxLengthValue ? undefined : `${errorMessage || ERROR_MESSAGE} ${maxLengthValue}`;
18         }
19
20         return undefined;
21     };
22 };