From c51cfd5670e04deb30d2e7866293dc210fb15b2f Mon Sep 17 00:00:00 2001 From: rsamec Date: Sun, 27 Jul 2014 09:05:21 +0200 Subject: [PATCH] node-form typescript definition file added --- CONTRIBUTORS.md | 1 + node-form/node-form-tests.ts | 31 ++ node-form/node-form.d.ts | 681 +++++++++++++++++++++++++++++++++++ 3 files changed, 713 insertions(+) create mode 100644 node-form/node-form-tests.ts create mode 100644 node-form/node-form.d.ts diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 1eb31acbbd..35e2980f7b 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -251,6 +251,7 @@ All definitions files include a header with the author and editors, so at some p * [Node.js](http://nodejs.org/) (from TypeScript samples) * [node_redis](https://github.com/mranney/node_redis) (by [Boris Yankov](https://github.com/borisyankov)) * [node-ffi](https://github.com/rbranson/node-ffi) (by [Paul Loyd](https://github.com/loyd)) +* [node-form] (https://github.com/rsamec/form) (by [Roman Samec] (https://github.com/rsamec)) * [node-git](https://github.com/christkv/node-git) (by [vvakame](https://github.com/vvakame)) * [nodeunit](https://github.com/caolan/nodeunit) (by [Jeff Goddard](https://github.com/jedigo)) * [node_zeromq](https://github.com/JustinTulloss/zeromq.node) (by [Dave McKeown](https://github.com/davemckeown)) diff --git a/node-form/node-form-tests.ts b/node-form/node-form-tests.ts new file mode 100644 index 0000000000..776014b681 --- /dev/null +++ b/node-form/node-form-tests.ts @@ -0,0 +1,31 @@ +/// +/// +/// +/// + + +export interface IPerson{ + Checked:boolean; + FirstName:string; + LastName:string; + Email:string; +} + +//create custom composite validator +var personValidator = new Validation.AbstractValidator(); + +//create field validators +var required = new Validation.RequiredValidator(); +var email = new Validation.EmailValidator(); +var maxLength = new Validation.MaxLengthValidator(); +maxLength.MaxLength = 15; + + +personValidator.RuleFor("FirstName", required); +personValidator.RuleFor("FirstName", maxLength); + +personValidator.RuleFor("LastName", required); +personValidator.RuleFor("LastName", maxLength); + +personValidator.RuleFor("Email", required); +personValidator.RuleFor("Email", email); diff --git a/node-form/node-form.d.ts b/node-form/node-form.d.ts new file mode 100644 index 0000000000..b842368963 --- /dev/null +++ b/node-form/node-form.d.ts @@ -0,0 +1,681 @@ +// Type definitions for node-form v1.0.0 +// Project: https://github.com/rsamec/form +// Definitions by: Roman Samec +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// +/// +declare module Validation { + /** + * It represents a propert y validator for atomic object. + */ + interface IPropertyValidator { + isAcceptable(s: any): boolean; + customMessage? (config: any, args: any): string; + tagName?: string; + } + /** + * It represents a property validator for simple string value. + */ + interface IStringValidator extends IPropertyValidator { + isAcceptable(s: string): boolean; + } + /** + * It represents an async property validator for atomic object. + */ + interface IAsyncPropertyValidator { + isAcceptable(s: any): Q.Promise; + customMessage? (config: any, args: any): string; + isAsync: boolean; + tagName?: string; + } + /** + * It represents an async property validator for simple string value. + */ + interface IAsyncStringPropertyValidator extends IAsyncPropertyValidator { + isAcceptable(s: string): Q.Promise; + } + /** + * It defines compare operators. + */ + enum CompareOperator { + LessThan = 0, + LessThanEqual = 1, + Equal = 2, + NotEqual = 3, + GreaterThanEqual = 4, + GreaterThan = 5, + } + class StringFce { + static format(s: string, args: any): string; + } + class NumberFce { + static GetNegDigits(value: string): number; + } + class LettersOnlyValidator implements IStringValidator { + public isAcceptable(s: string): boolean; + public tagName: string; + } + class ZipCodeValidator implements IStringValidator { + public isAcceptable(s: string): boolean; + public tagName: string; + } + class EmailValidator implements IStringValidator { + public isAcceptable(s: string): boolean; + public tagName: string; + } + class UrlValidator implements IStringValidator { + public isAcceptable(s: string): boolean; + public tagName: string; + } + class RequiredValidator implements IStringValidator { + public isAcceptable(s: string): boolean; + public tagName: string; + } + class DateValidator implements IStringValidator { + public isAcceptable(s: string): boolean; + public tagName: string; + } + class DateISOValidator implements IStringValidator { + public isAcceptable(s: string): boolean; + public tagName: string; + } + class NumberValidator implements IStringValidator { + public isAcceptable(s: string): boolean; + public tagName: string; + } + class DigitValidator implements IStringValidator { + public isAcceptable(s: string): boolean; + public tagName: string; + } + class SignedDigitValidator implements IStringValidator { + public isAcceptable(s: string): boolean; + public tagName: string; + } + class MinLengthValidator implements IStringValidator { + public MinLength: number; + constructor(MinLength?: number); + public isAcceptable(s: string): boolean; + public tagName: string; + } + class MaxLengthValidator implements IStringValidator { + public MaxLength: number; + constructor(MaxLength?: number); + public isAcceptable(s: string): boolean; + public tagName: string; + } + class RangeLengthValidator implements IStringValidator { + public RangeLength: number[]; + constructor(RangeLength?: number[]); + public isAcceptable(s: string): boolean; + public MinLength : number; + public MaxLength : number; + public tagName: string; + } + class MinValidator implements IPropertyValidator { + public Min: number; + constructor(Min?: number); + public isAcceptable(s: any): boolean; + public tagName: string; + } + class MaxValidator implements IPropertyValidator { + public Max: number; + constructor(Max?: number); + public isAcceptable(s: any): boolean; + public tagName: string; + } + class RangeValidator implements IPropertyValidator { + public Range: number[]; + constructor(Range?: number[]); + public isAcceptable(s: any): boolean; + public Min : number; + public Max : number; + public tagName: string; + } + class StepValidator implements IPropertyValidator { + public Step: string; + constructor(Step?: string); + public isAcceptable(s: any): boolean; + public tagName: string; + } + class PatternValidator implements IStringValidator { + public Pattern: string; + constructor(Pattern?: string); + public isAcceptable(s: string): boolean; + public tagName: string; + } + class ContainsValidator implements IAsyncPropertyValidator { + public Options: Q.Promise; + constructor(Options: Q.Promise); + public isAcceptable(s: string): Q.Promise; + public isAsync: boolean; + public tagName: string; + } +} +declare module Validation { + /** + * basic error structure + */ + interface IError { + HasError: boolean; + ErrorMessage: string; + TranslateArgs?: IErrorTranslateArgs; + } + /** + * support for localization of error messages + */ + interface IErrorTranslateArgs { + TranslateId: string; + MessageArgs: any; + } + /** + * It defines conditional function. + */ + interface IOptional { + (): boolean; + } + /** + * It represents the validation result. + */ + interface IValidationFailure extends IError { + IsAsync: boolean; + Error: IError; + } + /** + * This class provides unit of information about error. + * Implements composite design pattern to enable nesting of error information. + */ + interface IValidationResult { + /** + * The name of error collection. + */ + Name: string; + /** + * Add error information to child collection of errors. + * @param validationResult - error information to be added. + */ + Add(validationResult: IValidationResult): void; + /** + * Remove error information from child collection of errors. + * @param index - index of error information to be removed. + */ + Remove(index: number): void; + /** + * Return collections of child errors information. + */ + Children: IValidationResult[]; + /** + * Return true if there is any error. + */ + HasErrors: boolean; + /** + * Return true if there is any error and hasw dirty state. + */ + HasErrorsDirty: boolean; + /** + * Return error message, if there is no error, return empty string. + */ + ErrorMessage: string; + /** + * Return number of errors. + */ + ErrorCount: number; + /** + * It enables to have errors optional. + */ + Optional?: IOptional; + /** + * It enables support for localization of error messages. + */ + TranslateArgs?: IErrorTranslateArgs[]; + } + /** + * + * @ngdoc object + * @name Error + * @module Validation + * + * + * @description + * It represents basic error structure. + */ + class Error implements IError { + public HasError: boolean; + public ErrorMessage: string; + constructor(); + } + /** + * + * @ngdoc object + * @name ValidationFailure + * @module Validation + * + * + * @description + * It represents validation failure. + */ + class ValidationFailure implements IError { + public Error: IError; + public IsAsync: boolean; + constructor(Error: IError, IsAsync: boolean); + public HasError : boolean; + public ErrorMessage : string; + public TranslateArgs : IErrorTranslateArgs; + } + /** + * + * @ngdoc object + * @name ValidationResult + * @module Validation + * + * + * @description + * It represents simple abstract error object. + */ + class ValidationResult implements IValidationResult { + public Name: string; + constructor(Name: string); + public IsDirty: boolean; + public Children : IValidationResult[]; + public Add(error: IValidationResult): void; + public Remove(index: number): void; + public Optional: IOptional; + public TranslateArgs: IErrorTranslateArgs[]; + public HasErrorsDirty : boolean; + public HasErrors : boolean; + public ErrorCount : number; + public ErrorMessage : string; + } + /** + * + * @ngdoc object + * @name CompositeValidationResult + * @module Validation + * + * + * @description + * It represents composite error object. + */ + class CompositeValidationResult implements IValidationResult { + public Name: string; + public Children: IValidationResult[]; + constructor(Name: string); + public Optional: IOptional; + public AddFirst(error: IValidationResult): void; + public Add(error: IValidationResult): void; + public Remove(index: number): void; + public HasErrorsDirty : boolean; + public HasErrors : boolean; + public ErrorCount : number; + public ErrorMessage : string; + public TranslateArgs : IErrorTranslateArgs[]; + public LogErrors(headerMessage?: string): void; + public Errors : { + [name: string]: IValidationResult; + }; + private FlattenErros; + public SetDirty(): void; + public SetPristine(): void; + private SetDirtyEx(node, dirty); + private flattenErrors(node, errorCollection); + private traverse(node, indent); + } +} +declare module Validation { + /** + * @ngdoc module + * @name Validation + * + * + * @description + * # Validation (core module) + * The module itself contains the essential components for an validation engine to function. The table below + * lists a high level breakdown of each of the components (object, functions) available within this core module. + * + *
+ */ + /** + * It defines validation function. + */ + interface IValidate { + (args: IError): void; + } + /** + * It represents named validation function. + */ + interface IValidatorFce { + Name: string; + ValidationFce: IValidate; + } + /** + * This class represents custom validator. + */ + interface IValidator { + Validate(context: any): boolean; + Error: IError; + } + /** + * It represents abstract validator for type of . + */ + interface IAbstractValidator { + RuleFor(prop: string, validator: IPropertyValidator): any; + ValidationFor(prop: string, validator: IValidatorFce): any; + ValidatorFor(prop: string, validator: IAbstractValidator): any; + /** + * It creates new concrete validation rule and assigned data context to this rule. + * @param name of the rule + * @constructor + */ + CreateRule(name: string): IAbstractValidationRule; + CreateAbstractRule(name: string): IAbstractValidationRule; + CreateAbstractListRule(name: string): IAbstractValidationRule; + /** + * return true if this validation rule is intended for list of items, otherwise true + */ + ForList: boolean; + } + /** + * It represents concrete validation rule for type of . + */ + interface IAbstractValidationRule { + /** + * Performs validation using a validation context and returns a collection of Validation Failures. + */ + Validate(context: T): IValidationResult; + /** + * Performs validation using a validation context and returns a collection of Validation Failures asynchronoulsy. + */ + ValidateAsync(context: T): Q.Promise; + /** + * Performs validation and async validation using a validation context. + */ + ValidateAll(context: T): void; + /** + * Performs validation and async validation using a validation context for a passed field. + */ + ValidateField(context: T, propName: string): void; + /** + * Return validation results. + */ + ValidationResult: IValidationResult; + Rules: { + [name: string]: IPropertyValidationRule; + }; + Validators: { + [name: string]: IValidator; + }; + Children: { + [name: string]: AbstractValidationRule; + }; + } + /** + * It represents property validation rule for type of . + */ + interface IPropertyValidationRule { + /** + *The validators that are grouped under this rule. + */ + Validators: { + [name: string]: any; + }; + /** + * Performs validation using a validation context and returns a collection of Validation Failures. + */ + Validate(context: IValidationContext): IValidationFailure[]; + /** + * Performs validation using a validation context and returns a collection of Validation Failures asynchronoulsy. + */ + ValidateAsync(context: IValidationContext): Q.Promise; + } + /** + * It represents a data context for validation rule. + */ + interface IValidationContext { + /** + * Return current value. + */ + Value: string; + /** + * Return property name for current data context. + */ + Key: string; + /** + * Data context for validation rule. + */ + Data: T; + } + /** + * + * @ngdoc object + * @name AbstractValidator + * @module Validation + * + * + * @description + * It enables to create custom validator for your own abstract object (class) and to assign validation rules to its properties. + * You can assigned these rules + * + * + property validation rules - use _RuleFor_ property + * + property async validation rules - use _RuleFor_ property + * + shared validation rules - use _ValidationFor_ property + * + custom object validator - use _ValidatorFor_ property - enables composition of child custom validators + */ + class AbstractValidator implements IAbstractValidator { + public Validators: { + [name: string]: IPropertyValidator[]; + }; + public AbstractValidators: { + [name: string]: IAbstractValidator; + }; + public ValidationFunctions: { + [name: string]: IValidatorFce[]; + }; + public RuleFor(prop: string, validator: IPropertyValidator): void; + public ValidationFor(prop: string, fce: IValidatorFce): void; + public ValidatorFor(prop: string, validator: IAbstractValidator, forList?: boolean): void; + public CreateAbstractRule(name: string): AbstractValidationRule; + public CreateAbstractListRule(name: string): AbstractListValidationRule; + public CreateRule(name: string): AbstractValidationRule; + /** + * Return true if this validation rule is intended for list of items, otherwise true. + */ + public ForList: boolean; + } + /** + * + * @ngdoc object + * @name AbstractValidationRule + * @module Validation + * + * + * @description + * It represents concreate validator for custom object. It enables to assign validation rules to custom object properties. + */ + class AbstractValidationRule implements IAbstractValidationRule { + public Name: string; + public validator: AbstractValidator; + public ValidationResult: IValidationResult; + public Rules: { + [name: string]: IPropertyValidationRule; + }; + public Validators: { + [name: string]: IValidator; + }; + public Children: { + [name: string]: AbstractValidationRule; + }; + /** + * Return true if this validation rule is intended for list of items, otherwise true. + */ + public ForList: boolean; + constructor(Name: string, validator: AbstractValidator, forList?: boolean); + public addChildren(): void; + public SetOptional(fce: IOptional): void; + private createRuleFor(prop); + /** + * Performs validation using a validation context and returns a collection of Validation Failures. + */ + public Validate(context: T): IValidationResult; + /** + * Performs validation using a validation context and returns a collection of Validation Failures asynchronoulsy. + */ + public ValidateAsync(context: T): Q.Promise; + public ValidateAll(context: T): void; + public ValidateField(context: T, propName: string): void; + } + /** + * + * @ngdoc object + * @name AbstractListValidationRule + * @module Validation + * + * + * @description + * It represents an validator for custom object. It enables to assign rules to custom object properties. + */ + class AbstractListValidationRule extends AbstractValidationRule { + public Name: string; + public validator: AbstractValidator; + constructor(Name: string, validator: AbstractValidator); + /** + * Performs validation using a validation context and returns a collection of Validation Failures. + */ + public Validate(context: any): IValidationResult; + /** + * Performs validation using a validation context and returns a collection of Validation Failures asynchronoulsy. + */ + public ValidateAsync(context: any): Q.Promise; + private getValidationRule(i); + private getIndexedKey(i); + public NotifyListChanged(list: any[]): void; + } + /** + * + * @ngdoc object + * @name ValidationContext + * @module Validation + * + * + * @description + * It represents a data context for validation rule. + */ + class ValidationContext implements IValidationContext { + public Key: string; + public Data: T; + constructor(Key: string, Data: T); + public Value : any; + } + class MessageLocalization { + static customMsg: string; + static defaultMessages: { + "required": string; + "remote": string; + "email": string; + "url": string; + "date": string; + "dateISO": string; + "number": string; + "digits": string; + "signedDigits": string; + "creditcard": string; + "equalTo": string; + "maxlength": string; + "minlength": string; + "rangelength": string; + "range": string; + "max": string; + "min": string; + "step": string; + "contains": string; + "mask": string; + "custom": string; + }; + static ValidationMessages: { + "required": string; + "remote": string; + "email": string; + "url": string; + "date": string; + "dateISO": string; + "number": string; + "digits": string; + "signedDigits": string; + "creditcard": string; + "equalTo": string; + "maxlength": string; + "minlength": string; + "rangelength": string; + "range": string; + "max": string; + "min": string; + "step": string; + "contains": string; + "mask": string; + "custom": string; + }; + static GetValidationMessage(validator: any): string; + } + /** + * + * @ngdoc object + * @name PropertyValidationRule + * @module Validation + * + * + * @description + * It represents a property validation rule. The property has assigned collection of property validators. + */ + class PropertyValidationRule extends ValidationResult implements IPropertyValidationRule { + public Name: string; + public Validators: { + [name: string]: any; + }; + public ValidationFailures: { + [name: string]: IValidationFailure; + }; + constructor(Name: string, validatorsToAdd?: IPropertyValidator[]); + public AddValidator(validator: any): void; + public Errors : IError[]; + public HasErrors : boolean; + public ErrorCount : number; + public ErrorMessage : string; + public TranslateArgs : IErrorTranslateArgs[]; + /** + * Performs validation using a validation context and returns a collection of Validation Failures. + */ + public Validate(context: IValidationContext): IValidationFailure[]; + public ValidateEx(value: any): IValidationFailure[]; + /** + * Performs validation using a validation context and returns a collection of Validation Failures asynchronoulsy. + */ + public ValidateAsync(context: IValidationContext): Q.Promise; + /** + * Performs validation using a validation context and returns a collection of Validation Failures asynchronoulsy. + */ + public ValidateAsyncEx(value: string): Q.Promise; + } + /** + * + * @ngdoc object + * @name Validator + * @module Validation + * + * + * @description + * It represents a custom validator. It enables to define your own shared validation rules + */ + class Validator extends ValidationResult implements IValidator { + public Name: string; + private ValidateFce; + public Error: IError; + constructor(Name: string, ValidateFce: IValidate); + public Optional: IOptional; + public Validate(context: any): boolean; + public HasError : boolean; + public HasErrors : boolean; + public ErrorCount : number; + public ErrorMessage : string; + public TranslateArgs : IErrorTranslateArgs[]; + } +}