Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions schemas/get-birth-certificate.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,7 @@
"name": "dateOfBirth",
"type": "date",
"label": "Date of birth",
"required": false,
"validations": {
"regex": "^\\d{4}-\\d{2}-\\d{2}$",
"message": "Date of birth is required and must be in YYYY-MM-DD format"
}
"required": false
},
{
"name": "placeOfBirth",
Expand Down
6 changes: 1 addition & 5 deletions schemas/get-death-certificate.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,7 @@
"name": "dateOfDeath",
"type": "date",
"label": "Date of death",
"required": false,
"validations": {
"regex": "^\\d{4}-\\d{2}-\\d{2}$",
"message": "Date of death is required and must be in YYYY-MM-DD format"
}
"required": false
},
{
"name": "estimatedDateOfDeath",
Expand Down
43 changes: 33 additions & 10 deletions src/validation/schema-builder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,23 @@ export class SchemaBuilderService {
schema = z.boolean();
break;
case 'date':
schema = z
.string()
.regex(
/^\d{4}-\d{2}-\d{2}$/,
'Invalid date format (expected YYYY-MM-DD)',
);
// For optional date fields, allow empty strings or valid dates
if (!field.required) {
schema = z
.string()
.refine(
(val) => !val || /^\d{4}-\d{2}-\d{2}$/.test(val),
'Invalid date format (expected YYYY-MM-DD)',
);
} else {
// For required date fields, enforce the regex
schema = z
.string()
.regex(
/^\d{4}-\d{2}-\d{2}$/,
'Invalid date format (expected YYYY-MM-DD)',
);
}
break;
case 'select':
schema = z.string();
Expand Down Expand Up @@ -126,10 +137,22 @@ export class SchemaBuilderService {
(validations.min !== undefined || validations.max !== undefined)
) {
if (validations.min !== undefined) {
schema = schema.min(
validations.min,
validations.message || `Minimum length is ${validations.min}`,
);
const minLength = validations.min;
if (!required) {
// For optional fields, allow empty strings or strings that meet min length
schema = schema.refine(
(val: string) => !val || val.length >= minLength,
{
message:
validations.message || `Minimum length is ${minLength}`,
},
);
} else {
schema = schema.min(
minLength,
validations.message || `Minimum length is ${minLength}`,
);
}
}
if (validations.max !== undefined) {
schema = schema.max(
Expand Down