{"id":15060309,"url":"https://github.com/iamdtang/ember-changeset-conditional-validations","last_synced_at":"2026-05-16T06:13:24.978Z","repository":{"id":23934912,"uuid":"100202228","full_name":"iamdtang/ember-changeset-conditional-validations","owner":"iamdtang","description":"Conditional validations for ember-changeset-validations","archived":false,"fork":false,"pushed_at":"2022-12-08T08:54:55.000Z","size":991,"stargazers_count":27,"open_issues_count":24,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T07:04:09.635Z","etag":null,"topics":["ember","ember-addon","ember-changeset","emberjs","emberjs-addon","validation"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/ember-changeset-conditional-validations","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iamdtang.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-13T20:16:22.000Z","updated_at":"2024-05-30T02:44:41.000Z","dependencies_parsed_at":"2023-01-14T00:06:27.765Z","dependency_job_id":null,"html_url":"https://github.com/iamdtang/ember-changeset-conditional-validations","commit_stats":null,"previous_names":["skaterdav85/ember-changeset-conditional-validations"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamdtang%2Fember-changeset-conditional-validations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamdtang%2Fember-changeset-conditional-validations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamdtang%2Fember-changeset-conditional-validations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamdtang%2Fember-changeset-conditional-validations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamdtang","download_url":"https://codeload.github.com/iamdtang/ember-changeset-conditional-validations/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248166926,"owners_count":21058480,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ember","ember-addon","ember-changeset","emberjs","emberjs-addon","validation"],"created_at":"2024-09-24T22:56:41.376Z","updated_at":"2026-05-16T06:13:24.934Z","avatar_url":"https://github.com/iamdtang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/skaterdav85/ember-changeset-conditional-validations.svg?branch=master)](https://travis-ci.org/skaterdav85/ember-changeset-conditional-validations) [![Ember Observer Score](https://emberobserver.com/badges/ember-changeset-conditional-validations.svg)](https://emberobserver.com/addons/ember-changeset-conditional-validations)\n\n# ember-changeset-conditional-validations\n\nAn extra validator for conditional validations with [`ember-changeset-validations`](https://github.com/DockYard/ember-changeset-validations).\n\n## Installation\n\n```\nember install ember-changeset-conditional-validations\n```\n\n## Basic Usage\n\nLet'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.\n\n```js\nimport { validatePresence, validateLength } from 'ember-changeset-validations/validators';\nimport validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';\n\nexport default {\n  creditCardNumber: validateSometimes([\n    validatePresence(true),\n    validateLength({ is: 16 })\n  ], function(changes, content) {\n    return this.get('paymentMethod.isCreditCard');\n  })\n};\n```\n\n`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()`.\n\n```js\nimport Changeset from 'ember-changeset';\nimport lookupValidator from 'ember-changeset-validations';\nimport Validations from './../validations/settings';\n\nlet settings = {};\nlet changeset = new Changeset(settings, lookupValidator(Validations), Validations);\n\nconsole.log(changeset.get('isValid')); // true\nchangeset.set('paymentMethod', {\n  isCreditCard: true\n});\nchangeset.validate();\nconsole.log(changeset.get('isValid')); // false\nconsole.log(changeset.get('errors')); // [{key: 'creditCardNumber', validation: ['Credit card number can't be blank', 'Credit card number must be a number']}]\nchangeset.set('creditCardNumber', '1234567890123456');\nchangeset.validate();\nconsole.log(changeset.get('isValid')); // true\nchangeset.set('creditCardNumber', '1234');\nchangeset.validate();\nconsole.log(changeset.get('isValid')); // false\nconsole.log(changeset.get('errors')); // [{key: 'creditCardNumber', value: '1234', validation: ['Credit card number must be equal to 16']}]\nchangeset.set('paymentMethod', {\n  isCreditCard: false\n});\nchangeset.validate();\nconsole.log(changeset.get('isValid')); // true\n```\n\n### Combining Validations with Conditional Validations\n\nYou 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:\n\n```js\nimport { validateNumber } from 'ember-changeset-validations/validators';\nimport validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';\n\nexport default {\n  someProperty: [\n    validateNumber({ integer: true }),\n    validateSometimes(validateNumber({ gt: 5 }), function() {\n      // condition\n    })\n  ]\n};\n```\n\nLet'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:\n\n```js\nimport { validateNumber } from 'ember-changeset-validations/validators';\nimport validateSometimes from 'ember-changeset-conditional-validations/validators/sometimes';\n\nexport default {\n  someProperty: [\n    validateNumber({ integer: true }),\n    ...validateSometimes([\n      validateNumber({ gt: 5 }),\n      validateNumber({ lt: 10 })\n    ], function() {\n      // condition\n    })\n  ]\n};\n```\n\n## Installation\n\n* `git clone \u003crepository-url\u003e` this repository\n* `cd ember-changeset-conditional-validations`\n* `npm install`\n\n## Running\n\n* `ember serve`\n* Visit your app at [http://localhost:4200](http://localhost:4200).\n\n## Running Tests\n\n* `npm test` (Runs `ember try:each` to test your addon against multiple Ember versions)\n* `ember test`\n* `ember test --server`\n\n## Building\n\n* `ember build`\n\nFor more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamdtang%2Fember-changeset-conditional-validations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamdtang%2Fember-changeset-conditional-validations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamdtang%2Fember-changeset-conditional-validations/lists"}