Skip to content

Commit

Permalink
feat(validation-messages): displayName function
Browse files Browse the repository at this point in the history
  • Loading branch information
dkent600 authored and jdanyow committed Jan 31, 2017
1 parent 1b701ab commit 233fbbc
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion doc/article/en-US/validation-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ To begin defining a ruleset, use the `ValidationRules` class. Start by targeting

### displayName

Once you've targetted a property using `ensure` you can define the property's display name using `.displayName(name: string)`. Display names are used in validation messages. Specifying a display name is optional. If you do not explicitly set the display name the validation engine will attempt to compute the display name for you by splitting the property name on upper-case letters. A `firstName` property's display name would be `First Name`.
Once you've targetted a property using `ensure` you can define the property's display name using `.displayName(name: string|ValidationDisplayNameAccessor)`. Display names are used in validation messages. Specifying a display name is optional. If you do not explicitly set the display name the validation engine will attempt to compute the display name for you by splitting the property name on upper-case letters. A `firstName` property's display name would be `First Name`.

<code-listing heading="displayName">
<source-code lang="ES 2015">
Expand Down Expand Up @@ -98,6 +98,7 @@ All rules have a standard message that can be overriden on a case-by-case basis
* `$value`: the property value (at the moment the validation rule was executed).
* `$object`: the object that owns the property.
* `$config`: an object containing the rule's configuration. For example, the config for a `minLength` rule will have a `length` property.
* `$getDisplayName`: returns a displayable name of a property given the property name (irrespective of the property's displayName), split on capital letters, first letter ensured to be capitalized.

Here's an example:

Expand Down
4 changes: 3 additions & 1 deletion src/implementation/rule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Expression } from 'aurelia-binding';

export type ValidationDisplayNameAccessor = () => string;

/**
* Information related to a property that is the subject of validation.
*/
Expand All @@ -12,7 +14,7 @@ export interface RuleProperty {
/**
* The displayName of the property (or object).
*/
displayName: string | null;
displayName: string | ValidationDisplayNameAccessor | null;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/implementation/standard-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export class StandardValidator extends Validator {
$value: value,
$object: object,
$config: rule.config,
// returns the name of a given property, given just the property name (irrespective of the property's displayName)
// split on capital letters, first letter ensured to be capitalized
$getDisplayName: this.getDisplayName
};
return expression.evaluate(
Expand Down
4 changes: 2 additions & 2 deletions src/implementation/validation-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export class ValidationMessageProvider {
* Override this with your own custom logic.
* @param propertyName The property name.
*/
public getDisplayName(propertyName: string, displayName: string|null|undefined): string {
public getDisplayName(propertyName: string, displayName?: string|null|Function): string {
if (displayName !== null && displayName !== undefined) {
return displayName;
return (displayName instanceof Function) ? displayName() : displayName as string;
}

// split on upper-case letters.
Expand Down
3 changes: 2 additions & 1 deletion src/implementation/validation-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ValidationParser, PropertyAccessor } from './validation-parser';
import { isString } from './util';
import { Rules } from './rules';
import { validationMessages } from './validation-messages';
import { ValidationDisplayNameAccessor } from './rule';

/**
* Part of the fluent rule API. Enables customizing property rules.
Expand Down Expand Up @@ -223,7 +224,7 @@ export class FluentRules<TObject, TValue> {
/**
* Sets the display name of the ensured property.
*/
public displayName(name: string) {
public displayName(name: string | ValidationDisplayNameAccessor | null) {
this.property.displayName = name;
return this;
}
Expand Down
1 change: 1 addition & 0 deletions test/validation-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ describe('ValidationMessageProvider', () => {
expect(messageProvider.getDisplayName('fooBar', undefined)).toBe('Foo Bar');
expect(messageProvider.getDisplayName('foo bar', undefined)).toBe('Foo bar');
expect(messageProvider.getDisplayName('foo', 'hello')).toBe('hello');
expect(messageProvider.getDisplayName('foo', () => 'hello')).toBe('hello');
});
});

0 comments on commit 233fbbc

Please sign in to comment.