-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #536 from RomkeVdMeulen/feat-535-configure-trigger
feat(config): support global config option for default validation trigger
- Loading branch information
Showing
6 changed files
with
109 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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 GlobalValidationConfiguration { | ||
public static DEFAULT_VALIDATION_TRIGGER = validateTrigger.blur; | ||
|
||
private validatorType: { new (...args: any[]): Validator } = StandardValidator; | ||
private validationTrigger = GlobalValidationConfiguration.DEFAULT_VALIDATION_TRIGGER; | ||
|
||
/** | ||
* Use a custom Validator implementation. | ||
*/ | ||
public customValidator(type: { new (...args: any[]): Validator }) { | ||
this.validatorType = type; | ||
return this; | ||
} | ||
|
||
public defaultValidationTrigger(trigger: validateTrigger) { | ||
this.validationTrigger = trigger; | ||
return this; | ||
} | ||
|
||
public getDefaultValidationTrigger() { | ||
return this.validationTrigger; | ||
} | ||
|
||
/** | ||
* Applies the configuration. | ||
*/ | ||
public apply(container: Container) { | ||
const validator = container.get(this.validatorType); | ||
container.registerInstance(Validator, validator); | ||
container.registerInstance(GlobalValidationConfiguration, this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
GlobalValidationConfiguration, | ||
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 GlobalValidationConfiguration(); | ||
config.defaultValidationTrigger(validateTrigger.manual); | ||
container.registerInstance(GlobalValidationConfiguration, 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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
GlobalValidationConfiguration, | ||
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(GlobalValidationConfiguration.DEFAULT_VALIDATION_TRIGGER); | ||
|
||
const trigger = validateTrigger.changeOrBlur; | ||
const config = new GlobalValidationConfiguration(); | ||
config.defaultValidationTrigger(trigger); | ||
const controllerWithConfig = new ValidationController(validator, parser, config); | ||
expect(controllerWithConfig.validateTrigger).toBe(trigger); | ||
}); | ||
}); |