Giter VIP home page Giter VIP logo

ember-validators's Introduction

Ember Validators

Build Status npm version

A collection of EmberJS validators

ember install ember-validators

Helpful Links

Looking for help?

If it is a bug please open an issue on GitHub.

Usage

Validators can be individually imported and used as such

import validatePresence from 'ember-validators/presence';
import validateLength from 'ember-validators/length';

validatePresence('foo', { presence: true });
validateLength('foo', { min: 1 });

or via the validate method provided

import { validate } from 'ember-validators';

validate('presence', 'foo', { presence: true });
validate('length', 'foo', { min: 1 });

Validator Method Signature

Each validator has the following signature:

function (value, options, model, attribute) {}

Parameters:

  • value (Mixed):

    The value to validate

  • options (Object):

    A mutable object that holds validation specific options

  • model (Object):

    The model that is being validated

  • attribute (String):

    The attribute that is being validated

Returns:

  • Boolean

    true will be returned if the validation passed

  • Object

    Validation failed and a message should be built with the given attributes

    • type (String):

      The message type

    • value (Mixed):

      The value that was validated

    • context (Object):

      The error message context

    • message (String):

      The error message. If this is specified, use this string as the error message instead of building one.

ember-validators's People

Contributors

andreyfel avatar betocantu93 avatar bgentry avatar dependabot[bot] avatar ember-tomster avatar frykten avatar gilest avatar greatwizard avatar greenkeeperio-bot avatar herzzanu avatar jamess-m avatar jrjohnson avatar leizhao4 avatar offirgolan avatar pjfanning avatar sergeastapov avatar snewcomer avatar turbo87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ember-validators's Issues

Support moment object in date validator

Hi, it would be nice to support moment/computed property in date validator. I have a scenario where some field date must be after another field's date. I guess this example explains it pretty well.

expirationDate: [
  validator('date', {
    // model.createdDate is momentjs object
    after: computed.alias('model.createdDate'),
    precision: 'day',
    message: "Expiration date can't be before creation date.",
  })
]

Do you know how to make it work with current date validator? Otherwise, I can provide PR.

date validator: inconsistent meaning of allowBlank

The date validation has an option isBlank which is documented like so:

If true, skips validation if the value is empty

This accurately describes the implementation, which skips validation if isEmpty returns true for the value.

Other validators use allowBlank in combination with an isBlank check, and allowNone for an isEmpty check. This is inconsistent; blank should always mean blank, and empty/none should always mean empty.

There may be other validators with this problem, but I haven't checked.

Should this be fixed, and if so, how? One option is to deprecate the current allowBlank option in favor of allowNone, which would behave the same way. At the next major release, you could then add a real allowBlank option if desired.

0.1.2 has breaking changes

The recently released 0.1.2 of this package has breaking changes, replacing the packaged in require-module with ember-require-module.

This change breaks offirgolan/ember-cp-validations v2.7.2 which we use, since it's dependency is set to ~0.1.0 meaning it will pull in this version.

I suggest you read up on http://semver.org/, breaking changes should not be part of a patch release.

Nested `ds-error` validations don't work anymore

It looks like the ds-error validatior stopped working for nested errors. I assume this f829984 might have broken it.

The fix is most likely on this line f829984#diff-ca376f14c1cb0e5696506e00be4f0fa42e7212370c4b5e5bb8eef4970fdcdfceL25

In this case model[path] would work if the path is a simple key like errors. However, if the path is a nested key like foo.errors it won't work. We could fix this by doing model.get(path).

@snewcomer let me know if this is good for you and I can go ahead and create a PR.

[Bug] The inclusion validator validates "NaN" as a valid number...

Hi!

I'm pretty sure this was not meant to be as it wasn't tested as an accepted value:

  • with the inclusion validator, in a number-typed range, NaN is validated as true, whatever happens.

The issue is pretty simple when reading the code: https://github.com/offirgolan/ember-validators/blob/1568f472eb5d9851222b9944521ff1ba529641dd/addon/inclusion.js#L42

When checking their type, NaN and any number will return true as they are all numbers. And NaN is neither superior nor inferior to any number---since it has no value... So it will never return the validationError...

I'll file a PR for that, and I'll add a check to the tests suite.

Use of global Ember for Ember.String is deprecated

We're getting a deprecation warning in our CCI build/test runner:

Using Ember.String is deprecated. Please import methods directly from @ember/string. [deprecation id: ember-string.from-ember-module] This will be removed in ember-source 5.0.0.

The docs for this migration are here:
https://deprecations.emberjs.com/v4.x/#toc_ember-string-from-ember-module

The replacement fix is:
import { classify } from '@ember/string';

app/initializers/export-application-global.js

and

tests/unit/initializers/export-application-global-test.js

and the accompanying code line/syntax change.

[Bug] `date value is not finite in DateTimeFormat.format()` when passing locale `de` & specific `format`

Description

Passing a locale like de, it... with a specific format into before / onOrBefore/ after / onOrAfter validator is causing this error:
date value is not finite in DateTimeFormat.format().

This occures, because the parseAsDate is passing the locale to parseDate function, which returns in for locale en the date format 1.1.2015.

new Date('1.1.2015') returns a Invalid Date and this produces this error.

https://github.com/rwwagner90/ember-validators/blob/1282748147a691f3fcffacb0c478a676cc9e309a/addon/date.js#L141-L146

Fix:

There is not necessary to pass locale to parseAsDate, because parseAsDate returns always a new Date object.
We can always pass en-us in parseDate function.

Enviroment

Ember: v3.28.x / v4.x
Ember Validators: v4.1.2

Configurable date utility or native for date validators

As apps have more options available to them (luxon, date-fns) and moment-core recommends using luxon, we could make this configurable and fallback to browser native. We can also include some of the pitfalls of parsing strings with the Date constructor so ppl know if they might have any issues.

This would allow this validator to have broader use. I'd be happy to do the work.

ref poteto/ember-changeset-validations#208

URL validation incorrectly require a period in the authority

In the URL regex:

/(?:([A-Za-z]+):)?(\/{0,3})[a-zA-Z0-9][a-zA-Z-0-9]*(\.[\w-]+)+([\w.,@?^=%&:\/~+#-{}]*[\w@?^=%&\/~+#-{}])??/

the (\.[\w-]+)+ is incorrect as it requires at least one dot in the authority, which is not a requirement of valid URLs, and prevents, for example, using http://localhost which is a valid URL. I think maybe changing the \. to a \.? would fix it, but rather than hacking on the existing regex, it probably makes sense to go and find a regex or library that validates URLs and doesn't exhibit this bug.

Support embroider

ref #96 #92 This was a stepping stone to supporting Embroider. Our tests still fail, so we still have a step or two "somewhere" to fix, whether in this addon or in another library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.