fix: textarea handle undefined

This commit is contained in:
Elliot DeNolf
2021-01-17 21:40:07 -05:00
parent c12d4757a4
commit ba31397ac1
4 changed files with 56 additions and 8 deletions

View File

@@ -0,0 +1,34 @@
import { textarea } from './validations';
describe('Field Validations', () => {
describe('textarea', () => {
it('should validate', () => {
const val = 'test';
const result = textarea(val);
expect(result).toBe(true);
});
it('should show default message when required and not present', () => {
const val = undefined;
const result = textarea(val, { required: true });
expect(result).toBe('This field is required.');
});
it('should handle undefined', () => {
const val = undefined;
const result = textarea(val);
expect(result).toBe(true);
});
it('should validate maxLength', () => {
const val = 'toolong';
const result = textarea(val, { maxLength: 5 });
expect(result).toBe('This value must be shorter than the max length of 5 characters.');
});
it('should validate minLength', () => {
const val = 'short';
const result = textarea(val, { minLength: 10 });
expect(result).toBe('This value must be longer than the minimum length of 10 characters.');
});
});
});

View File

@@ -26,16 +26,16 @@ export const number: Validate = (value: string, options = {}) => {
};
export const text: Validate = (value: string, options = {}) => {
if (options.maxLength && (value && value.length > options.maxLength)) {
if (value && options.maxLength && value.length > options.maxLength) {
return `This value must be shorter than the max length of ${options.maxLength} characters.`;
}
if (options.minLength && (value && value.length < options.minLength)) {
if (value && options.minLength && value?.length < options.minLength) {
return `This value must be longer than the minimum length of ${options.minLength} characters.`;
}
if (options.required) {
if (typeof value !== 'string' || (typeof value === 'string' && value.length === 0)) {
if (typeof value !== 'string' || (typeof value === 'string' && value?.length === 0)) {
return defaultMessage;
}
}
@@ -44,11 +44,11 @@ export const text: Validate = (value: string, options = {}) => {
};
export const password: Validate = (value: string, options = {}) => {
if (options.maxLength && value.length > options.maxLength) {
if (value && options.maxLength && value.length > options.maxLength) {
return `This value must be shorter than the max length of ${options.maxLength} characters.`;
}
if (options.minLength && value.length < options.minLength) {
if (value && options.minLength && value.length < options.minLength) {
return `This value must be longer than the minimum length of ${options.minLength} characters.`;
}
@@ -69,11 +69,11 @@ export const email: Validate = (value: string, options = {}) => {
};
export const textarea: Validate = (value: string, options = {}) => {
if (options.maxLength && value.length > options.maxLength) {
if (value && options.maxLength && value.length > options.maxLength) {
return `This value must be shorter than the max length of ${options.maxLength} characters.`;
}
if (options.minLength && value.length < options.minLength) {
if (value && options.minLength && value.length < options.minLength) {
return `This value must be longer than the minimum length of ${options.minLength} characters.`;
}