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

Improve date validation #506

Merged
merged 4 commits into from
Dec 3, 2024
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
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
Loading