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

fix: make sure dates are correctly validated & parsed #639

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 35 additions & 0 deletions src/packages/router/route/params/test/parameter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,41 @@ describe('module "router/route/params"', () => {
});
});

describe('- type "date"', () => {
[true, false].forEach(required => {
describe(`- ${required ? 'required' : 'optional'}`, () => {
let value;

beforeEach(() => {
value = new Date();
subject = new Parameter({
required,
type: 'date',
path: 'meta.test'
});
});

if (required) {
it('fails when the value is null', () => {
expect(() => subject.validate(null)).to.throw(TypeError);
});
} else {
it('passes when the value is null', () => {
expect(subject.validate(null)).to.be.null;
});
}

it('fails when there is a type mismatch', () => {
expect(() => subject.validate('test')).to.throw(TypeError);
});

it('returns the value when the type and value match', () => {
expect(subject.validate(value)).to.equal(value);
});
});
});
});

primitives.forEach(({ type, valid, falsy, invalid }) => {
describe(`- type "${type}"`, () => {
[true, false].forEach(required => {
Expand Down
4 changes: 4 additions & 0 deletions src/packages/router/route/params/utils/validate-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export default function validateType(
isValid = isObject(value) || isNull(value);
break;

case 'date':
isValid = value instanceof Date;
break;

default:
isValid = type === valueType;
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/transform-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export function transformKeys<T: Object | Array<mixed>>(
const recurse = deep
&& value
&& typeof value === 'object'
&& !Array.isArray(value);
&& !Array.isArray(value)
&& !(value instanceof Date);

if (recurse) {
return {
Expand Down