Skip to content
Permalink
Browse files

feat(config): support global config option for default validation tri…

…gger
  • Loading branch information
RomkeVdMeulen committed Oct 18, 2019
1 parent d3a2998 commit 39a4e679513e3fa716b46cedce24931b3b57028f
@@ -1,5 +1,6 @@
// Exports

export * from './config';
export * from './controller-validate-result';
export * from './get-target-dom-element';
export * from './property-info';
@@ -26,8 +27,7 @@ export * from './implementation/validation-rules';
// Configuration

import { Container } from 'aurelia-dependency-injection';
import { Validator } from './validator';
import { StandardValidator } from './implementation/standard-validator';
import { AureliaValidationConfiguration } from './config';
import { ValidationMessageParser } from './implementation/validation-message-parser';
import { PropertyAccessorParser } from './property-accessor-parser';
import { ValidationRules } from './implementation/validation-rules';
@@ -41,28 +41,6 @@ import {
import { ValidationErrorsCustomAttribute } from './validation-errors-custom-attribute';
import { ValidationRendererCustomAttribute } from './validation-renderer-custom-attribute';

/**
* Aurelia Validation Configuration API
*/
export class AureliaValidationConfiguration {
private validatorType: { new (...args: any[]): Validator } = StandardValidator;

/**
* Use a custom Validator implementation.
*/
public customValidator(type: { new (...args: any[]): Validator }) {
this.validatorType = type;
}

/**
* Applies the configuration.
*/
public apply(container: Container) {
const validator = container.get(this.validatorType);
container.registerInstance(Validator, validator);
}
}

/**
* Configures the plugin.
*/
@@ -0,0 +1,36 @@
import { Container } from 'aurelia-dependency-injection';
import { Validator } from './validator';
import { StandardValidator } from './implementation/standard-validator';
import { validateTrigger } from './validate-trigger';

/**
* Aurelia Validation Configuration API
*/
export class AureliaValidationConfiguration {
private validatorType: { new (...args: any[]): Validator } = StandardValidator;
private validationTrigger = validateTrigger.blur;

/**
* Use a custom Validator implementation.
*/
public customValidator(type: { new (...args: any[]): Validator }) {
this.validatorType = type;
}

public defaultValidationTrigger(trigger: validateTrigger) {
this.validationTrigger = trigger;
}

public getDefaultValidationTrigger() {
return this.validationTrigger;
}

/**
* Applies the configuration.
*/
public apply(container: Container) {
const validator = container.get(this.validatorType);
container.registerInstance(Validator, validator);
container.registerInstance(AureliaValidationConfiguration, this);
}
}
@@ -1,4 +1,5 @@
import { Container } from 'aurelia-dependency-injection';
import { AureliaValidationConfiguration } from './config';
import { ValidationController } from './validation-controller';
import { Validator } from './validator';
import { PropertyAccessorParser } from './property-accessor-parser';
@@ -21,7 +22,8 @@ export class ValidationControllerFactory {
validator = this.container.get(Validator) as Validator;
}
const propertyParser = this.container.get(PropertyAccessorParser) as PropertyAccessorParser;
return new ValidationController(validator, propertyParser);
const config = this.container.get(AureliaValidationConfiguration) as AureliaValidationConfiguration;
return new ValidationController(validator, propertyParser, config);
}

/**
@@ -1,4 +1,5 @@
import { Binding, Expression } from 'aurelia-binding';
import { AureliaValidationConfiguration } from './config';
import { Validator } from './validator';
import { validateTrigger } from './validate-trigger';
import { getPropertyInfo } from './property-info';
@@ -15,7 +16,7 @@ import { ValidateEvent } from './validate-event';
* Exposes the current list of validation results for binding purposes.
*/
export class ValidationController {
public static inject = [Validator, PropertyAccessorParser];
public static inject = [Validator, PropertyAccessorParser, AureliaValidationConfiguration];

// Registered bindings (via the validate binding behavior)
private bindings = new Map<Binding, BindingInfo>();
@@ -47,14 +48,20 @@ export class ValidationController {
/**
* The trigger that will invoke automatic validation of a property used in a binding.
*/
public validateTrigger = validateTrigger.blur;
public validateTrigger: validateTrigger;

// Promise that resolves when validation has completed.
private finishValidating: Promise<any> = Promise.resolve();

private eventCallbacks: ((event: ValidateEvent) => void)[] = [];

constructor(private validator: Validator, private propertyParser: PropertyAccessorParser) { }
constructor(
private validator: Validator,
private propertyParser: PropertyAccessorParser,
config: AureliaValidationConfiguration,
) {
this.validateTrigger = config.getDefaultValidationTrigger();
}

/**
* Subscribe to controller validate and reset events. These events occur when the
@@ -1,22 +1,28 @@
import { Container, Optional } from 'aurelia-dependency-injection';
import {
ValidationControllerFactory,
ValidationController,
Validator
} from '../src/aurelia-validation';

describe('ValidationControllerFactory', () => {
it('createForCurrentScope', () => {
const container = new Container();
const standardValidator = {};
container.registerInstance(Validator, standardValidator);
const childContainer = container.createChild();
const factory = childContainer.get(ValidationControllerFactory);
const controller = factory.createForCurrentScope();
expect(controller.validator).toBe(standardValidator);
expect(container.get(Optional.of(ValidationController))).toBe(null);
expect(childContainer.get(Optional.of(ValidationController))).toBe(controller);
const customValidator = {};
expect(factory.createForCurrentScope(customValidator).validator).toBe(customValidator);
});
});
import { Container, Optional } from 'aurelia-dependency-injection';
import {
AureliaValidationConfiguration,
ValidationControllerFactory,
ValidationController,
Validator,
validateTrigger
} from '../src/aurelia-validation';

describe('ValidationControllerFactory', () => {
it('createForCurrentScope', () => {
const container = new Container();
const standardValidator = {};
container.registerInstance(Validator, standardValidator);
const config = new AureliaValidationConfiguration();
config.defaultValidationTrigger(validateTrigger.manual);
container.registerInstance(AureliaValidationConfiguration, config);
const childContainer = container.createChild();
const factory = childContainer.get(ValidationControllerFactory);
const controller = factory.createForCurrentScope();
expect(controller.validator).toBe(standardValidator);
expect(controller.validateTrigger).toBe(validateTrigger.manual);
expect(container.get(Optional.of(ValidationController))).toBe(null);
expect(childContainer.get(Optional.of(ValidationController))).toBe(controller);
const customValidator = {};
expect(factory.createForCurrentScope(customValidator).validator).toBe(customValidator);
});
});

0 comments on commit 39a4e67

Please sign in to comment.
You can’t perform that action at this time.