Skip to content

Commit

Permalink
fix: make sure dates are correctly validated & parsed (#639)
Browse files Browse the repository at this point in the history
* fix: make sure dates are correctly validated
fix: make sure transform-keys doesn't recurse into a Date object

* Check date last

* Add test case
  • Loading branch information
nickschot authored and zacharygolba committed Jan 17, 2017
1 parent 00273c8 commit b7c66cb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
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

0 comments on commit b7c66cb

Please sign in to comment.