|
|
|
|
@@ -6,10 +6,6 @@ const maxLengthMessage = (length: number) => `This value must be shorter than th
|
|
|
|
|
const requiredMessage = 'This field is required.';
|
|
|
|
|
let options: ValidateOptions<any, any, any> = {
|
|
|
|
|
operation: 'create',
|
|
|
|
|
field: {
|
|
|
|
|
type: 'text',
|
|
|
|
|
name: 'text',
|
|
|
|
|
},
|
|
|
|
|
data: undefined,
|
|
|
|
|
siblingData: undefined,
|
|
|
|
|
};
|
|
|
|
|
@@ -23,7 +19,7 @@ describe('Field Validations', () => {
|
|
|
|
|
});
|
|
|
|
|
it('should show required message', () => {
|
|
|
|
|
const val = undefined;
|
|
|
|
|
const result = text(val, { ...options, field: { ...options.field, required: true } });
|
|
|
|
|
const result = text(val, { ...options, required: true });
|
|
|
|
|
expect(result).toBe(requiredMessage);
|
|
|
|
|
});
|
|
|
|
|
it('should handle undefined', () => {
|
|
|
|
|
@@ -33,22 +29,22 @@ describe('Field Validations', () => {
|
|
|
|
|
});
|
|
|
|
|
it('should validate maxLength', () => {
|
|
|
|
|
const val = 'toolong';
|
|
|
|
|
const result = text(val, { ...options, field: { ...options.field, maxLength: 5 } });
|
|
|
|
|
const result = text(val, { ...options, maxLength: 5 });
|
|
|
|
|
expect(result).toBe(maxLengthMessage(5));
|
|
|
|
|
});
|
|
|
|
|
it('should validate minLength', () => {
|
|
|
|
|
const val = 'short';
|
|
|
|
|
const result = text(val, { ...options, field: { ...options.field, minLength: 10 } });
|
|
|
|
|
const result = text(val, { ...options, minLength: 10 });
|
|
|
|
|
expect(result).toBe(minLengthMessage(10));
|
|
|
|
|
});
|
|
|
|
|
it('should validate maxLength with no value', () => {
|
|
|
|
|
const val = undefined;
|
|
|
|
|
const result = text(val, { ...options, field: { ...options.field, maxLength: 5 } });
|
|
|
|
|
const result = text(val, { ...options, maxLength: 5 });
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
it('should validate minLength with no value', () => {
|
|
|
|
|
const val = undefined;
|
|
|
|
|
const result = text(val, { ...options, field: { ...options.field, minLength: 10 } });
|
|
|
|
|
const result = text(val, { ...options, minLength: 10 });
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
@@ -62,7 +58,7 @@ describe('Field Validations', () => {
|
|
|
|
|
});
|
|
|
|
|
it('should show required message', () => {
|
|
|
|
|
const val = undefined;
|
|
|
|
|
const result = textarea(val, { ...options, field: { ...options.field, required: true } });
|
|
|
|
|
const result = textarea(val, { ...options, required: true });
|
|
|
|
|
expect(result).toBe(requiredMessage);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@@ -73,29 +69,30 @@ describe('Field Validations', () => {
|
|
|
|
|
});
|
|
|
|
|
it('should validate maxLength', () => {
|
|
|
|
|
const val = 'toolong';
|
|
|
|
|
const result = textarea(val, { ...options, field: { ...options.field, maxLength: 5 } });
|
|
|
|
|
const result = textarea(val, { ...options, maxLength: 5 });
|
|
|
|
|
expect(result).toBe(maxLengthMessage(5));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should validate minLength', () => {
|
|
|
|
|
const val = 'short';
|
|
|
|
|
const result = textarea(val, { ...options, field: { ...options.field, minLength: 10 } });
|
|
|
|
|
const result = textarea(val, { ...options, minLength: 10 });
|
|
|
|
|
expect(result).toBe(minLengthMessage(10));
|
|
|
|
|
});
|
|
|
|
|
it('should validate maxLength with no value', () => {
|
|
|
|
|
const val = undefined;
|
|
|
|
|
const result = textarea(val, { ...options, field: { ...options.field, maxLength: 5 } });
|
|
|
|
|
const result = textarea(val, { ...options, maxLength: 5 });
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
it('should validate minLength with no value', () => {
|
|
|
|
|
const val = undefined;
|
|
|
|
|
const result = textarea(val, { ...options, field: { ...options.field, minLength: 10 } });
|
|
|
|
|
const result = textarea(val, { ...options, minLength: 10 });
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('password', () => {
|
|
|
|
|
options.field = { type: 'password', name: 'test' };
|
|
|
|
|
options.type = 'password';
|
|
|
|
|
options.name = 'test';
|
|
|
|
|
it('should validate', () => {
|
|
|
|
|
const val = 'test';
|
|
|
|
|
const result = password(val, options);
|
|
|
|
|
@@ -103,7 +100,7 @@ describe('Field Validations', () => {
|
|
|
|
|
});
|
|
|
|
|
it('should show required message', () => {
|
|
|
|
|
const val = undefined;
|
|
|
|
|
const result = password(val, { ...options, field: { ...options.field, required: true } });
|
|
|
|
|
const result = password(val, { ...options, required: true });
|
|
|
|
|
expect(result).toBe(requiredMessage);
|
|
|
|
|
});
|
|
|
|
|
it('should handle undefined', () => {
|
|
|
|
|
@@ -113,60 +110,52 @@ describe('Field Validations', () => {
|
|
|
|
|
});
|
|
|
|
|
it('should validate maxLength', () => {
|
|
|
|
|
const val = 'toolong';
|
|
|
|
|
const result = password(val, { ...options, field: { ...options.field, maxLength: 5 } });
|
|
|
|
|
const result = password(val, { ...options, maxLength: 5 });
|
|
|
|
|
expect(result).toBe(maxLengthMessage(5));
|
|
|
|
|
});
|
|
|
|
|
it('should validate minLength', () => {
|
|
|
|
|
const val = 'short';
|
|
|
|
|
const result = password(val, { ...options, field: { ...options.field, minLength: 10 } });
|
|
|
|
|
const result = password(val, { ...options, minLength: 10 });
|
|
|
|
|
expect(result).toBe(minLengthMessage(10));
|
|
|
|
|
});
|
|
|
|
|
it('should validate maxLength with no value', () => {
|
|
|
|
|
const val = undefined;
|
|
|
|
|
const result = password(val, { ...options, field: { ...options.field, maxLength: 5 } });
|
|
|
|
|
const result = password(val, { ...options, maxLength: 5 });
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
it('should validate minLength with no value', () => {
|
|
|
|
|
const val = undefined;
|
|
|
|
|
const result = password(val, { ...options, field: { ...options.field, minLength: 10 } });
|
|
|
|
|
const result = password(val, { ...options, minLength: 10 });
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('select', () => {
|
|
|
|
|
options.field = {
|
|
|
|
|
type: 'select',
|
|
|
|
|
options: ['one', 'two', 'three'],
|
|
|
|
|
};
|
|
|
|
|
options.type = 'select';
|
|
|
|
|
options.options = ['one', 'two', 'three'];
|
|
|
|
|
const optionsRequired = {
|
|
|
|
|
...options,
|
|
|
|
|
field: {
|
|
|
|
|
...options.field,
|
|
|
|
|
required: true,
|
|
|
|
|
options: [{
|
|
|
|
|
value: 'one',
|
|
|
|
|
label: 'One',
|
|
|
|
|
}, {
|
|
|
|
|
value: 'two',
|
|
|
|
|
label: 'two',
|
|
|
|
|
}, {
|
|
|
|
|
value: 'three',
|
|
|
|
|
label: 'three',
|
|
|
|
|
}],
|
|
|
|
|
},
|
|
|
|
|
required: true,
|
|
|
|
|
options: [{
|
|
|
|
|
value: 'one',
|
|
|
|
|
label: 'One',
|
|
|
|
|
}, {
|
|
|
|
|
value: 'two',
|
|
|
|
|
label: 'two',
|
|
|
|
|
}, {
|
|
|
|
|
value: 'three',
|
|
|
|
|
label: 'three',
|
|
|
|
|
}],
|
|
|
|
|
};
|
|
|
|
|
const optionsWithEmptyString = {
|
|
|
|
|
...options,
|
|
|
|
|
field: {
|
|
|
|
|
...options.field,
|
|
|
|
|
options: [{
|
|
|
|
|
value: '',
|
|
|
|
|
label: 'None',
|
|
|
|
|
}, {
|
|
|
|
|
value: 'option',
|
|
|
|
|
label: 'Option',
|
|
|
|
|
}],
|
|
|
|
|
},
|
|
|
|
|
options: [{
|
|
|
|
|
value: '',
|
|
|
|
|
label: 'None',
|
|
|
|
|
}, {
|
|
|
|
|
value: 'option',
|
|
|
|
|
label: 'Option',
|
|
|
|
|
}],
|
|
|
|
|
};
|
|
|
|
|
it('should allow valid input', () => {
|
|
|
|
|
const val = 'one';
|
|
|
|
|
@@ -204,35 +193,35 @@ describe('Field Validations', () => {
|
|
|
|
|
});
|
|
|
|
|
it('should prevent undefined input with required and hasMany', () => {
|
|
|
|
|
let val;
|
|
|
|
|
options.field.hasMany = true;
|
|
|
|
|
options.hasMany = true;
|
|
|
|
|
const result = select(val, optionsRequired);
|
|
|
|
|
expect(result).not.toStrictEqual(true);
|
|
|
|
|
});
|
|
|
|
|
it('should prevent empty array input with required and hasMany', () => {
|
|
|
|
|
optionsRequired.field.hasMany = true;
|
|
|
|
|
optionsRequired.hasMany = true;
|
|
|
|
|
const result = select([], optionsRequired);
|
|
|
|
|
expect(result).not.toStrictEqual(true);
|
|
|
|
|
});
|
|
|
|
|
it('should prevent empty string array input with required and hasMany', () => {
|
|
|
|
|
options.field.hasMany = true;
|
|
|
|
|
options.hasMany = true;
|
|
|
|
|
const result = select([''], optionsRequired);
|
|
|
|
|
expect(result).not.toStrictEqual(true);
|
|
|
|
|
});
|
|
|
|
|
it('should prevent null input with required and hasMany', () => {
|
|
|
|
|
const val = null;
|
|
|
|
|
options.field.hasMany = true;
|
|
|
|
|
options.hasMany = true;
|
|
|
|
|
const result = select(val, optionsRequired);
|
|
|
|
|
expect(result).not.toStrictEqual(true);
|
|
|
|
|
});
|
|
|
|
|
it('should allow valid input with option objects', () => {
|
|
|
|
|
const val = 'one';
|
|
|
|
|
options.field.hasMany = false;
|
|
|
|
|
options.hasMany = false;
|
|
|
|
|
const result = select(val, optionsRequired);
|
|
|
|
|
expect(result).toStrictEqual(true);
|
|
|
|
|
});
|
|
|
|
|
it('should prevent invalid input with option objects', () => {
|
|
|
|
|
const val = 'bad';
|
|
|
|
|
options.field.hasMany = false;
|
|
|
|
|
options.hasMany = false;
|
|
|
|
|
const result = select(val, optionsRequired);
|
|
|
|
|
expect(result).not.toStrictEqual(true);
|
|
|
|
|
});
|
|
|
|
|
@@ -243,7 +232,7 @@ describe('Field Validations', () => {
|
|
|
|
|
});
|
|
|
|
|
it('should allow empty string input with option object and required', () => {
|
|
|
|
|
const val = '';
|
|
|
|
|
optionsWithEmptyString.field.required = true;
|
|
|
|
|
optionsWithEmptyString.required = true;
|
|
|
|
|
const result = select(val, optionsWithEmptyString);
|
|
|
|
|
expect(result).toStrictEqual(true);
|
|
|
|
|
});
|
|
|
|
|
@@ -259,13 +248,13 @@ describe('Field Validations', () => {
|
|
|
|
|
});
|
|
|
|
|
it('should allow valid input with hasMany option objects', () => {
|
|
|
|
|
const val = ['one', 'three'];
|
|
|
|
|
optionsRequired.field.hasMany = true;
|
|
|
|
|
optionsRequired.hasMany = true;
|
|
|
|
|
const result = select(val, optionsRequired);
|
|
|
|
|
expect(result).toStrictEqual(true);
|
|
|
|
|
});
|
|
|
|
|
it('should prevent invalid input with hasMany option objects', () => {
|
|
|
|
|
const val = ['three', 'bad'];
|
|
|
|
|
optionsRequired.field.hasMany = true;
|
|
|
|
|
optionsRequired.hasMany = true;
|
|
|
|
|
const result = select(val, optionsRequired);
|
|
|
|
|
expect(result).not.toStrictEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|