New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ember-changeset-conditional-validations

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-changeset-conditional-validations

Conditional validations for ember-changeset-validations

  • 0.6.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
843
increased by26.77%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status Ember Observer Score

ember-changeset-conditional-validations

An extra validator for conditional validations with ember-changeset-validations.

Installation

ember install ember-changeset-conditional-validations

Basic Usage

Let's say you want to validate a user's settings. Only if the payment method is a credit card should the credit card number validations be applied.

import { validatePresence, validateLength } from 'ember-changeset-validations/validators';
import validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';

export default {
  creditCardNumber: validateSometimes([
    validatePresence(true),
    validateLength({ is: 16 })
  ], function(changes, content) {
    return this.get('paymentMethod.isCreditCard');
  })
};

validateSometimes() takes 2 arguments. The first is a validator or an array of validators you want applied to the attribute. The second argument is a callback function which represents the condition. If the condition callback returns true, the rules will be added. This callback function will be invoked with the changeset's changes and content. The callback will also be invoked with its this value set to an object that has a get() method for accessing a property. this.get(property) first proxies to the changes and then the underlying content, and has the same semantics as Ember.get().

import Changeset from 'ember-changeset';
import lookupValidator from 'ember-changeset-validations';
import Validations from './../validations/settings';

let settings = {};
let changeset = new Changeset(settings, lookupValidator(Validations), Validations);

console.log(changeset.get('isValid')); // true
changeset.set('paymentMethod', {
  isCreditCard: true
});
changeset.validate();
console.log(changeset.get('isValid')); // false
console.log(changeset.get('errors')); // [{key: 'creditCardNumber', validation: ['Credit card number can't be blank', 'Credit card number must be a number']}]
changeset.set('creditCardNumber', '1234567890123456');
changeset.validate();
console.log(changeset.get('isValid')); // true
changeset.set('creditCardNumber', '1234');
changeset.validate();
console.log(changeset.get('isValid')); // false
console.log(changeset.get('errors')); // [{key: 'creditCardNumber', value: '1234', validation: ['Credit card number must be equal to 16']}]
changeset.set('paymentMethod', {
  isCreditCard: false
});
changeset.validate();
console.log(changeset.get('isValid')); // true

Combining Validations with Conditional Validations

You can also have a combination of validations that will always run and conditional validations. For example, say you wanted to validate that a property is a number, but conditionally validate that the number is greater than 5. You could do something like the following:

import { validateNumber } from 'ember-changeset-validations/validators';
import validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';

export default {
  someProperty: [
    validateNumber({ integer: true }),
    validateSometimes(validateNumber({ gt: 5 }), function() {
      // condition
    })
  ]
};

Let's say in the previous example that you also wanted to conditionally validate that the number is less than 10. You could do something like the following:

import { validateNumber } from 'ember-changeset-validations/validators';
import validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';

export default {
  someProperty: [
    validateNumber({ integer: true }),
    ...validateSometimes([
      validateNumber({ gt: 5 }),
      validateNumber({ lt: 10 })
    ], function() {
      // condition
    })
  ]
};

Installation

  • git clone <repository-url> this repository
  • cd ember-changeset-conditional-validations
  • npm install

Running

Running Tests

  • npm test (Runs ember try:each to test your addon against multiple Ember versions)
  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit https://ember-cli.com/.

Keywords

FAQs

Package last updated on 27 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc