Skip to content

Commit

Permalink
feat(Validator): server-side validation
Browse files Browse the repository at this point in the history
fixes #398
  • Loading branch information
jdanyow committed Jan 19, 2017
1 parent 47f4f8b commit 1b701ab
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = function(config) {

// list of files to exclude
exclude: [
'dist/test/test/server.js',
],


Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"devDependencies": {
"aurelia-bootstrapper": "^2.0.1",
"aurelia-pal-browser": "^1.1.0",
"aurelia-pal-nodejs": "^1.0.0-beta.1.0.0",
"aurelia-polyfills": "^1.1.1",
"aurelia-testing": "^1.0.0-beta.2.0.1",
"concurrently": "^3.1.0",
Expand Down
12 changes: 7 additions & 5 deletions src/aurelia-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class AureliaValidationConfiguration {
* Configures the plugin.
*/
export function configure(
frameworkConfig: { container: Container, globalResources: (...resources: string[]) => any },
frameworkConfig: { container: Container, globalResources?: (...resources: string[]) => any },
callback?: (config: AureliaValidationConfiguration) => void
) {
// the fluent rule definition API needs the parser to translate messages
Expand All @@ -70,8 +70,10 @@ export function configure(
config.apply(frameworkConfig.container);

// globalize the behaviors.
frameworkConfig.globalResources(
'./validate-binding-behavior',
'./validation-errors-custom-attribute',
'./validation-renderer-custom-attribute');
if (frameworkConfig.globalResources) {
frameworkConfig.globalResources(
'./validate-binding-behavior',
'./validation-errors-custom-attribute',
'./validation-renderer-custom-attribute');
}
}
3 changes: 2 additions & 1 deletion src/validation-errors-custom-attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { customAttribute, bindable } from 'aurelia-templating';
import { ValidationController } from './validation-controller';
import { ValidateResult } from './validate-result';
import { ValidationRenderer, RenderInstruction } from './validation-renderer';
import { DOM } from 'aurelia-pal';

export interface RenderedError {
error: ValidateResult;
Expand All @@ -12,7 +13,7 @@ export interface RenderedError {

@customAttribute('validation-errors')
export class ValidationErrorsCustomAttribute implements ValidationRenderer {
public static inject = [Element, Lazy.of(ValidationController)];
public static inject = [DOM.Element, Lazy.of(ValidationController)];

@bindable({ defaultBindingMode: bindingMode.oneWay })
public controller: ValidationController | null = null;
Expand Down
45 changes: 45 additions & 0 deletions test/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'aurelia-polyfills';
import {initialize} from 'aurelia-pal-nodejs';
import {Container} from 'aurelia-dependency-injection';
import {configure as configureBindingLanguage} from 'aurelia-templating-binding';
import {
configure as configureValidation,
ValidationRules,
Validator
} from '../src/aurelia-validation';
import * as assert from 'assert';

initialize();
const container = new Container();
configureBindingLanguage({ container });
configureValidation({ container });

const rules = ValidationRules
.ensure('firstName').required()
.ensure('lastName').required()
.rules;

const validator: Validator = container.get(Validator);

validator.validateObject({ firstName: '', lastName: 'test'}, rules)
.then(result => {
assert(result.length === 2);
assert(result[0].propertyName === 'firstName');
assert(result[0].valid === false);
assert(result[1].propertyName === 'lastName');
assert(result[1].valid === true);
});

validator.validateProperty({ firstName: '', lastName: 'test'}, 'firstName', rules)
.then(result => {
assert(result.length === 1);
assert(result[0].propertyName === 'firstName');
assert(result[0].valid === false);
});

validator.validateProperty({ firstName: '', lastName: 'test'}, 'lastName', rules)
.then(result => {
assert(result.length === 1);
assert(result[0].propertyName === 'lastName');
assert(result[0].valid === true);
});

0 comments on commit 1b701ab

Please sign in to comment.