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

replace exception thrown for invalid dates #1339

Merged
merged 1 commit into from
Apr 21, 2017
Merged

replace exception thrown for invalid dates #1339

merged 1 commit into from
Apr 21, 2017

Conversation

dhmlau
Copy link
Member

@dhmlau dhmlau commented Apr 21, 2017

Description

Originated from PR #718
There's no response from the user to grant me access to his fork, so I've created a separate PR. User @hotaru355 has signed CLA.

Excerpt from original PR:

Instantiating a model with an invalid date would not throw an exception, 
but calling isValid() on it would return false, without having to add the validation method explicitly.

2 tests are removed because they are no longer valid because no exceptions are thrown when instantiating the model.

  • should fail if field validation fails in manipuliation.test.js
  • throws an error when property of type Date is set to an invalid value in datatype.test.js

Related issues

#718

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style
    guide

@@ -426,6 +426,9 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
var requiredOptions = typeof prop.required === 'object' ? prop.required : undefined;
ModelClass.validatesPresenceOf(propertyName, requiredOptions);
}
if (DataType === Date) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do a one liner if statement here? if (DataType === Date) ModelClass.validatesDateOf(propertyName);

var d = new Date(arg);
if (isNaN(d.getTime())) {
throw new Error(g.f('Invalid date: %s', arg));
if (arg === null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. One liner if statement.

/*!
* Date validator
*/
function validateDate(attr, conf, err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the need of conf here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that conf is needed, otherwise the tests failed.
It's a required attribute for similar functions defined inside validators (in line397), even though it's not being used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhmlau I am not sure what you mean

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the validators are defined in the block around line 397. There are validatePresence, validateAbsence and also the newly added validateDate. If you go to those validate* functions, they all have conf. This is needed when those functions are being called.
In fact, without conf, tests fail because it expects the 2nd parameter in validate* function to be a function.

if (this[attr] === null || this[attr] === undefined) return;

var date = new Date(this[attr]);
if (isNaN(date.getTime())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One liner if statement please :)

}
var d = new Date(arg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd wrap this with a try catch block in case there is an error here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants