Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(text-validation) - pass only values that are strings #12

Merged
merged 12 commits into from
Jun 21, 2023
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"scripts": {
"build": "node scripts/build.js",
"test": "jest",
"test:watch": "jest --watchAll",
"lint": "eslint \"src/**/*.{js,ts}\"",
"format": "npm run format:prettier && npm run format:eslint",
"prepare": "husky install",
Expand Down
26 changes: 13 additions & 13 deletions src/tests/createHeadlessForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ describe('createHeadlessForm', () => {

const fieldValidator = fields[0].schema;
expect(fieldValidator.isValidSync('CI007')).toBe(true);
expect(fieldValidator.isValidSync(true)).toBe(true); // @BUG RMT-446 - cannot be a bool
expect(fieldValidator.isValidSync(1)).toBe(true); // @BUG RMT-446 - cannot be a number
expect(fieldValidator.isValidSync(0)).toBe(true); // @BUG RMT-446 - cannot be a number
expect(fieldValidator.isValidSync(true)).toBe(false);
expect(fieldValidator.isValidSync(1)).toBe(false);
expect(fieldValidator.isValidSync(0)).toBe(false);

expect(() => fieldValidator.validateSync('')).toThrowError('Required field');
});
Expand Down Expand Up @@ -2313,7 +2313,7 @@ describe('createHeadlessForm', () => {
validateForm({
validate_tabs: 'no',
a_fieldset: {
id_number: 123,
id_number: '123',
},
mandatory_group_array: 'no',
})
Expand All @@ -2328,7 +2328,7 @@ describe('createHeadlessForm', () => {
validateForm({
validate_tabs: 'yes',
a_fieldset: {
id_number: 123,
id_number: '123',
},
mandatory_group_array: 'no',
})
Expand All @@ -2344,7 +2344,7 @@ describe('createHeadlessForm', () => {
validateForm({
validate_tabs: 'yes',
a_fieldset: {
id_number: 123,
id_number: '123',
},
mandatory_group_array: 'yes',
a_group_array: [{ full_name: 'adfs' }],
Expand All @@ -2355,7 +2355,7 @@ describe('createHeadlessForm', () => {
validateForm({
validate_tabs: 'yes',
a_fieldset: {
id_number: 123,
id_number: '123',
tabs: 2,
},
mandatory_group_array: 'no',
Expand Down Expand Up @@ -2446,7 +2446,7 @@ describe('createHeadlessForm', () => {
validateForm({
validate_fieldset: ['id_number'],
a_fieldset: {
id_number: 123,
id_number: '123',
},
})
).toBeUndefined();
Expand All @@ -2455,7 +2455,7 @@ describe('createHeadlessForm', () => {
validateForm({
validate_fieldset: ['id_number', 'all'],
a_fieldset: {
id_number: 123,
id_number: '123',
},
})
).toEqual({
Expand All @@ -2468,7 +2468,7 @@ describe('createHeadlessForm', () => {
validateForm({
validate_fieldset: ['id_number', 'all'],
a_fieldset: {
id_number: 123,
id_number: '123',
tabs: 2,
},
})
Expand All @@ -2483,7 +2483,7 @@ describe('createHeadlessForm', () => {
validateForm({
validate_fieldset: ['id_number'],
a_fieldset: {
id_number: 123,
id_number: '123',
},
})
).toBeUndefined();
Expand All @@ -2492,7 +2492,7 @@ describe('createHeadlessForm', () => {
validateForm({
validate_fieldset: ['id_number', 'all'],
a_fieldset: {
id_number: 123,
id_number: '123',
},
})
).toEqual({
Expand All @@ -2505,7 +2505,7 @@ describe('createHeadlessForm', () => {
validateForm({
validate_fieldset: ['id_number', 'all'],
a_fieldset: {
id_number: 123,
id_number: '123',
tabs: 2,
},
})
Expand Down
9 changes: 8 additions & 1 deletion src/yupSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ export const baseString = string().trim();
const todayDateHint = new Date().toISOString().substring(0, 10);
const convertBytesToKB = convertDiskSizeFromTo('Bytes', 'KB');
const convertKbBytesToMB = convertDiskSizeFromTo('KB', 'MB');
const useOnlyStrings = (value, originalValue) => {
if (typeof originalValue === 'string') {
return originalValue;
}

return null;
};

const yupSchemas = {
text: string().trim().nullable(),
text: string().transform(useOnlyStrings).trim().nullable(),
gabrielseco marked this conversation as resolved.
Show resolved Hide resolved
select: string().trim().nullable(),
radio: string().trim().nullable(),
date: string()
Expand Down