Skip to content

chore: improve error message for faker.date.birthday #1843

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

Merged
merged 5 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 6 additions & 4 deletions src/modules/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,12 @@ export class DateModule {
refDate?: string | Date | number;
} = {}
): Date {
if (options.max < options.min) {
throw new FakerError(
`Max ${options.max} should be larger than or equal to min ${options.min}.`
);
}

const mode = options.mode === 'age' ? 'age' : 'year';
const refDate = toDate(options.refDate, this.faker.defaultRefDate);
const refYear = refDate.getUTCFullYear();
Expand All @@ -971,10 +977,6 @@ export class DateModule {
);
}

if (max < min) {
throw new FakerError(`Max ${max} should be larger then min ${min}.`);
}

return new Date(this.faker.number.int({ min, max }));
}
}
2 changes: 1 addition & 1 deletion test/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ describe('date', () => {
faker.date.birthdate({ min, max, mode: 'year' })
).toThrow(
new FakerError(
`Max 662515200000 should be larger then min 946771200000.`
`Max 1990 should be larger than or equal to min 2000.`
)
);
});
Expand Down