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

Date validator addition #208

Closed
wants to merge 9 commits into from
Closed

Date validator addition #208

wants to merge 9 commits into from

Conversation

jathanasiou
Copy link
Contributor

@jathanasiou jathanasiou commented Dec 2, 2019

Closes #204 .

Changes proposed in this pull request

Adds date validator, along with documentation and unit testing of corresponding options.

Moment shim had to be included in order for the validator and tests to run.

Copy link
Collaborator

@snewcomer snewcomer left a comment

Choose a reason for hiding this comment

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

Perfect! Love all the tests. I'm putting this upstream issue as sort of a blocker. The changes required would be quite small.

adopted-ember-addons/ember-validators#69

@@ -0,0 +1,265 @@
import moment from 'moment';
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can/should avoid moment and use native JS functions or date-fns if last resort.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used it in the unit tests since the dependency was needed anyway so might as well use the closest thing possible to the validator implementation. If ember-validators does remove moment it would indeed be better to work without it in tests.

Copy link
Collaborator

Choose a reason for hiding this comment

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

For the extremely simple cases ember-validators uses it for, it definitely should be optional and use the platform if so. This test would still be useful as the API would look like:

validator(key, 'Nov || 3rd || 1998', { dateFn: moment }) or something like that

);
assert.equal(validator(key, 'Nov || 3rd || 1998'), true);
});

Copy link
Collaborator

Choose a reason for hiding this comment

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

wow. Phenomenal tests!

};
}

export {errorFormat};
Copy link
Collaborator

Choose a reason for hiding this comment

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

export { errorFormat }

const errorFormat = "MMM Do, YYYY";

export default function validateNumber(options = {}) {
options = withDefaults(options, { allowBlank: false, errorFormat: errorFormat});
Copy link
Collaborator

Choose a reason for hiding this comment

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

... errorFormat: errorFormat }

@snewcomer
Copy link
Collaborator

Another thought is we just re-implement the date validator ourselves if that issue goes unaddressed for some reason.

@snewcomer
Copy link
Collaborator

snewcomer commented Dec 10, 2019

I have a feeling we need to roll our own and avoid use of ember-validators for now. We can upstream a PR once this lands. Is that something you might be up for doing? Or would you like me to spike it out? To start, I think we can use just plain comparison operators on native Date constructs. What do you think @jathanasiou?

@jathanasiou
Copy link
Contributor Author

jathanasiou commented Dec 10, 2019

I think we can use just plain comparison operators on native Date constructs. What do you think

I have the impression that working with vanilla Date objects would be a bit painful (and possibly make the code rather bulky or unreadable) so I don't think I would go for that.

Is that something you might be up for doing?

Implementing date validations from scratch sounds a bit demanding and I don't have plenty of time right now, though I am coincidentally working with Dayjs in another project and it seems pretty small and useful for both operations and testing/comparisons.

I can try giving it a go this weekend if that sounds good to you and can let you if I have any progress.

@snewcomer
Copy link
Collaborator

That sounds good! If we do go the route of requiring a library, tree shakeability or immutability would be important points!

Just to note, if we are limited <, >, <=, >= (as shown in the link below), then we can construct date objects and compare (they are converted to unix epoch seconds when using these I believe). Moreover, since they are relative and don't require timezone, we should be set with just JS.

https://github.com/offirgolan/ember-validators/blob/master/addon/date.js

@snewcomer
Copy link
Collaborator

snewcomer commented Jan 21, 2020

@jathanasiou Would this be something you would like me to tackle? I got the time ;)

@jathanasiou
Copy link
Contributor Author

@snewcomer Apologies for the delay. I have other tasks on my plate right now and cannot currently commit to this. If you'd like to do the rest of the work and to refactor out moment feel free to :).

@snewcomer snewcomer mentioned this pull request Jan 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Date validator request
2 participants