Skip to content

Commit

Permalink
Merge pull request #160 from formio/FIO-9086-time-component-default-v…
Browse files Browse the repository at this point in the history
…alue-validation

FIO-9086: use for validation only dataFormat (data storage format)
  • Loading branch information
brendanbond authored Oct 15, 2024
2 parents 1a100f3 + 2c3ab48 commit cabaa43
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/process/validation/__tests__/Validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ it('Validator will throw the correct errors given a flat components array', asyn
email: 'brendanb',
url: 'htpigoogle',
inputMask: 'hello, world',
time: ['12:00:00', '11:00'], // one of the values is provided in incorrect format (format instead dataFormat)
submit: false,
};
for (let component of simpleForm.components) {
Expand All @@ -34,5 +35,5 @@ it('Validator will throw the correct errors given a flat components array', asyn
errors = [...errors, ...scope.errors.map((error) => error.errorKeyOrMessage)];
}
}
expect(errors).to.have.length(6);
expect(errors).to.have.length(7);
});
11 changes: 10 additions & 1 deletion src/process/validation/__tests__/fixtures/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2290,14 +2290,23 @@ export const simpleForm = {
id: 'e8vls3a',
keyModified: true,
},
{
label: 'Time',
type: 'time',
key: 'time',
input: true,
dataFormat: 'HH:mm:ss',
format: 'HH:mm',
multiple: true,
},
{
type: 'button',
label: 'Submit',
key: 'submit',
disableOnInvalid: true,
input: true,
tableView: false,
},
}
],
settings: {},
properties: {},
Expand Down
3 changes: 2 additions & 1 deletion src/process/validation/rules/__tests__/validateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const timeField: TimeComponent = {
key: 'time',
label: 'Time',
input: true,
dataFormat: 'HH:mm:ss'
dataFormat: 'HH:mm:ss',
format: 'HH:mm'
};

it('Should validate a time component with a valid time value', async () => {
Expand Down
5 changes: 1 addition & 4 deletions src/process/validation/rules/validateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ export const validateTimeSync: RuleFnSync = (context: ValidationContext) => {
}
try {
if (!value || isComponentDataEmpty(component, data, path)) return null;
// Server side evaluations of validity should use the "dataFormat" vs the "format" which is used on the client.
const format = config?.server ?
((component as TimeComponent).dataFormat || 'HH:mm:ss') :
((component as TimeComponent).format || 'HH:mm');
const format = (component as TimeComponent).dataFormat || 'HH:mm:ss';
const isValid = dayjs(String(value), format, true).isValid();
return isValid ? null : new FieldError('time', context);
}
Expand Down

0 comments on commit cabaa43

Please sign in to comment.