Skip to content

Commit

Permalink
feat(config): support global config option for default validation tri…
Browse files Browse the repository at this point in the history
…gger
  • Loading branch information
RomkeVdMeulen committed Oct 18, 2019
1 parent d3a2998 commit 39a4e67
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 50 deletions.
26 changes: 2 additions & 24 deletions src/aurelia-validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Exports

export * from './config';
export * from './controller-validate-result';
export * from './get-target-dom-element';
export * from './property-info';
Expand All @@ -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';
Expand All @@ -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.
*/
Expand Down
36 changes: 36 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 3 additions & 1 deletion src/validation-controller-factory.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/validation-controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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>();
Expand Down Expand Up @@ -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
Expand Down
50 changes: 28 additions & 22 deletions test/validation-controller-factory.ts
Original file line number Diff line number Diff line change
@@ -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.