cmichaelgraham / aurelia-typescript

A starter kit for working with the Aurelia TypeScript type definitions
MIT License
144 stars 52 forks source link

Proposal to enable extending validation plugin type definition with custom validation rules #39

Closed luboid closed 3 years ago

luboid commented 9 years ago

Hi,

If I create custom validation rule, like this

export class CustomValidationRule extends ValidationRule {
  constructor(threshold) {
    super(
        ...
    );
  }
}

ValidationGroupBuilder.prototype['prcbIsNumber'] = function () {
  return this.passesRule(new CustomValidationRule());
}

ValidationGroup.prototype['prcbIsNumber'] = function () {
  return this.builder.prcbIsNumber();
};

it will will be nice we can call it with statement like this

    this.validation.on(this.country, undefined).ensure('CSTLCNTRY').prcbIsNumber()
    // but this is not possible now 
    // I need to cast it to any to be able to do that
    (<any>this.validation.on(this.country, undefined)).ensure('CSTLCNTRY').prcbIsNumber()

there is solution but I need to rewrite aurelia-validation.d.ts every time when it is published

declare module 'aurelia-validation/validation/validation-group' {
    /**
     * Encapsulates validation rules and their current validation state for a given subject
     * @class ValidationGroup
     * @constructor
     */
    export interface IValidationGroup {
        // ...

            /**
         * Instantiates a new {ValidationGroup}
         * @param subject The subject to evaluate
         * @param observerLocator The observerLocator used to monitor changes on the subject
         * @param config The configuration
         */
        new(subject: any, observerLocator: any, config: any);

        // ...

        /**
         * Specifies that the execution of the previous validation rule should use the specified error message if it fails
         * @param message either a static string or a function that takes two arguments: newValue (the value that has been evaluated) and threshold.
         * @returns {ValidationGroup} returns this ValidationGroup, to enable fluent API
         */
        withMessage(message: any): any;
    }

    export var ValidationGroup : IValidationGroup;
}

then I can define my validation.d.ts

declare module "aurelia-validation/validation/validation-group" {
  export interface IValidationGroup {
    prcbIsNumber():IValidationGroup
  }
}

links:

http://stackoverflow.com/questions/23217334/how-do-i-extend-a-typescript-class-definition-in-a-separate-definition-file

https://github.com/Microsoft/TypeScript/issues/819