diff --git a/bower.json b/bower.json index 4dea8048..3292121e 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "aurelia-validation", - "version": "1.3.0", + "version": "1.3.1", "description": "Validation for Aurelia applications", "keywords": [ "aurelia", diff --git a/dist/amd/aurelia-validation.js b/dist/amd/aurelia-validation.js index 29520b9e..0c3c6d23 100644 --- a/dist/amd/aurelia-validation.js +++ b/dist/amd/aurelia-validation.js @@ -1,4 +1,4 @@ -define('aurelia-validation', ['exports', 'aurelia-pal', 'aurelia-binding', 'aurelia-dependency-injection', 'aurelia-task-queue', 'aurelia-templating', 'aurelia-framework', 'aurelia-logging'], function (exports, aureliaPal, aureliaBinding, aureliaDependencyInjection, aureliaTaskQueue, aureliaTemplating, aureliaFramework, LogManager) { 'use strict'; +define('aurelia-validation', ['exports', 'aurelia-pal', 'aurelia-binding', 'aurelia-dependency-injection', 'aurelia-task-queue', 'aurelia-templating', 'aurelia-logging'], function (exports, aureliaPal, aureliaBinding, aureliaDependencyInjection, aureliaTaskQueue, aureliaTemplating, LogManager) { 'use strict'; /** * Gets the DOM element associated with the data-binding. Most of the time it's @@ -940,7 +940,7 @@ define('aurelia-validation', ['exports', 'aurelia-pal', 'aurelia-binding', 'aure this.renderer = null; }; ValidationRendererCustomAttribute = __decorate([ - aureliaFramework.customAttribute('validation-renderer') + aureliaTemplating.customAttribute('validation-renderer') ], ValidationRendererCustomAttribute); return ValidationRendererCustomAttribute; }()); diff --git a/dist/aurelia-validation.d.ts b/dist/aurelia-validation.d.ts new file mode 100644 index 00000000..317724d8 --- /dev/null +++ b/dist/aurelia-validation.d.ts @@ -0,0 +1,896 @@ +import { AccessKeyed, AccessMember, AccessScope, Binary, Binding, BindingBehavior, CallMember, Conditional, Expression, Parser, Scope, ValueConverter } from 'aurelia-binding'; +import { Container, Lazy } from 'aurelia-dependency-injection'; +import { TaskQueue } from 'aurelia-task-queue'; +import { BindingLanguage, ViewResources } from 'aurelia-templating'; + +/** + * The result of validating an individual validation rule. + */ +export declare class ValidateResult { + rule: any; + object: any; + propertyName: string | number | null; + valid: boolean; + message: string | null; + private static nextId; + /** + * A number that uniquely identifies the result instance. + */ + id: number; + /** + * @param rule The rule associated with the result. Validator implementation specific. + * @param object The object that was validated. + * @param propertyName The name of the property that was validated. + * @param error The error, if the result is a validation error. + */ + constructor(rule: any, object: any, propertyName: string | number | null, valid: boolean, message?: string | null); + toString(): string | null; +} +/** + * Instructions for the validation controller's validate method. + */ +export interface ValidateInstruction { + /** + * The object to validate. + */ + object: any; + /** + * The property to validate. Optional. + */ + propertyName?: any; + /** + * The rules to validate. Optional. + */ + rules?: any; +} +/** + * The result of a call to the validation controller's validate method. + */ +export interface ControllerValidateResult { + /** + * Whether validation passed. + */ + valid: boolean; + /** + * The validation result of every rule that was evaluated. + */ + results: ValidateResult[]; + /** + * The instruction passed to the controller's validate method. + */ + instruction?: ValidateInstruction; +} +/** + * Gets the DOM element associated with the data-binding. Most of the time it's + * the binding.target but sometimes binding.target is an aurelia custom element, + * or custom attribute which is a javascript "class" instance, so we need to use + * the controller's container to retrieve the actual DOM element. + */ +export declare function getTargetDOMElement(binding: any, view: any): Element; +/** + * Retrieves the object and property name for the specified expression. + * @param expression The expression + * @param source The scope + */ +export declare function getPropertyInfo(expression: Expression, source: Scope): { + object: object; + propertyName: string; +} | null; +export declare type PropertyAccessor = (object: TObject) => TValue; +export declare class PropertyAccessorParser { + private parser; + static inject: (typeof Parser)[]; + constructor(parser: Parser); + parse(property: string | number | PropertyAccessor): string | number; +} +export declare function getAccessorExpression(fn: string): string; +/** + * Validates objects and properties. + */ +export declare abstract class Validator { + /** + * Validates the specified property. + * @param object The object to validate. + * @param propertyName The name of the property to validate. + * @param rules Optional. If unspecified, the implementation should lookup the rules for the + * specified object. This may not be possible for all implementations of this interface. + */ + abstract validateProperty(object: any, propertyName: string, rules?: any): Promise; + /** + * Validates all rules for specified object and it's properties. + * @param object The object to validate. + * @param rules Optional. If unspecified, the implementation should lookup the rules for the + * specified object. This may not be possible for all implementations of this interface. + */ + abstract validateObject(object: any, rules?: any): Promise; + /** + * Determines whether a rule exists in a set of rules. + * @param rules The rules to search. + * @parem rule The rule to find. + */ + abstract ruleExists(rules: any, rule: any): boolean; +} +/** + * Validation triggers. + */ +export declare enum validateTrigger { + /** + * Manual validation. Use the controller's `validate()` and `reset()` methods + * to validate all bindings. + */ + manual = 0, + /** + * Validate the binding when the binding's target element fires a DOM "blur" event. + */ + blur = 1, + /** + * Validate the binding when it updates the model due to a change in the view. + */ + change = 2, + /** + * Validate the binding when the binding's target element fires a DOM "blur" event and + * when it updates the model due to a change in the view. + */ + changeOrBlur = 3 +} +/** + * A result to render (or unrender) and the associated elements (if any) + */ +export interface ResultInstruction { + /** + * The validation result. + */ + result: ValidateResult; + /** + * The associated elements (if any). + */ + elements: Element[]; +} +/** + * Defines which validation results to render and which validation results to unrender. + */ +export interface RenderInstruction { + /** + * The "kind" of render instruction. Either 'validate' or 'reset'. + */ + kind: 'validate' | 'reset'; + /** + * The results to render. + */ + render: ResultInstruction[]; + /** + * The results to unrender. + */ + unrender: ResultInstruction[]; +} +/** + * Renders validation results. + */ +export interface ValidationRenderer { + /** + * Render the validation results. + * @param instruction The render instruction. Defines which results to render and which + * results to unrender. + */ + render(instruction: RenderInstruction): void; +} +export declare class ValidateEvent { + /** + * The type of validate event. Either "validate" or "reset". + */ + readonly type: 'validate' | 'reset'; + /** + * The controller's current array of errors. For an array containing both + * failed rules and passed rules, use the "results" property. + */ + readonly errors: ValidateResult[]; + /** + * The controller's current array of validate results. This + * includes both passed rules and failed rules. For an array of only failed rules, + * use the "errors" property. + */ + readonly results: ValidateResult[]; + /** + * The instruction passed to the "validate" or "reset" event. Will be null when + * the controller's validate/reset method was called with no instruction argument. + */ + readonly instruction: ValidateInstruction | null; + /** + * In events with type === "validate", this property will contain the result + * of validating the instruction (see "instruction" property). Use the controllerValidateResult + * to access the validate results specific to the call to "validate" + * (as opposed to using the "results" and "errors" properties to access the controller's entire + * set of results/errors). + */ + readonly controllerValidateResult: ControllerValidateResult | null; + constructor( + /** + * The type of validate event. Either "validate" or "reset". + */ + type: 'validate' | 'reset', + /** + * The controller's current array of errors. For an array containing both + * failed rules and passed rules, use the "results" property. + */ + errors: ValidateResult[], + /** + * The controller's current array of validate results. This + * includes both passed rules and failed rules. For an array of only failed rules, + * use the "errors" property. + */ + results: ValidateResult[], + /** + * The instruction passed to the "validate" or "reset" event. Will be null when + * the controller's validate/reset method was called with no instruction argument. + */ + instruction: ValidateInstruction | null, + /** + * In events with type === "validate", this property will contain the result + * of validating the instruction (see "instruction" property). Use the controllerValidateResult + * to access the validate results specific to the call to "validate" + * (as opposed to using the "results" and "errors" properties to access the controller's entire + * set of results/errors). + */ + controllerValidateResult: ControllerValidateResult | null); +} +/** + * Orchestrates validation. + * Manages a set of bindings, renderers and objects. + * Exposes the current list of validation results for binding purposes. + */ +export declare class ValidationController { + private validator; + private propertyParser; + static inject: (typeof PropertyAccessorParser | typeof Validator)[]; + private bindings; + private renderers; + /** + * Validation results that have been rendered by the controller. + */ + private results; + /** + * Validation errors that have been rendered by the controller. + */ + errors: ValidateResult[]; + /** + * Whether the controller is currently validating. + */ + validating: boolean; + private elements; + private objects; + /** + * The trigger that will invoke automatic validation of a property used in a binding. + */ + validateTrigger: validateTrigger; + private finishValidating; + private eventCallbacks; + constructor(validator: Validator, propertyParser: PropertyAccessorParser); + /** + * Subscribe to controller validate and reset events. These events occur when the + * controller's "validate"" and "reset" methods are called. + * @param callback The callback to be invoked when the controller validates or resets. + */ + subscribe(callback: (event: ValidateEvent) => void): { + dispose: () => void; + }; + /** + * Adds an object to the set of objects that should be validated when validate is called. + * @param object The object. + * @param rules Optional. The rules. If rules aren't supplied the Validator implementation will lookup the rules. + */ + addObject(object: any, rules?: any): void; + /** + * Removes an object from the set of objects that should be validated when validate is called. + * @param object The object. + */ + removeObject(object: any): void; + /** + * Adds and renders an error. + */ + addError(message: string, object: TObject, propertyName?: string | PropertyAccessor | null): ValidateResult; + /** + * Removes and unrenders an error. + */ + removeError(result: ValidateResult): void; + /** + * Adds a renderer. + * @param renderer The renderer. + */ + addRenderer(renderer: ValidationRenderer): void; + /** + * Removes a renderer. + * @param renderer The renderer. + */ + removeRenderer(renderer: ValidationRenderer): void; + /** + * Registers a binding with the controller. + * @param binding The binding instance. + * @param target The DOM element. + * @param rules (optional) rules associated with the binding. Validator implementation specific. + */ + registerBinding(binding: Binding, target: Element, rules?: any): void; + /** + * Unregisters a binding with the controller. + * @param binding The binding instance. + */ + unregisterBinding(binding: Binding): void; + /** + * Interprets the instruction and returns a predicate that will identify + * relevant results in the list of rendered validation results. + */ + private getInstructionPredicate; + /** + * Validates and renders results. + * @param instruction Optional. Instructions on what to validate. If undefined, all + * objects and bindings will be validated. + */ + validate(instruction?: ValidateInstruction): Promise; + /** + * Resets any rendered validation results (unrenders). + * @param instruction Optional. Instructions on what to reset. If unspecified all rendered results + * will be unrendered. + */ + reset(instruction?: ValidateInstruction): void; + /** + * Gets the elements associated with an object and propertyName (if any). + */ + private getAssociatedElements; + private processResultDelta; + /** + * Validates the property associated with a binding. + */ + validateBinding(binding: Binding): void; + /** + * Resets the results for a property associated with a binding. + */ + resetBinding(binding: Binding): void; + /** + * Changes the controller's validateTrigger. + * @param newTrigger The new validateTrigger + */ + changeTrigger(newTrigger: validateTrigger): void; + /** + * Revalidates the controller's current set of errors. + */ + revalidateErrors(): void; + private invokeCallbacks; +} +declare abstract class ValidateBindingBehaviorBase { + private taskQueue; + constructor(taskQueue: TaskQueue); + protected abstract getValidateTrigger(controller: ValidationController): validateTrigger; + bind(binding: any, source: any, rulesOrController?: ValidationController | any, rules?: any): void; + unbind(binding: any): void; +} +/** + * Binding behavior. Indicates the bound property should be validated + * when the validate trigger specified by the associated controller's + * validateTrigger property occurs. + */ +export declare class ValidateBindingBehavior extends ValidateBindingBehaviorBase { + static inject: (typeof TaskQueue)[]; + getValidateTrigger(controller: ValidationController): validateTrigger; +} +/** + * Binding behavior. Indicates the bound property will be validated + * manually, by calling controller.validate(). No automatic validation + * triggered by data-entry or blur will occur. + */ +export declare class ValidateManuallyBindingBehavior extends ValidateBindingBehaviorBase { + static inject: (typeof TaskQueue)[]; + getValidateTrigger(): validateTrigger; +} +/** + * Binding behavior. Indicates the bound property should be validated + * when the associated element blurs. + */ +export declare class ValidateOnBlurBindingBehavior extends ValidateBindingBehaviorBase { + static inject: (typeof TaskQueue)[]; + getValidateTrigger(): validateTrigger; +} +/** + * Binding behavior. Indicates the bound property should be validated + * when the associated element is changed by the user, causing a change + * to the model. + */ +export declare class ValidateOnChangeBindingBehavior extends ValidateBindingBehaviorBase { + static inject: (typeof TaskQueue)[]; + getValidateTrigger(): validateTrigger; +} +/** + * Binding behavior. Indicates the bound property should be validated + * when the associated element blurs or is changed by the user, causing + * a change to the model. + */ +export declare class ValidateOnChangeOrBlurBindingBehavior extends ValidateBindingBehaviorBase { + static inject: (typeof TaskQueue)[]; + getValidateTrigger(): validateTrigger; +} +/** + * Creates ValidationController instances. + */ +export declare class ValidationControllerFactory { + private container; + static get(container: Container): ValidationControllerFactory; + constructor(container: Container); + /** + * Creates a new controller instance. + */ + create(validator?: Validator): ValidationController; + /** + * Creates a new controller and registers it in the current element's container so that it's + * available to the validate binding behavior and renderers. + */ + createForCurrentScope(validator?: Validator): ValidationController; +} +export interface RenderedError { + error: ValidateResult; + targets: Element[]; +} +export declare class ValidationErrorsCustomAttribute implements ValidationRenderer { + private boundaryElement; + private controllerAccessor; + static inject(): ({ + new (): Element; + prototype: Element; + } | Lazy)[]; + controller: ValidationController | null; + errors: RenderedError[]; + private errorsInternal; + constructor(boundaryElement: Element, controllerAccessor: () => ValidationController); + sort(): void; + interestingElements(elements: Element[]): Element[]; + render(instruction: RenderInstruction): void; + bind(): void; + unbind(): void; +} +export declare class ValidationRendererCustomAttribute { + private container; + private controller; + private value; + private renderer; + created(view: any): void; + bind(): void; + unbind(): void; +} +export declare type ValidationDisplayNameAccessor = () => string; +/** + * Information related to a property that is the subject of validation. + */ +export interface RuleProperty { + /** + * The property name. null indicates the rule targets the object itself. + */ + name: string | number | null; + /** + * The displayName of the property (or object). + */ + displayName: string | ValidationDisplayNameAccessor | null; +} +/** + * A rule definition. Associations a rule with a property or object. + */ +export interface Rule { + property: RuleProperty; + condition: (value: TValue, object?: TObject) => boolean | Promise; + config: object; + when: ((object: TObject) => boolean) | null; + messageKey: string; + message: Expression | null; + sequence: number; + tag?: string; +} +/** + * Sets, unsets and retrieves rules on an object or constructor function. + */ +export declare class Rules { + /** + * The name of the property that stores the rules. + */ + private static key; + /** + * Applies the rules to a target. + */ + static set(target: any, rules: Rule[][]): void; + /** + * Removes rules from a target. + */ + static unset(target: any): void; + /** + * Retrieves the target's rules. + */ + static get(target: any): Rule[][] | null; +} +export declare type Chain = any; +export declare type Assign = any; +export declare type AccessThis = any; +export declare type AccessScope = any; +export declare type CallScope = any; +export declare type CallFunction = any; +export declare type PrefixNot = any; +export declare type LiteralPrimitive = any; +export declare type LiteralArray = any; +export declare type LiteralObject = any; +export declare type LiteralString = any; +declare class ExpressionVisitor { + visitChain(chain: Chain): void; + visitBindingBehavior(behavior: BindingBehavior): void; + visitValueConverter(converter: ValueConverter): void; + visitAssign(assign: Assign): void; + visitConditional(conditional: Conditional): void; + visitAccessThis(access: AccessThis): void; + visitAccessScope(access: AccessScope): void; + visitAccessMember(access: AccessMember): void; + visitAccessKeyed(access: AccessKeyed): void; + visitCallScope(call: CallScope): void; + visitCallFunction(call: CallFunction): void; + visitCallMember(call: CallMember): void; + visitPrefix(prefix: PrefixNot): void; + visitBinary(binary: Binary): void; + visitLiteralPrimitive(literal: LiteralPrimitive): void; + visitLiteralArray(literal: LiteralArray): void; + visitLiteralObject(literal: LiteralObject): void; + visitLiteralString(literal: LiteralString): void; + private visitArgs; +} +export declare class ValidationMessageParser { + private bindinqLanguage; + static inject: (typeof BindingLanguage)[]; + private emptyStringExpression; + private nullExpression; + private undefinedExpression; + private cache; + constructor(bindinqLanguage: BindingLanguage); + parse(message: string): Expression; + private coalesce; +} +export declare class MessageExpressionValidator extends ExpressionVisitor { + private originalMessage; + static validate(expression: Expression, originalMessage: string): void; + constructor(originalMessage: string); + visitAccessScope(access: AccessScope): void; +} +export interface ValidationMessages { + [key: string]: string; +} +/** + * Dictionary of validation messages. [messageKey]: messageExpression + */ +export declare const validationMessages: ValidationMessages; +/** + * Retrieves validation messages and property display names. + */ +export declare class ValidationMessageProvider { + parser: ValidationMessageParser; + static inject: (typeof ValidationMessageParser)[]; + constructor(parser: ValidationMessageParser); + /** + * Returns a message binding expression that corresponds to the key. + * @param key The message key. + */ + getMessage(key: string): Expression; + /** + * Formulates a property display name using the property name and the configured + * displayName (if provided). + * Override this with your own custom logic. + * @param propertyName The property name. + */ + getDisplayName(propertyName: string | number, displayName?: string | null | (() => string)): string; +} +/** + * Validates. + * Responsible for validating objects and properties. + */ +export declare class StandardValidator extends Validator { + static inject: (typeof ViewResources | typeof ValidationMessageProvider)[]; + private messageProvider; + private lookupFunctions; + private getDisplayName; + constructor(messageProvider: ValidationMessageProvider, resources: ViewResources); + /** + * Validates the specified property. + * @param object The object to validate. + * @param propertyName The name of the property to validate. + * @param rules Optional. If unspecified, the rules will be looked up using the metadata + * for the object created by ValidationRules....on(class/object) + */ + validateProperty(object: any, propertyName: string | number, rules?: any): Promise; + /** + * Validates all rules for specified object and it's properties. + * @param object The object to validate. + * @param rules Optional. If unspecified, the rules will be looked up using the metadata + * for the object created by ValidationRules....on(class/object) + */ + validateObject(object: any, rules?: any): Promise; + /** + * Determines whether a rule exists in a set of rules. + * @param rules The rules to search. + * @parem rule The rule to find. + */ + ruleExists(rules: Rule[][], rule: Rule): boolean; + private getMessage; + private validateRuleSequence; + private validate; +} +/** + * Part of the fluent rule API. Enables customizing property rules. + */ +export declare class FluentRuleCustomizer { + private fluentEnsure; + private fluentRules; + private parsers; + private rule; + constructor(property: RuleProperty, condition: (value: TValue, object?: TObject) => boolean | Promise, config: object | undefined, fluentEnsure: FluentEnsure, fluentRules: FluentRules, parsers: Parsers); + /** + * Validate subsequent rules after previously declared rules have + * been validated successfully. Use to postpone validation of costly + * rules until less expensive rules pass validation. + */ + then(): this; + /** + * Specifies the key to use when looking up the rule's validation message. + */ + withMessageKey(key: string): this; + /** + * Specifies rule's validation message. + */ + withMessage(message: string): this; + /** + * Specifies a condition that must be met before attempting to validate the rule. + * @param condition A function that accepts the object as a parameter and returns true + * or false whether the rule should be evaluated. + */ + when(condition: (object: TObject) => boolean): this; + /** + * Tags the rule instance, enabling the rule to be found easily + * using ValidationRules.taggedRules(rules, tag) + */ + tag(tag: string): this; + /** + * Target a property with validation rules. + * @param property The property to target. Can be the property name or a property accessor function. + */ + ensure(subject: string | ((model: TObject) => TValue2)): FluentRules; + /** + * Targets an object with validation rules. + */ + ensureObject(): FluentRules; + /** + * Rules that have been defined using the fluent API. + */ + readonly rules: Rule[][]; + /** + * Applies the rules to a class or object, making them discoverable by the StandardValidator. + * @param target A class or object. + */ + on(target: any): FluentEnsure; + /** + * Applies an ad-hoc rule function to the ensured property or object. + * @param condition The function to validate the rule. + * Will be called with two arguments, the property value and the object. + * Should return a boolean or a Promise that resolves to a boolean. + */ + satisfies(condition: (value: TValue, object: TObject) => boolean | Promise, config?: object): FluentRuleCustomizer; + /** + * Applies a rule by name. + * @param name The name of the custom or standard rule. + * @param args The rule's arguments. + */ + satisfiesRule(name: string, ...args: any[]): FluentRuleCustomizer; + /** + * Applies the "required" rule to the property. + * The value cannot be null, undefined or whitespace. + */ + required(): FluentRuleCustomizer; + /** + * Applies the "matches" rule to the property. + * Value must match the specified regular expression. + * null, undefined and empty-string values are considered valid. + */ + matches(regex: RegExp): FluentRuleCustomizer; + /** + * Applies the "email" rule to the property. + * null, undefined and empty-string values are considered valid. + */ + email(): FluentRuleCustomizer; + /** + * Applies the "minLength" STRING validation rule to the property. + * null, undefined and empty-string values are considered valid. + */ + minLength(length: number): FluentRuleCustomizer; + /** + * Applies the "maxLength" STRING validation rule to the property. + * null, undefined and empty-string values are considered valid. + */ + maxLength(length: number): FluentRuleCustomizer; + /** + * Applies the "minItems" ARRAY validation rule to the property. + * null and undefined values are considered valid. + */ + minItems(count: number): FluentRuleCustomizer; + /** + * Applies the "maxItems" ARRAY validation rule to the property. + * null and undefined values are considered valid. + */ + maxItems(count: number): FluentRuleCustomizer; + /** + * Applies the "equals" validation rule to the property. + * null, undefined and empty-string values are considered valid. + */ + equals(expectedValue: TValue): FluentRuleCustomizer; +} +/** + * Part of the fluent rule API. Enables applying rules to properties and objects. + */ +export declare class FluentRules { + private fluentEnsure; + private parsers; + private property; + static customRules: { + [name: string]: { + condition: (value: any, object?: any, ...fluentArgs: any[]) => boolean | Promise; + argsToConfig?: (...args: any[]) => any; + }; + }; + /** + * Current rule sequence number. Used to postpone evaluation of rules until rules + * with lower sequence number have successfully validated. The "then" fluent API method + * manages this property, there's usually no need to set it directly. + */ + sequence: number; + constructor(fluentEnsure: FluentEnsure, parsers: Parsers, property: RuleProperty); + /** + * Sets the display name of the ensured property. + */ + displayName(name: string | ValidationDisplayNameAccessor | null): this; + /** + * Applies an ad-hoc rule function to the ensured property or object. + * @param condition The function to validate the rule. + * Will be called with two arguments, the property value and the object. + * Should return a boolean or a Promise that resolves to a boolean. + */ + satisfies(condition: (value: TValue, object?: TObject) => boolean | Promise, config?: object): FluentRuleCustomizer; + /** + * Applies a rule by name. + * @param name The name of the custom or standard rule. + * @param args The rule's arguments. + */ + satisfiesRule(name: string, ...args: any[]): FluentRuleCustomizer; + /** + * Applies the "required" rule to the property. + * The value cannot be null, undefined or whitespace. + */ + required(): FluentRuleCustomizer; + /** + * Applies the "matches" rule to the property. + * Value must match the specified regular expression. + * null, undefined and empty-string values are considered valid. + */ + matches(regex: RegExp): FluentRuleCustomizer; + /** + * Applies the "email" rule to the property. + * null, undefined and empty-string values are considered valid. + */ + email(): FluentRuleCustomizer; + /** + * Applies the "minLength" STRING validation rule to the property. + * null, undefined and empty-string values are considered valid. + */ + minLength(length: number): FluentRuleCustomizer; + /** + * Applies the "maxLength" STRING validation rule to the property. + * null, undefined and empty-string values are considered valid. + */ + maxLength(length: number): FluentRuleCustomizer; + /** + * Applies the "minItems" ARRAY validation rule to the property. + * null and undefined values are considered valid. + */ + minItems(count: number): FluentRuleCustomizer; + /** + * Applies the "maxItems" ARRAY validation rule to the property. + * null and undefined values are considered valid. + */ + maxItems(count: number): FluentRuleCustomizer; + /** + * Applies the "equals" validation rule to the property. + * null and undefined values are considered valid. + */ + equals(expectedValue: TValue): FluentRuleCustomizer; +} +/** + * Part of the fluent rule API. Enables targeting properties and objects with rules. + */ +export declare class FluentEnsure { + private parsers; + /** + * Rules that have been defined using the fluent API. + */ + rules: Rule[][]; + constructor(parsers: Parsers); + /** + * Target a property with validation rules. + * @param property The property to target. Can be the property name or a property accessor + * function. + */ + ensure(property: string | number | PropertyAccessor): FluentRules; + /** + * Targets an object with validation rules. + */ + ensureObject(): FluentRules; + /** + * Applies the rules to a class or object, making them discoverable by the StandardValidator. + * @param target A class or object. + */ + on(target: any): this; + private assertInitialized; + private mergeRules; +} +/** + * Fluent rule definition API. + */ +export declare class ValidationRules { + private static parsers; + static initialize(messageParser: ValidationMessageParser, propertyParser: PropertyAccessorParser): void; + /** + * Target a property with validation rules. + * @param property The property to target. Can be the property name or a property accessor function. + */ + static ensure(property: string | number | PropertyAccessor): FluentRules; + /** + * Targets an object with validation rules. + */ + static ensureObject(): FluentRules; + /** + * Defines a custom rule. + * @param name The name of the custom rule. Also serves as the message key. + * @param condition The rule function. + * @param message The message expression + * @param argsToConfig A function that maps the rule's arguments to a "config" + * object that can be used when evaluating the message expression. + */ + static customRule(name: string, condition: (value: any, object?: any, ...args: any[]) => boolean | Promise, message: string, argsToConfig?: (...args: any[]) => any): void; + /** + * Returns rules with the matching tag. + * @param rules The rules to search. + * @param tag The tag to search for. + */ + static taggedRules(rules: Rule[][], tag: string): Rule[][]; + /** + * Returns rules that have no tag. + * @param rules The rules to search. + */ + static untaggedRules(rules: Rule[][]): Rule[][]; + /** + * Removes the rules from a class or object. + * @param target A class or object. + */ + static off(target: any): void; +} +export interface Parsers { + message: ValidationMessageParser; + property: PropertyAccessorParser; +} +/** + * Aurelia Validation Configuration API + */ +export declare class AureliaValidationConfiguration { + private validatorType; + /** + * Use a custom Validator implementation. + */ + customValidator(type: { + new (...args: any[]): Validator; + }): void; + /** + * Applies the configuration. + */ + apply(container: Container): void; +} +/** + * Configures the plugin. + */ +export declare function configure(frameworkConfig: { + container: Container; + globalResources?: (...resources: any[]) => any; +}, callback?: (config: AureliaValidationConfiguration) => void): void; \ No newline at end of file diff --git a/dist/commonjs/aurelia-validation.js b/dist/commonjs/aurelia-validation.js index fc3f9314..d13b7640 100644 --- a/dist/commonjs/aurelia-validation.js +++ b/dist/commonjs/aurelia-validation.js @@ -7,7 +7,6 @@ var aureliaBinding = require('aurelia-binding'); var aureliaDependencyInjection = require('aurelia-dependency-injection'); var aureliaTaskQueue = require('aurelia-task-queue'); var aureliaTemplating = require('aurelia-templating'); -var aureliaFramework = require('aurelia-framework'); var LogManager = require('aurelia-logging'); /** @@ -950,7 +949,7 @@ var ValidationRendererCustomAttribute = /** @class */ (function () { this.renderer = null; }; ValidationRendererCustomAttribute = __decorate([ - aureliaFramework.customAttribute('validation-renderer') + aureliaTemplating.customAttribute('validation-renderer') ], ValidationRendererCustomAttribute); return ValidationRendererCustomAttribute; }()); diff --git a/dist/es2015/aurelia-validation.js b/dist/es2015/aurelia-validation.js index b66f6e70..4c67ca36 100644 --- a/dist/es2015/aurelia-validation.js +++ b/dist/es2015/aurelia-validation.js @@ -3,7 +3,6 @@ import { AccessMember, AccessScope, AccessKeyed, BindingBehavior, ValueConverter import { Optional, Lazy } from 'aurelia-dependency-injection'; import { TaskQueue } from 'aurelia-task-queue'; import { customAttribute, bindable, BindingLanguage, ViewResources } from 'aurelia-templating'; -import { customAttribute as customAttribute$1 } from 'aurelia-framework'; import { getLogger } from 'aurelia-logging'; /** @@ -869,7 +868,7 @@ let ValidationRendererCustomAttribute = class ValidationRendererCustomAttribute } }; ValidationRendererCustomAttribute = __decorate([ - customAttribute$1('validation-renderer') + customAttribute('validation-renderer') ], ValidationRendererCustomAttribute); /** diff --git a/dist/es2017/aurelia-validation.js b/dist/es2017/aurelia-validation.js index b66f6e70..4c67ca36 100644 --- a/dist/es2017/aurelia-validation.js +++ b/dist/es2017/aurelia-validation.js @@ -3,7 +3,6 @@ import { AccessMember, AccessScope, AccessKeyed, BindingBehavior, ValueConverter import { Optional, Lazy } from 'aurelia-dependency-injection'; import { TaskQueue } from 'aurelia-task-queue'; import { customAttribute, bindable, BindingLanguage, ViewResources } from 'aurelia-templating'; -import { customAttribute as customAttribute$1 } from 'aurelia-framework'; import { getLogger } from 'aurelia-logging'; /** @@ -869,7 +868,7 @@ let ValidationRendererCustomAttribute = class ValidationRendererCustomAttribute } }; ValidationRendererCustomAttribute = __decorate([ - customAttribute$1('validation-renderer') + customAttribute('validation-renderer') ], ValidationRendererCustomAttribute); /** diff --git a/dist/native-modules/aurelia-validation.js b/dist/native-modules/aurelia-validation.js index ef2c2b4a..72c02d98 100644 --- a/dist/native-modules/aurelia-validation.js +++ b/dist/native-modules/aurelia-validation.js @@ -3,7 +3,6 @@ import { AccessMember, AccessScope, AccessKeyed, BindingBehavior, ValueConverter import { Optional, Lazy } from 'aurelia-dependency-injection'; import { TaskQueue } from 'aurelia-task-queue'; import { customAttribute, bindable, BindingLanguage, ViewResources } from 'aurelia-templating'; -import { customAttribute as customAttribute$1 } from 'aurelia-framework'; import { getLogger } from 'aurelia-logging'; /** @@ -947,7 +946,7 @@ var ValidationRendererCustomAttribute = /** @class */ (function () { this.renderer = null; }; ValidationRendererCustomAttribute = __decorate([ - customAttribute$1('validation-renderer') + customAttribute('validation-renderer') ], ValidationRendererCustomAttribute); return ValidationRendererCustomAttribute; }()); diff --git a/dist/system/aurelia-validation.js b/dist/system/aurelia-validation.js index e31bcf14..d1578309 100644 --- a/dist/system/aurelia-validation.js +++ b/dist/system/aurelia-validation.js @@ -1,6 +1,6 @@ -System.register(['aurelia-pal', 'aurelia-binding', 'aurelia-dependency-injection', 'aurelia-task-queue', 'aurelia-templating', 'aurelia-framework', 'aurelia-logging'], function (exports, module) { +System.register(['aurelia-pal', 'aurelia-binding', 'aurelia-dependency-injection', 'aurelia-task-queue', 'aurelia-templating', 'aurelia-logging'], function (exports, module) { 'use strict'; - var DOM, AccessMember, AccessScope, AccessKeyed, BindingBehavior, ValueConverter, getContextFor, Parser, bindingBehavior, bindingMode, LiteralString, Binary, Conditional, LiteralPrimitive, CallMember, Optional, Lazy, TaskQueue, customAttribute, bindable, BindingLanguage, ViewResources, customAttribute$1, getLogger; + var DOM, AccessMember, AccessScope, AccessKeyed, BindingBehavior, ValueConverter, getContextFor, Parser, bindingBehavior, bindingMode, LiteralString, Binary, Conditional, LiteralPrimitive, CallMember, Optional, Lazy, TaskQueue, customAttribute, bindable, BindingLanguage, ViewResources, getLogger; return { setters: [function (module) { DOM = module.DOM; @@ -29,8 +29,6 @@ System.register(['aurelia-pal', 'aurelia-binding', 'aurelia-dependency-injection bindable = module.bindable; BindingLanguage = module.BindingLanguage; ViewResources = module.ViewResources; - }, function (module) { - customAttribute$1 = module.customAttribute; }, function (module) { getLogger = module.getLogger; }], @@ -985,7 +983,7 @@ System.register(['aurelia-pal', 'aurelia-binding', 'aurelia-dependency-injection this.renderer = null; }; ValidationRendererCustomAttribute = __decorate([ - customAttribute$1('validation-renderer') + customAttribute('validation-renderer') ], ValidationRendererCustomAttribute); return ValidationRendererCustomAttribute; }())); diff --git a/dist/types/aurelia-validation.d.ts b/dist/types/aurelia-validation.d.ts deleted file mode 100644 index d8db2f4e..00000000 --- a/dist/types/aurelia-validation.d.ts +++ /dev/null @@ -1,46 +0,0 @@ -export * from './controller-validate-result'; -export * from './get-target-dom-element'; -export * from './property-info'; -export * from './property-accessor-parser'; -export * from './validate-binding-behavior'; -export * from './validate-event'; -export * from './validate-instruction'; -export * from './validate-result'; -export * from './validate-trigger'; -export * from './validation-controller'; -export * from './validation-controller-factory'; -export * from './validation-errors-custom-attribute'; -export * from './validation-renderer-custom-attribute'; -export * from './validation-renderer'; -export * from './validator'; -export * from './implementation/rule'; -export * from './implementation/rules'; -export * from './implementation/standard-validator'; -export * from './implementation/validation-messages'; -export * from './implementation/validation-message-parser'; -export * from './implementation/validation-rules'; -import { Container } from 'aurelia-dependency-injection'; -import { Validator } from './validator'; -/** - * Aurelia Validation Configuration API - */ -export declare class AureliaValidationConfiguration { - private validatorType; - /** - * Use a custom Validator implementation. - */ - customValidator(type: { - new (...args: any[]): Validator; - }): void; - /** - * Applies the configuration. - */ - apply(container: Container): void; -} -/** - * Configures the plugin. - */ -export declare function configure(frameworkConfig: { - container: Container; - globalResources?: (...resources: any[]) => any; -}, callback?: (config: AureliaValidationConfiguration) => void): void; diff --git a/dist/types/controller-validate-result.d.ts b/dist/types/controller-validate-result.d.ts deleted file mode 100644 index 08049098..00000000 --- a/dist/types/controller-validate-result.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { ValidateResult } from './validate-result'; -import { ValidateInstruction } from './validate-instruction'; -/** - * The result of a call to the validation controller's validate method. - */ -export interface ControllerValidateResult { - /** - * Whether validation passed. - */ - valid: boolean; - /** - * The validation result of every rule that was evaluated. - */ - results: ValidateResult[]; - /** - * The instruction passed to the controller's validate method. - */ - instruction?: ValidateInstruction; -} diff --git a/dist/types/get-target-dom-element.d.ts b/dist/types/get-target-dom-element.d.ts deleted file mode 100644 index 38409e45..00000000 --- a/dist/types/get-target-dom-element.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Gets the DOM element associated with the data-binding. Most of the time it's - * the binding.target but sometimes binding.target is an aurelia custom element, - * or custom attribute which is a javascript "class" instance, so we need to use - * the controller's container to retrieve the actual DOM element. - */ -export declare function getTargetDOMElement(binding: any, view: any): Element; diff --git a/dist/types/implementation/expression-visitor.d.ts b/dist/types/implementation/expression-visitor.d.ts deleted file mode 100644 index 9a6f3d98..00000000 --- a/dist/types/implementation/expression-visitor.d.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { ValueConverter, Conditional, AccessMember, AccessKeyed, CallMember, BindingBehavior, Binary } from 'aurelia-binding'; -export declare type Chain = any; -export declare type Assign = any; -export declare type AccessThis = any; -export declare type AccessScope = any; -export declare type CallScope = any; -export declare type CallFunction = any; -export declare type PrefixNot = any; -export declare type LiteralPrimitive = any; -export declare type LiteralArray = any; -export declare type LiteralObject = any; -export declare type LiteralString = any; -export declare class ExpressionVisitor { - visitChain(chain: Chain): void; - visitBindingBehavior(behavior: BindingBehavior): void; - visitValueConverter(converter: ValueConverter): void; - visitAssign(assign: Assign): void; - visitConditional(conditional: Conditional): void; - visitAccessThis(access: AccessThis): void; - visitAccessScope(access: AccessScope): void; - visitAccessMember(access: AccessMember): void; - visitAccessKeyed(access: AccessKeyed): void; - visitCallScope(call: CallScope): void; - visitCallFunction(call: CallFunction): void; - visitCallMember(call: CallMember): void; - visitPrefix(prefix: PrefixNot): void; - visitBinary(binary: Binary): void; - visitLiteralPrimitive(literal: LiteralPrimitive): void; - visitLiteralArray(literal: LiteralArray): void; - visitLiteralObject(literal: LiteralObject): void; - visitLiteralString(literal: LiteralString): void; - private visitArgs; -} diff --git a/dist/types/implementation/rule.d.ts b/dist/types/implementation/rule.d.ts deleted file mode 100644 index 6812a750..00000000 --- a/dist/types/implementation/rule.d.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { Expression } from 'aurelia-binding'; -export declare type ValidationDisplayNameAccessor = () => string; -/** - * Information related to a property that is the subject of validation. - */ -export interface RuleProperty { - /** - * The property name. null indicates the rule targets the object itself. - */ - name: string | number | null; - /** - * The displayName of the property (or object). - */ - displayName: string | ValidationDisplayNameAccessor | null; -} -/** - * A rule definition. Associations a rule with a property or object. - */ -export interface Rule { - property: RuleProperty; - condition: (value: TValue, object?: TObject) => boolean | Promise; - config: object; - when: ((object: TObject) => boolean) | null; - messageKey: string; - message: Expression | null; - sequence: number; - tag?: string; -} diff --git a/dist/types/implementation/rules.d.ts b/dist/types/implementation/rules.d.ts deleted file mode 100644 index 6a87738d..00000000 --- a/dist/types/implementation/rules.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Rule } from './rule'; -/** - * Sets, unsets and retrieves rules on an object or constructor function. - */ -export declare class Rules { - /** - * The name of the property that stores the rules. - */ - private static key; - /** - * Applies the rules to a target. - */ - static set(target: any, rules: Rule[][]): void; - /** - * Removes rules from a target. - */ - static unset(target: any): void; - /** - * Retrieves the target's rules. - */ - static get(target: any): Rule[][] | null; -} diff --git a/dist/types/implementation/standard-validator.d.ts b/dist/types/implementation/standard-validator.d.ts deleted file mode 100644 index 04ce047a..00000000 --- a/dist/types/implementation/standard-validator.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { ViewResources } from 'aurelia-templating'; -import { Validator } from '../validator'; -import { ValidateResult } from '../validate-result'; -import { Rule } from './rule'; -import { ValidationMessageProvider } from './validation-messages'; -/** - * Validates. - * Responsible for validating objects and properties. - */ -export declare class StandardValidator extends Validator { - static inject: (typeof ValidationMessageProvider | typeof ViewResources)[]; - private messageProvider; - private lookupFunctions; - private getDisplayName; - constructor(messageProvider: ValidationMessageProvider, resources: ViewResources); - /** - * Validates the specified property. - * @param object The object to validate. - * @param propertyName The name of the property to validate. - * @param rules Optional. If unspecified, the rules will be looked up using the metadata - * for the object created by ValidationRules....on(class/object) - */ - validateProperty(object: any, propertyName: string | number, rules?: any): Promise; - /** - * Validates all rules for specified object and it's properties. - * @param object The object to validate. - * @param rules Optional. If unspecified, the rules will be looked up using the metadata - * for the object created by ValidationRules....on(class/object) - */ - validateObject(object: any, rules?: any): Promise; - /** - * Determines whether a rule exists in a set of rules. - * @param rules The rules to search. - * @parem rule The rule to find. - */ - ruleExists(rules: Rule[][], rule: Rule): boolean; - private getMessage; - private validateRuleSequence; - private validate; -} diff --git a/dist/types/implementation/validation-message-parser.d.ts b/dist/types/implementation/validation-message-parser.d.ts deleted file mode 100644 index 35b9fe66..00000000 --- a/dist/types/implementation/validation-message-parser.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Expression, AccessScope } from 'aurelia-binding'; -import { BindingLanguage } from 'aurelia-templating'; -import { ExpressionVisitor } from './expression-visitor'; -export declare class ValidationMessageParser { - private bindinqLanguage; - static inject: (typeof BindingLanguage)[]; - private emptyStringExpression; - private nullExpression; - private undefinedExpression; - private cache; - constructor(bindinqLanguage: BindingLanguage); - parse(message: string): Expression; - private coalesce; -} -export declare class MessageExpressionValidator extends ExpressionVisitor { - private originalMessage; - static validate(expression: Expression, originalMessage: string): void; - constructor(originalMessage: string); - visitAccessScope(access: AccessScope): void; -} diff --git a/dist/types/implementation/validation-messages.d.ts b/dist/types/implementation/validation-messages.d.ts deleted file mode 100644 index 1e3ae3b0..00000000 --- a/dist/types/implementation/validation-messages.d.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Expression } from 'aurelia-binding'; -import { ValidationMessageParser } from './validation-message-parser'; -export interface ValidationMessages { - [key: string]: string; -} -/** - * Dictionary of validation messages. [messageKey]: messageExpression - */ -export declare const validationMessages: ValidationMessages; -/** - * Retrieves validation messages and property display names. - */ -export declare class ValidationMessageProvider { - parser: ValidationMessageParser; - static inject: (typeof ValidationMessageParser)[]; - constructor(parser: ValidationMessageParser); - /** - * Returns a message binding expression that corresponds to the key. - * @param key The message key. - */ - getMessage(key: string): Expression; - /** - * Formulates a property display name using the property name and the configured - * displayName (if provided). - * Override this with your own custom logic. - * @param propertyName The property name. - */ - getDisplayName(propertyName: string | number, displayName?: string | null | (() => string)): string; -} diff --git a/dist/types/implementation/validation-rules.d.ts b/dist/types/implementation/validation-rules.d.ts deleted file mode 100644 index 63926a78..00000000 --- a/dist/types/implementation/validation-rules.d.ts +++ /dev/null @@ -1,262 +0,0 @@ -import { Rule, RuleProperty, ValidationDisplayNameAccessor } from './rule'; -import { ValidationMessageParser } from './validation-message-parser'; -import { PropertyAccessorParser, PropertyAccessor } from '../property-accessor-parser'; -/** - * Part of the fluent rule API. Enables customizing property rules. - */ -export declare class FluentRuleCustomizer { - private fluentEnsure; - private fluentRules; - private parsers; - private rule; - constructor(property: RuleProperty, condition: (value: TValue, object?: TObject) => boolean | Promise, config: object | undefined, fluentEnsure: FluentEnsure, fluentRules: FluentRules, parsers: Parsers); - /** - * Validate subsequent rules after previously declared rules have - * been validated successfully. Use to postpone validation of costly - * rules until less expensive rules pass validation. - */ - then(): this; - /** - * Specifies the key to use when looking up the rule's validation message. - */ - withMessageKey(key: string): this; - /** - * Specifies rule's validation message. - */ - withMessage(message: string): this; - /** - * Specifies a condition that must be met before attempting to validate the rule. - * @param condition A function that accepts the object as a parameter and returns true - * or false whether the rule should be evaluated. - */ - when(condition: (object: TObject) => boolean): this; - /** - * Tags the rule instance, enabling the rule to be found easily - * using ValidationRules.taggedRules(rules, tag) - */ - tag(tag: string): this; - /** - * Target a property with validation rules. - * @param property The property to target. Can be the property name or a property accessor function. - */ - ensure(subject: string | ((model: TObject) => TValue2)): FluentRules; - /** - * Targets an object with validation rules. - */ - ensureObject(): FluentRules; - /** - * Rules that have been defined using the fluent API. - */ - readonly rules: Rule[][]; - /** - * Applies the rules to a class or object, making them discoverable by the StandardValidator. - * @param target A class or object. - */ - on(target: any): FluentEnsure; - /** - * Applies an ad-hoc rule function to the ensured property or object. - * @param condition The function to validate the rule. - * Will be called with two arguments, the property value and the object. - * Should return a boolean or a Promise that resolves to a boolean. - */ - satisfies(condition: (value: TValue, object: TObject) => boolean | Promise, config?: object): FluentRuleCustomizer; - /** - * Applies a rule by name. - * @param name The name of the custom or standard rule. - * @param args The rule's arguments. - */ - satisfiesRule(name: string, ...args: any[]): FluentRuleCustomizer; - /** - * Applies the "required" rule to the property. - * The value cannot be null, undefined or whitespace. - */ - required(): FluentRuleCustomizer; - /** - * Applies the "matches" rule to the property. - * Value must match the specified regular expression. - * null, undefined and empty-string values are considered valid. - */ - matches(regex: RegExp): FluentRuleCustomizer; - /** - * Applies the "email" rule to the property. - * null, undefined and empty-string values are considered valid. - */ - email(): FluentRuleCustomizer; - /** - * Applies the "minLength" STRING validation rule to the property. - * null, undefined and empty-string values are considered valid. - */ - minLength(length: number): FluentRuleCustomizer; - /** - * Applies the "maxLength" STRING validation rule to the property. - * null, undefined and empty-string values are considered valid. - */ - maxLength(length: number): FluentRuleCustomizer; - /** - * Applies the "minItems" ARRAY validation rule to the property. - * null and undefined values are considered valid. - */ - minItems(count: number): FluentRuleCustomizer; - /** - * Applies the "maxItems" ARRAY validation rule to the property. - * null and undefined values are considered valid. - */ - maxItems(count: number): FluentRuleCustomizer; - /** - * Applies the "equals" validation rule to the property. - * null, undefined and empty-string values are considered valid. - */ - equals(expectedValue: TValue): FluentRuleCustomizer; -} -/** - * Part of the fluent rule API. Enables applying rules to properties and objects. - */ -export declare class FluentRules { - private fluentEnsure; - private parsers; - private property; - static customRules: { - [name: string]: { - condition: (value: any, object?: any, ...fluentArgs: any[]) => boolean | Promise; - argsToConfig?: (...args: any[]) => any; - }; - }; - /** - * Current rule sequence number. Used to postpone evaluation of rules until rules - * with lower sequence number have successfully validated. The "then" fluent API method - * manages this property, there's usually no need to set it directly. - */ - sequence: number; - constructor(fluentEnsure: FluentEnsure, parsers: Parsers, property: RuleProperty); - /** - * Sets the display name of the ensured property. - */ - displayName(name: string | ValidationDisplayNameAccessor | null): this; - /** - * Applies an ad-hoc rule function to the ensured property or object. - * @param condition The function to validate the rule. - * Will be called with two arguments, the property value and the object. - * Should return a boolean or a Promise that resolves to a boolean. - */ - satisfies(condition: (value: TValue, object?: TObject) => boolean | Promise, config?: object): FluentRuleCustomizer; - /** - * Applies a rule by name. - * @param name The name of the custom or standard rule. - * @param args The rule's arguments. - */ - satisfiesRule(name: string, ...args: any[]): FluentRuleCustomizer; - /** - * Applies the "required" rule to the property. - * The value cannot be null, undefined or whitespace. - */ - required(): FluentRuleCustomizer; - /** - * Applies the "matches" rule to the property. - * Value must match the specified regular expression. - * null, undefined and empty-string values are considered valid. - */ - matches(regex: RegExp): FluentRuleCustomizer; - /** - * Applies the "email" rule to the property. - * null, undefined and empty-string values are considered valid. - */ - email(): FluentRuleCustomizer; - /** - * Applies the "minLength" STRING validation rule to the property. - * null, undefined and empty-string values are considered valid. - */ - minLength(length: number): FluentRuleCustomizer; - /** - * Applies the "maxLength" STRING validation rule to the property. - * null, undefined and empty-string values are considered valid. - */ - maxLength(length: number): FluentRuleCustomizer; - /** - * Applies the "minItems" ARRAY validation rule to the property. - * null and undefined values are considered valid. - */ - minItems(count: number): FluentRuleCustomizer; - /** - * Applies the "maxItems" ARRAY validation rule to the property. - * null and undefined values are considered valid. - */ - maxItems(count: number): FluentRuleCustomizer; - /** - * Applies the "equals" validation rule to the property. - * null and undefined values are considered valid. - */ - equals(expectedValue: TValue): FluentRuleCustomizer; -} -/** - * Part of the fluent rule API. Enables targeting properties and objects with rules. - */ -export declare class FluentEnsure { - private parsers; - /** - * Rules that have been defined using the fluent API. - */ - rules: Rule[][]; - constructor(parsers: Parsers); - /** - * Target a property with validation rules. - * @param property The property to target. Can be the property name or a property accessor - * function. - */ - ensure(property: string | number | PropertyAccessor): FluentRules; - /** - * Targets an object with validation rules. - */ - ensureObject(): FluentRules; - /** - * Applies the rules to a class or object, making them discoverable by the StandardValidator. - * @param target A class or object. - */ - on(target: any): this; - private assertInitialized; - private mergeRules; -} -/** - * Fluent rule definition API. - */ -export declare class ValidationRules { - private static parsers; - static initialize(messageParser: ValidationMessageParser, propertyParser: PropertyAccessorParser): void; - /** - * Target a property with validation rules. - * @param property The property to target. Can be the property name or a property accessor function. - */ - static ensure(property: string | number | PropertyAccessor): FluentRules; - /** - * Targets an object with validation rules. - */ - static ensureObject(): FluentRules; - /** - * Defines a custom rule. - * @param name The name of the custom rule. Also serves as the message key. - * @param condition The rule function. - * @param message The message expression - * @param argsToConfig A function that maps the rule's arguments to a "config" - * object that can be used when evaluating the message expression. - */ - static customRule(name: string, condition: (value: any, object?: any, ...args: any[]) => boolean | Promise, message: string, argsToConfig?: (...args: any[]) => any): void; - /** - * Returns rules with the matching tag. - * @param rules The rules to search. - * @param tag The tag to search for. - */ - static taggedRules(rules: Rule[][], tag: string): Rule[][]; - /** - * Returns rules that have no tag. - * @param rules The rules to search. - */ - static untaggedRules(rules: Rule[][]): Rule[][]; - /** - * Removes the rules from a class or object. - * @param target A class or object. - */ - static off(target: any): void; -} -export interface Parsers { - message: ValidationMessageParser; - property: PropertyAccessorParser; -} diff --git a/dist/types/property-accessor-parser.d.ts b/dist/types/property-accessor-parser.d.ts deleted file mode 100644 index e5a4ee49..00000000 --- a/dist/types/property-accessor-parser.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Parser } from 'aurelia-binding'; -export declare type PropertyAccessor = (object: TObject) => TValue; -export declare class PropertyAccessorParser { - private parser; - static inject: (typeof Parser)[]; - constructor(parser: Parser); - parse(property: string | number | PropertyAccessor): string | number; -} -export declare function getAccessorExpression(fn: string): string; diff --git a/dist/types/property-info.d.ts b/dist/types/property-info.d.ts deleted file mode 100644 index 3e3e5661..00000000 --- a/dist/types/property-info.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Expression, Scope } from 'aurelia-binding'; -/** - * Retrieves the object and property name for the specified expression. - * @param expression The expression - * @param source The scope - */ -export declare function getPropertyInfo(expression: Expression, source: Scope): { - object: object; - propertyName: string; -} | null; diff --git a/dist/types/util.d.ts b/dist/types/util.d.ts deleted file mode 100644 index 0fb2c86c..00000000 --- a/dist/types/util.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -export declare function isString(value: any): boolean; -export declare function isNumber(value: any): boolean; diff --git a/dist/types/validate-binding-behavior-base.d.ts b/dist/types/validate-binding-behavior-base.d.ts deleted file mode 100644 index 0248159c..00000000 --- a/dist/types/validate-binding-behavior-base.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { TaskQueue } from 'aurelia-task-queue'; -import { ValidationController } from './validation-controller'; -import { validateTrigger } from './validate-trigger'; -/** - * Binding behavior. Indicates the bound property should be validated. - */ -export declare abstract class ValidateBindingBehaviorBase { - private taskQueue; - constructor(taskQueue: TaskQueue); - protected abstract getValidateTrigger(controller: ValidationController): validateTrigger; - bind(binding: any, source: any, rulesOrController?: ValidationController | any, rules?: any): void; - unbind(binding: any): void; -} diff --git a/dist/types/validate-binding-behavior.d.ts b/dist/types/validate-binding-behavior.d.ts deleted file mode 100644 index 90f40d9d..00000000 --- a/dist/types/validate-binding-behavior.d.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { TaskQueue } from 'aurelia-task-queue'; -import { ValidationController } from './validation-controller'; -import { validateTrigger } from './validate-trigger'; -import { ValidateBindingBehaviorBase } from './validate-binding-behavior-base'; -/** - * Binding behavior. Indicates the bound property should be validated - * when the validate trigger specified by the associated controller's - * validateTrigger property occurs. - */ -export declare class ValidateBindingBehavior extends ValidateBindingBehaviorBase { - static inject: (typeof TaskQueue)[]; - getValidateTrigger(controller: ValidationController): validateTrigger; -} -/** - * Binding behavior. Indicates the bound property will be validated - * manually, by calling controller.validate(). No automatic validation - * triggered by data-entry or blur will occur. - */ -export declare class ValidateManuallyBindingBehavior extends ValidateBindingBehaviorBase { - static inject: (typeof TaskQueue)[]; - getValidateTrigger(): validateTrigger; -} -/** - * Binding behavior. Indicates the bound property should be validated - * when the associated element blurs. - */ -export declare class ValidateOnBlurBindingBehavior extends ValidateBindingBehaviorBase { - static inject: (typeof TaskQueue)[]; - getValidateTrigger(): validateTrigger; -} -/** - * Binding behavior. Indicates the bound property should be validated - * when the associated element is changed by the user, causing a change - * to the model. - */ -export declare class ValidateOnChangeBindingBehavior extends ValidateBindingBehaviorBase { - static inject: (typeof TaskQueue)[]; - getValidateTrigger(): validateTrigger; -} -/** - * Binding behavior. Indicates the bound property should be validated - * when the associated element blurs or is changed by the user, causing - * a change to the model. - */ -export declare class ValidateOnChangeOrBlurBindingBehavior extends ValidateBindingBehaviorBase { - static inject: (typeof TaskQueue)[]; - getValidateTrigger(): validateTrigger; -} diff --git a/dist/types/validate-event.d.ts b/dist/types/validate-event.d.ts deleted file mode 100644 index c32fb2fe..00000000 --- a/dist/types/validate-event.d.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { ValidateResult } from './validate-result'; -import { ValidateInstruction } from './validate-instruction'; -import { ControllerValidateResult } from './controller-validate-result'; -export declare class ValidateEvent { - /** - * The type of validate event. Either "validate" or "reset". - */ - readonly type: 'validate' | 'reset'; - /** - * The controller's current array of errors. For an array containing both - * failed rules and passed rules, use the "results" property. - */ - readonly errors: ValidateResult[]; - /** - * The controller's current array of validate results. This - * includes both passed rules and failed rules. For an array of only failed rules, - * use the "errors" property. - */ - readonly results: ValidateResult[]; - /** - * The instruction passed to the "validate" or "reset" event. Will be null when - * the controller's validate/reset method was called with no instruction argument. - */ - readonly instruction: ValidateInstruction | null; - /** - * In events with type === "validate", this property will contain the result - * of validating the instruction (see "instruction" property). Use the controllerValidateResult - * to access the validate results specific to the call to "validate" - * (as opposed to using the "results" and "errors" properties to access the controller's entire - * set of results/errors). - */ - readonly controllerValidateResult: ControllerValidateResult | null; - constructor( - /** - * The type of validate event. Either "validate" or "reset". - */ - type: 'validate' | 'reset', - /** - * The controller's current array of errors. For an array containing both - * failed rules and passed rules, use the "results" property. - */ - errors: ValidateResult[], - /** - * The controller's current array of validate results. This - * includes both passed rules and failed rules. For an array of only failed rules, - * use the "errors" property. - */ - results: ValidateResult[], - /** - * The instruction passed to the "validate" or "reset" event. Will be null when - * the controller's validate/reset method was called with no instruction argument. - */ - instruction: ValidateInstruction | null, - /** - * In events with type === "validate", this property will contain the result - * of validating the instruction (see "instruction" property). Use the controllerValidateResult - * to access the validate results specific to the call to "validate" - * (as opposed to using the "results" and "errors" properties to access the controller's entire - * set of results/errors). - */ - controllerValidateResult: ControllerValidateResult | null); -} diff --git a/dist/types/validate-instruction.d.ts b/dist/types/validate-instruction.d.ts deleted file mode 100644 index 114c2d24..00000000 --- a/dist/types/validate-instruction.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Instructions for the validation controller's validate method. - */ -export interface ValidateInstruction { - /** - * The object to validate. - */ - object: any; - /** - * The property to validate. Optional. - */ - propertyName?: any; - /** - * The rules to validate. Optional. - */ - rules?: any; -} diff --git a/dist/types/validate-result.d.ts b/dist/types/validate-result.d.ts deleted file mode 100644 index 5e9fdf9d..00000000 --- a/dist/types/validate-result.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** - * The result of validating an individual validation rule. - */ -export declare class ValidateResult { - rule: any; - object: any; - propertyName: string | number | null; - valid: boolean; - message: string | null; - private static nextId; - /** - * A number that uniquely identifies the result instance. - */ - id: number; - /** - * @param rule The rule associated with the result. Validator implementation specific. - * @param object The object that was validated. - * @param propertyName The name of the property that was validated. - * @param error The error, if the result is a validation error. - */ - constructor(rule: any, object: any, propertyName: string | number | null, valid: boolean, message?: string | null); - toString(): string | null; -} diff --git a/dist/types/validate-trigger.d.ts b/dist/types/validate-trigger.d.ts deleted file mode 100644 index f8f4a450..00000000 --- a/dist/types/validate-trigger.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Validation triggers. - */ -export declare enum validateTrigger { - /** - * Manual validation. Use the controller's `validate()` and `reset()` methods - * to validate all bindings. - */ - manual = 0, - /** - * Validate the binding when the binding's target element fires a DOM "blur" event. - */ - blur = 1, - /** - * Validate the binding when it updates the model due to a change in the view. - */ - change = 2, - /** - * Validate the binding when the binding's target element fires a DOM "blur" event and - * when it updates the model due to a change in the view. - */ - changeOrBlur = 3 -} diff --git a/dist/types/validation-controller-factory.d.ts b/dist/types/validation-controller-factory.d.ts deleted file mode 100644 index b1589666..00000000 --- a/dist/types/validation-controller-factory.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Container } from 'aurelia-dependency-injection'; -import { ValidationController } from './validation-controller'; -import { Validator } from './validator'; -/** - * Creates ValidationController instances. - */ -export declare class ValidationControllerFactory { - private container; - static get(container: Container): ValidationControllerFactory; - constructor(container: Container); - /** - * Creates a new controller instance. - */ - create(validator?: Validator): ValidationController; - /** - * Creates a new controller and registers it in the current element's container so that it's - * available to the validate binding behavior and renderers. - */ - createForCurrentScope(validator?: Validator): ValidationController; -} diff --git a/dist/types/validation-controller.d.ts b/dist/types/validation-controller.d.ts deleted file mode 100644 index 17433d5c..00000000 --- a/dist/types/validation-controller.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -import { Binding } from 'aurelia-binding'; -import { Validator } from './validator'; -import { validateTrigger } from './validate-trigger'; -import { ValidationRenderer } from './validation-renderer'; -import { ValidateResult } from './validate-result'; -import { ValidateInstruction } from './validate-instruction'; -import { ControllerValidateResult } from './controller-validate-result'; -import { PropertyAccessorParser, PropertyAccessor } from './property-accessor-parser'; -import { ValidateEvent } from './validate-event'; -/** - * Orchestrates validation. - * Manages a set of bindings, renderers and objects. - * Exposes the current list of validation results for binding purposes. - */ -export declare class ValidationController { - private validator; - private propertyParser; - static inject: (typeof Validator | typeof PropertyAccessorParser)[]; - private bindings; - private renderers; - /** - * Validation results that have been rendered by the controller. - */ - private results; - /** - * Validation errors that have been rendered by the controller. - */ - errors: ValidateResult[]; - /** - * Whether the controller is currently validating. - */ - validating: boolean; - private elements; - private objects; - /** - * The trigger that will invoke automatic validation of a property used in a binding. - */ - validateTrigger: validateTrigger; - private finishValidating; - private eventCallbacks; - constructor(validator: Validator, propertyParser: PropertyAccessorParser); - /** - * Subscribe to controller validate and reset events. These events occur when the - * controller's "validate"" and "reset" methods are called. - * @param callback The callback to be invoked when the controller validates or resets. - */ - subscribe(callback: (event: ValidateEvent) => void): { - dispose: () => void; - }; - /** - * Adds an object to the set of objects that should be validated when validate is called. - * @param object The object. - * @param rules Optional. The rules. If rules aren't supplied the Validator implementation will lookup the rules. - */ - addObject(object: any, rules?: any): void; - /** - * Removes an object from the set of objects that should be validated when validate is called. - * @param object The object. - */ - removeObject(object: any): void; - /** - * Adds and renders an error. - */ - addError(message: string, object: TObject, propertyName?: string | PropertyAccessor | null): ValidateResult; - /** - * Removes and unrenders an error. - */ - removeError(result: ValidateResult): void; - /** - * Adds a renderer. - * @param renderer The renderer. - */ - addRenderer(renderer: ValidationRenderer): void; - /** - * Removes a renderer. - * @param renderer The renderer. - */ - removeRenderer(renderer: ValidationRenderer): void; - /** - * Registers a binding with the controller. - * @param binding The binding instance. - * @param target The DOM element. - * @param rules (optional) rules associated with the binding. Validator implementation specific. - */ - registerBinding(binding: Binding, target: Element, rules?: any): void; - /** - * Unregisters a binding with the controller. - * @param binding The binding instance. - */ - unregisterBinding(binding: Binding): void; - /** - * Interprets the instruction and returns a predicate that will identify - * relevant results in the list of rendered validation results. - */ - private getInstructionPredicate; - /** - * Validates and renders results. - * @param instruction Optional. Instructions on what to validate. If undefined, all - * objects and bindings will be validated. - */ - validate(instruction?: ValidateInstruction): Promise; - /** - * Resets any rendered validation results (unrenders). - * @param instruction Optional. Instructions on what to reset. If unspecified all rendered results - * will be unrendered. - */ - reset(instruction?: ValidateInstruction): void; - /** - * Gets the elements associated with an object and propertyName (if any). - */ - private getAssociatedElements; - private processResultDelta; - /** - * Validates the property associated with a binding. - */ - validateBinding(binding: Binding): void; - /** - * Resets the results for a property associated with a binding. - */ - resetBinding(binding: Binding): void; - /** - * Changes the controller's validateTrigger. - * @param newTrigger The new validateTrigger - */ - changeTrigger(newTrigger: validateTrigger): void; - /** - * Revalidates the controller's current set of errors. - */ - revalidateErrors(): void; - private invokeCallbacks; -} diff --git a/dist/types/validation-errors-custom-attribute.d.ts b/dist/types/validation-errors-custom-attribute.d.ts deleted file mode 100644 index bebb1a51..00000000 --- a/dist/types/validation-errors-custom-attribute.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Lazy } from 'aurelia-dependency-injection'; -import { ValidationController } from './validation-controller'; -import { ValidateResult } from './validate-result'; -import { ValidationRenderer, RenderInstruction } from './validation-renderer'; -export interface RenderedError { - error: ValidateResult; - targets: Element[]; -} -export declare class ValidationErrorsCustomAttribute implements ValidationRenderer { - private boundaryElement; - private controllerAccessor; - static inject(): ({ - new (): Element; - prototype: Element; - } | Lazy)[]; - controller: ValidationController | null; - errors: RenderedError[]; - private errorsInternal; - constructor(boundaryElement: Element, controllerAccessor: () => ValidationController); - sort(): void; - interestingElements(elements: Element[]): Element[]; - render(instruction: RenderInstruction): void; - bind(): void; - unbind(): void; -} diff --git a/dist/types/validation-renderer-custom-attribute.d.ts b/dist/types/validation-renderer-custom-attribute.d.ts deleted file mode 100644 index 2fc48aa8..00000000 --- a/dist/types/validation-renderer-custom-attribute.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -export declare class ValidationRendererCustomAttribute { - private container; - private controller; - private value; - private renderer; - created(view: any): void; - bind(): void; - unbind(): void; -} diff --git a/dist/types/validation-renderer.d.ts b/dist/types/validation-renderer.d.ts deleted file mode 100644 index 71c62752..00000000 --- a/dist/types/validation-renderer.d.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { ValidateResult } from './validate-result'; -/** - * A result to render (or unrender) and the associated elements (if any) - */ -export interface ResultInstruction { - /** - * The validation result. - */ - result: ValidateResult; - /** - * The associated elements (if any). - */ - elements: Element[]; -} -/** - * Defines which validation results to render and which validation results to unrender. - */ -export interface RenderInstruction { - /** - * The "kind" of render instruction. Either 'validate' or 'reset'. - */ - kind: 'validate' | 'reset'; - /** - * The results to render. - */ - render: ResultInstruction[]; - /** - * The results to unrender. - */ - unrender: ResultInstruction[]; -} -/** - * Renders validation results. - */ -export interface ValidationRenderer { - /** - * Render the validation results. - * @param instruction The render instruction. Defines which results to render and which - * results to unrender. - */ - render(instruction: RenderInstruction): void; -} diff --git a/dist/types/validator.d.ts b/dist/types/validator.d.ts deleted file mode 100644 index a9f49985..00000000 --- a/dist/types/validator.d.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { ValidateResult } from './validate-result'; -/** - * Validates objects and properties. - */ -export declare abstract class Validator { - /** - * Validates the specified property. - * @param object The object to validate. - * @param propertyName The name of the property to validate. - * @param rules Optional. If unspecified, the implementation should lookup the rules for the - * specified object. This may not be possible for all implementations of this interface. - */ - abstract validateProperty(object: any, propertyName: string, rules?: any): Promise; - /** - * Validates all rules for specified object and it's properties. - * @param object The object to validate. - * @param rules Optional. If unspecified, the implementation should lookup the rules for the - * specified object. This may not be possible for all implementations of this interface. - */ - abstract validateObject(object: any, rules?: any): Promise; - /** - * Determines whether a rule exists in a set of rules. - * @param rules The rules to search. - * @parem rule The rule to find. - */ - abstract ruleExists(rules: any, rule: any): boolean; -} diff --git a/doc/CHANGELOG.md b/doc/CHANGELOG.md index dd1f59c6..9d04b5b4 100644 --- a/doc/CHANGELOG.md +++ b/doc/CHANGELOG.md @@ -1,3 +1,13 @@ + +## [1.3.1](https://github.com/aurelia/validation/compare/1.3.0...1.3.1) (2018-11-16) + + +### Bug Fixes + +* **CustomAttribute:** import customAttribute from aurelia-templating ([08c2f45](https://github.com/aurelia/validation/commit/08c2f45)), closes [#507](https://github.com/aurelia/validation/issues/507) + + + # [1.3.0](https://github.com/aurelia/validation/compare/1.2.3...1.3.0) (2018-10-30) diff --git a/package.json b/package.json index 5f9e1b91..e08d69d6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aurelia-validation", - "version": "1.3.0", + "version": "1.3.1", "description": "Validation for Aurelia applications", "keywords": [ "aurelia",