Skip to content
Permalink
Browse files

feat(config): make config constructor param optional and place defaul…

…t trigger in static property
  • Loading branch information
RomkeVdMeulen committed Nov 1, 2019
1 parent 39a4e67 commit a52f4c403bb68ed895fd0b7a3ea217c67ce22e2d
Showing with 29 additions and 3 deletions.
  1. +3 −1 src/config.ts
  2. +4 −2 src/validation-controller.ts
  3. +22 −0 test/validation-controller.ts
@@ -7,8 +7,10 @@ import { validateTrigger } from './validate-trigger';
* Aurelia Validation Configuration API
*/
export class AureliaValidationConfiguration {
public static DEFAULT_VALIDATION_TRIGGER = validateTrigger.blur;

private validatorType: { new (...args: any[]): Validator } = StandardValidator;
private validationTrigger = validateTrigger.blur;
private validationTrigger = AureliaValidationConfiguration.DEFAULT_VALIDATION_TRIGGER;

/**
* Use a custom Validator implementation.
@@ -58,9 +58,11 @@ export class ValidationController {
constructor(
private validator: Validator,
private propertyParser: PropertyAccessorParser,
config: AureliaValidationConfiguration,
config?: AureliaValidationConfiguration,
) {
this.validateTrigger = config.getDefaultValidationTrigger();
this.validateTrigger = config instanceof AureliaValidationConfiguration
? config.getDefaultValidationTrigger()
: AureliaValidationConfiguration.DEFAULT_VALIDATION_TRIGGER;
}

/**
@@ -0,0 +1,22 @@
import {
AureliaValidationConfiguration,
PropertyAccessorParser,
ValidationController,
Validator,
validateTrigger,
} from '../src/aurelia-validation';

describe('ValidationController', () => {
it('takes a validator, a PropertyAccessorParser, and optional config', () => {
const validator = {} as any as Validator;
const parser = {} as any as PropertyAccessorParser;
const controller = new ValidationController(validator, parser);
expect(controller.validateTrigger).toBe(AureliaValidationConfiguration.DEFAULT_VALIDATION_TRIGGER);

const trigger = validateTrigger.changeOrBlur;
const config = new AureliaValidationConfiguration();
config.defaultValidationTrigger(trigger);
const controllerWithConfig = new ValidationController(validator, parser, config);
expect(controllerWithConfig.validateTrigger).toBe(trigger);
});
});

0 comments on commit a52f4c4

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