redux-form-validation
[arvados-workbench2.git] / src / validators / max-length.tsx
diff --git a/src/validators/max-length.tsx b/src/validators/max-length.tsx
new file mode 100644 (file)
index 0000000..1f8e509
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+export const ERROR_MESSAGE = 'Maximum string length of this field is: ';
+export const DEFAULT_MAX_VALUE = 60;
+
+interface MaxLengthProps {
+    maxLengthValue: number;  
+    defaultErrorMessage: string;
+}
+
+// TODO types for maxLength
+const maxLength: any = (maxLengthValue = DEFAULT_MAX_VALUE, errorMessage = ERROR_MESSAGE) => {
+    return (value: string) => {
+        if (value) {
+            return  value && value && value.length <= maxLengthValue ? undefined : `${errorMessage || ERROR_MESSAGE} ${maxLengthValue}`;
+        }
+
+        return undefined;
+    };
+};
+
+export default maxLength;
\ No newline at end of file