Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
luboid opened this issue Jun 16, 2015 · 0 comments

Comments

@luboid
Copy link

luboid commented Jun 16, 2015

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

microsoft/TypeScript#819

@luboid luboid closed this as completed Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant