Permalink
Browse files

feat(validation): move to aurelia-validation 0.12+

BREAKING CHANGE: Update to aurelia-validation@^0.12.3 See the changed documenation on validation of usage
  • Loading branch information...
doktordirk committed Sep 8, 2016
1 parent cc67188 commit e211ffbbc8c23d513174713e7b5dd391ee313634
@@ -35,15 +35,15 @@ The [changelog](doc/changelog.md) provides you with information about important
## Uses
* [aurelia-api](https://www.npmjs.com/package/aurelia-api)
* `aurelia-validation@0.6.6`
* `aurelia-validation@^0.12.3`
## Used by
* [aurelia-datatable](https://www.npmjs.com/package/aurelia-datatable).
## Installation
Aurelia-orm needs an installation of [aurelia-api](https://www.npmjs.com/package/aurelia-api) and `aurelia-validation@0.6.6`.
Aurelia-orm needs an installation of [aurelia-api](https://www.npmjs.com/package/aurelia-api) and `aurelia-validation@^0.12.3`.
### Aureli-Cli
@@ -81,9 +81,9 @@ And add following to the `bundles.dist.aurelia.includes` section of `build/bundl
```js
"get-prop",
"aurelia-datatable",
"[aurelia-datatable/**/*.js]",
"aurelia-datatable/**/*.html!text",
"aurelia-orm",
"[aurelia-orm/**/*.js]",
"aurelia-orm/**/*.html!text",
```
If the installation results in having forks, try resolving them by running:
@@ -111,18 +111,21 @@ Here's a snippet to give you an idea of what this module does.
```javascript
import {Entity, validatedResource} from 'aurelia-orm';
import {ensure} from 'aurelia-validation';
import {ValidationRules} from 'aurelia-validation';
@validatedResource('user')
export class UserEntity extends Entity {
@ensure(it => it.isNotEmpty().containsOnlyAlpha().hasLengthBetween(3, 20))
username = null;
@ensure(it => it.isNotEmpty().isStrongPassword())
email = null;
password = null;
@ensure(it => it.isNotEmpty().isEmail())
email = null;
constructor() {
super();
ValidationRules
.ensure('email').required().email()
.ensure('password').required().minLength(8).maxLength(20)
.on(this);
}
}
```
@@ -144,14 +147,22 @@ export class Create {
create () {
this.requestInFlight = true;
this.entity.save()
.then(result => {
this.entity.validate()
.then(validationErrors => {
if (validationErrors.length !== 0) {
console.error(error);
throw validationErrors[0];
}
return this.entity.save();
}).then(result => {
this.requestInFlight = false;
console.log('User created successfully');
})
.catch(error => {
console.error(error);
this.requestInFlight = false;
// notify of the error?
});
}
}
@@ -28,7 +28,7 @@
"aurelia-logging": "^1.0.0-rc.1.0.0",
"aurelia-metadata": "^1.0.0-rc.1.0.0",
"aurelia-templating": "^1.0.0-rc.1.0.0",
"aurelia-validation": "0.6.6",
"aurelia-validation": "^0.12.3",
"get-prop": "^0.0.10",
"typer": "^1.1.0"
}
109 config.js
@@ -7,66 +7,147 @@ System.config({
},
map: {
"aurelia-api": "npm:aurelia-api@3.0.0-rc8",
"aurelia-binding": "npm:aurelia-binding@1.0.1",
"aurelia-api": "npm:aurelia-api@3.0.0-rc9",
"aurelia-binding": "npm:aurelia-binding@1.0.4",
"aurelia-bootstrapper": "npm:aurelia-bootstrapper@1.0.0",
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal-browser": "npm:aurelia-pal-browser@1.0.0",
"aurelia-polyfills": "npm:aurelia-polyfills@1.0.0",
"aurelia-templating": "npm:aurelia-templating@1.0.0",
"aurelia-validation": "npm:aurelia-validation@0.6.6",
"aurelia-polyfills": "npm:aurelia-polyfills@1.1.0",
"aurelia-templating-binding": "npm:aurelia-templating-binding@1.0.0",
"aurelia-validation": "npm:aurelia-validation@0.12.3",
"fetch": "github:github/fetch@1.0.0",
"get-prop": "npm:get-prop@0.0.10",
"typer": "npm:typer@1.1.0",
"npm:aurelia-api@3.0.0-rc8": {
"npm:aurelia-api@3.0.0-rc9": {
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-fetch-client": "npm:aurelia-fetch-client@1.0.0",
"aurelia-framework": "npm:aurelia-framework@1.0.2",
"aurelia-path": "npm:aurelia-path@1.0.0",
"extend": "npm:extend@3.0.0"
},
"npm:aurelia-binding@1.0.1": {
"npm:aurelia-binding@1.0.4": {
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0",
"aurelia-task-queue": "npm:aurelia-task-queue@1.0.0"
},
"npm:aurelia-bootstrapper@1.0.0": {
"aurelia-event-aggregator": "npm:aurelia-event-aggregator@1.0.0",
"aurelia-framework": "npm:aurelia-framework@1.0.2",
"aurelia-history": "npm:aurelia-history@1.0.0",
"aurelia-history-browser": "npm:aurelia-history-browser@1.0.0",
"aurelia-loader-default": "npm:aurelia-loader-default@1.0.0",
"aurelia-logging-console": "npm:aurelia-logging-console@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0",
"aurelia-pal-browser": "npm:aurelia-pal-browser@1.0.0",
"aurelia-polyfills": "npm:aurelia-polyfills@1.1.0",
"aurelia-router": "npm:aurelia-router@1.0.2",
"aurelia-templating": "npm:aurelia-templating@1.1.0",
"aurelia-templating-binding": "npm:aurelia-templating-binding@1.0.0",
"aurelia-templating-resources": "npm:aurelia-templating-resources@1.0.0",
"aurelia-templating-router": "npm:aurelia-templating-router@1.0.0"
},
"npm:aurelia-dependency-injection@1.0.0": {
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-event-aggregator@1.0.0": {
"aurelia-logging": "npm:aurelia-logging@1.0.0"
},
"npm:aurelia-framework@1.0.2": {
"aurelia-binding": "npm:aurelia-binding@1.0.4",
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-loader": "npm:aurelia-loader@1.0.0",
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0",
"aurelia-path": "npm:aurelia-path@1.0.0",
"aurelia-task-queue": "npm:aurelia-task-queue@1.0.0",
"aurelia-templating": "npm:aurelia-templating@1.1.0"
},
"npm:aurelia-history-browser@1.0.0": {
"aurelia-history": "npm:aurelia-history@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-loader-default@1.0.0": {
"aurelia-loader": "npm:aurelia-loader@1.0.0",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-loader@1.0.0": {
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-path": "npm:aurelia-path@1.0.0"
},
"npm:aurelia-logging-console@1.0.0": {
"aurelia-logging": "npm:aurelia-logging@1.0.0"
},
"npm:aurelia-metadata@1.0.0": {
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-pal-browser@1.0.0": {
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-polyfills@1.0.0": {
"npm:aurelia-polyfills@1.1.0": {
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-route-recognizer@1.0.0": {
"aurelia-path": "npm:aurelia-path@1.0.0"
},
"npm:aurelia-router@1.0.2": {
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-event-aggregator": "npm:aurelia-event-aggregator@1.0.0",
"aurelia-history": "npm:aurelia-history@1.0.0",
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-path": "npm:aurelia-path@1.0.0",
"aurelia-route-recognizer": "npm:aurelia-route-recognizer@1.0.0"
},
"npm:aurelia-task-queue@1.0.0": {
"aurelia-pal": "npm:aurelia-pal@1.0.0"
},
"npm:aurelia-templating@1.0.0": {
"aurelia-binding": "npm:aurelia-binding@1.0.1",
"npm:aurelia-templating-binding@1.0.0": {
"aurelia-binding": "npm:aurelia-binding@1.0.4",
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-templating": "npm:aurelia-templating@1.1.0"
},
"npm:aurelia-templating-resources@1.0.0": {
"aurelia-binding": "npm:aurelia-binding@1.0.4",
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-loader": "npm:aurelia-loader@1.0.0",
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0",
"aurelia-path": "npm:aurelia-path@1.0.0",
"aurelia-task-queue": "npm:aurelia-task-queue@1.0.0"
"aurelia-task-queue": "npm:aurelia-task-queue@1.0.0",
"aurelia-templating": "npm:aurelia-templating@1.1.0"
},
"npm:aurelia-templating-router@1.0.0": {
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0",
"aurelia-path": "npm:aurelia-path@1.0.0",
"aurelia-router": "npm:aurelia-router@1.0.2",
"aurelia-templating": "npm:aurelia-templating@1.1.0"
},
"npm:aurelia-validation@0.6.6": {
"aurelia-binding": "npm:aurelia-binding@1.0.1",
"npm:aurelia-templating@1.1.0": {
"aurelia-binding": "npm:aurelia-binding@1.0.4",
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-loader": "npm:aurelia-loader@1.0.0",
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0",
"aurelia-templating": "npm:aurelia-templating@1.0.0"
"aurelia-pal": "npm:aurelia-pal@1.0.0",
"aurelia-path": "npm:aurelia-path@1.0.0",
"aurelia-task-queue": "npm:aurelia-task-queue@1.0.0"
},
"npm:aurelia-validation@0.12.3": {
"aurelia-binding": "npm:aurelia-binding@1.0.4",
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0",
"aurelia-logging": "npm:aurelia-logging@1.0.0",
"aurelia-pal": "npm:aurelia-pal@1.0.0",
"aurelia-task-queue": "npm:aurelia-task-queue@1.0.0",
"aurelia-templating": "npm:aurelia-templating@1.1.0"
}
}
});
@@ -212,21 +212,55 @@ Check if entity has validation enabled.
### Returns
A boolean indicating if validation is enabled or not.
A boolean indicating if validation is enabled or not. This will enable the (Standard)Validator on the entity.
---------
## .getValidation()
## .getValidator()
When enabled, this method returns the validation instance.
When enabled, this method returns the validator instance.
### Parameters
* None
### Returns
An instance of [aurelia-validation](https://github.com/aurelia/validation).
An instance of the validator. See [aurelia-validation](http://aurelia.io/hub.html#/doc/article/aurelia/validation).
---------
## .setValidator(validator)
Set the validator. Per default the StandardValidator is used if validation is enabled, but you can set a custom validator if needed.
### Parameters
| Parameter | Type | Description |
| --------- | --------------- | ---------------------------------------- |
| validator | Validator | An instance of a Validator implemenation |
### Returns
Itself
---------
## .validate(property?, rules?)
Calls the validator on the entity. Per default all properties and all rules on the entity are tested, but one can optionally select the property and/or pass a rule to be tested.
If validation is not enabled on the entity, it is considered valid.
### Parameters
| Parameter | Type | Description |
| --------- | --------------- | ------------------------------------ |
| property | string | (optional) Tje property to be tested |
| rules | ValidationRules | (optional) The rules ro test |
### Returns
Promise<[ValidationError]>
---------
@@ -19,6 +19,9 @@ export function configure(aurelia) {
.standardConfiguration()
.developmentLogging()
// Initialize validation first, if you want to use it
// .plugin('aurelia-validation')
// Register the plugin, and register your entities
.plugin('aurelia-orm', builder => {
builder.registerEntities(entities);
@@ -187,17 +187,24 @@ export class Weather extends Entity {}
### Bonus: validation
Aurelia-orm extends aurelia-validate, and adds validation for your associations.
Aurelia-orm supports aurelia-validation, and adds validation for your associations.
To add validation for associations, simply use the .hasAssociation() rule like so:
```js
import {ensure} from 'aurelia-validation';
import {ValidationRules} from 'aurelia-validation';
import {association, validatedResource, Entity} from 'aurelia-orm';
@validatedResource()
export class SomeEntity extends Entity {
@association('manufacturer')
@ensure(it => it.hasAssociation())
manufacturer = null;
constructor() {
super();
ValidationRules
.ensure('manufacturer').satisfiesRule('hasAssociation')
.on(this);
}
}
```
@@ -52,20 +52,27 @@ There's an easier way to do this, as described in the chapter [Configuration](co
Here's an example that showcases (almost) all possibilities for your entity:
```js
import {ensure} from 'aurelia-validation';
import {ValidationRules} from 'aurelia-validation';
import {Entity, type, association, validatedResource} from 'aurelia-orm';
@validatedResource('product')
export class Product extends Entity {
@type('string')
@ensure(it => it.isNotEmpty().containsOnlyAlpha().hasLengthBetween(3, 20))
name = null;
@association('category')
category = null;
@association({collection: 'media'})
media = [];
constructor() {
super();
ValidationRules
.ensure('name').required().minLength(8).maxLength(20)
.on(this);
}
}
```
@@ -38,9 +38,9 @@ And add following to the `bundles.dist.aurelia.includes` section of `build/bundl
```js
"get-prop",
"aurelia-datatable",
"[aurelia-datatable/**/*.js]",
"aurelia-datatable/**/*.html!text",
"aurelia-orm",
"[aurelia-orm/**/*.js]",
"aurelia-orm/**/*.html!text",
```
If the installation results in having forks, try resolving them by running:
Oops, something went wrong.

0 comments on commit e211ffb

Please sign in to comment.