Skip to content

Commit

Permalink
Merge pull request #506 from kipr/improve-date-validation
Browse files Browse the repository at this point in the history
Improve date validation
  • Loading branch information
tcorbly authored Dec 3, 2024
2 parents 9b9a5af + 1b23b61 commit 0525c51
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simulator",
"version": "2.0.1",
"version": "2.0.2",
"sideEffects": false,
"main": "dist/index.js",
"author": "KISS Institute for Practical Robotics",
Expand Down
18 changes: 16 additions & 2 deletions src/components/interface/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,26 @@ namespace Form {
}

export const IDENTITY_FINALIZER = (value: string) => value;
export const DATE_FINALIZER = (value: string) => {
let valueParts: string[] = [];
const validDelimters = ['/', '-'];
for (const delimiter of validDelimters) {
valueParts = value.split(delimiter);
if (valueParts.length === 3) {
break;
}
}

const [mm, dd, yyyy] = valueParts;
return `${mm}/${dd}/${yyyy}`;
};

export const EMAIL_VALIDATOR = (value: string) => Validators.validate(value, Validators.Types.Email);
export const PASSWORD_VALIDATOR = (value: string) => Validators.validatePassword(value);
export const DATE_VALIDATOR = (value: string) => Validators.validate(value, Validators.Types.Date);
export const NON_EMPTY_VALIDATOR = (value: string) => Validators.validate(value, Validators.Types.Length, 1);


export const email = (id: string, text: string, tooltip?: string, assist?: () => void, assistText?: string): Item<string> => ({
id,
text,
Expand All @@ -262,7 +276,7 @@ namespace Form {
text,
tooltip,
validator: DATE_VALIDATOR,
finalizer: IDENTITY_FINALIZER,
finalizer: DATE_FINALIZER,
assist,
assistText,
});
Expand Down
17 changes: 14 additions & 3 deletions src/util/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,20 @@ export namespace Validators {
return value.length >= length;
case Types.Email:
return /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value);
case Types.Date:
// TODO: validate that it's a real date (e.g. 02/30/2000 should not pass)
return /^(0[1-9]|1[012])(\/|-)(0[1-9]|[12][0-9]|3[01])(\/|-)(19|20)\d{2}$/.test(value);
case Types.Date: {
// Ensure it matches the expected format
const regexRes = /^(0[1-9]|1[012])(?:\/|-)(0[1-9]|[12][0-9]|3[01])(?:\/|-)((?:19|20)\d{2})$/.exec(value);
if (regexRes === null) return false;

// Ensure it's a valid date
// Date constructor will rollover invalid dates, like 2024-02-30 to 2024-03-01, so ensure the individual parts didn't change
const [m, d, y] = regexRes.slice(1).map(s => parseInt(s));
const dateObj = new Date(Date.UTC(y, m - 1, d));
return !isNaN(dateObj.getTime()) &&
dateObj.getUTCFullYear() === y &&
(dateObj.getUTCMonth() + 1) === m &&
dateObj.getUTCDate() === d;
}
default:
return false;
}
Expand Down

0 comments on commit 0525c51

Please sign in to comment.