-
-
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.
feat(config): support global config option for default validation tri…
…gger
- Loading branch information
1 parent
d3a2998
commit 39a4e67
Showing
5 changed files
with
79 additions
and
50 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,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); | ||
} | ||
} |
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 { | ||
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); | ||
}); | ||
}); |