diff --git a/validatorjs/validatorjs-tests.ts b/validatorjs/validatorjs-tests.ts new file mode 100644 index 0000000000..1c2096c03b --- /dev/null +++ b/validatorjs/validatorjs-tests.ts @@ -0,0 +1,40 @@ +// ValidatorJS Test +// ================================================================================ +/// + +// Imports +// -------------------------------------------------------------------------------- +import * as Validator from "validatorjs" + +var rules: any = { + foo: "required" +} + +var data: any = { + foo: "bar" +} + +var validator: ValidatorJS.Validator = new Validator(data, rules) + +var passes: boolean = validator.passes() as boolean +validator.passes(() => {}) + +var fails: boolean = validator.fails() as boolean +validator.fails(() => {}) + +var check: boolean = validator.check() + +var errors: ValidatorJS.Errors = validator.errors +var all: ValidatorJS.ValidationErrors = errors.all() +var error: Array = errors.get("foo") +var first: string | boolean = errors.first("foo") +var has: boolean = errors.has("foo") + +Validator.setMessages("en", {}) +var messages: ValidatorJS.ErrorMessages = Validator.getMessages("en") +Validator.useLang("en") +var lang: string = Validator.getDefaultLang() +Validator.setAttributeFormatter((attributes: any) => ({})) +Validator.stopOnError(true) +Validator.register("custom", () => {}, "error.custom") +Validator.registerAsync("custom", () => {}, "error.custom") diff --git a/validatorjs/validatorjs.d.ts b/validatorjs/validatorjs.d.ts new file mode 100644 index 0000000000..50df271a94 --- /dev/null +++ b/validatorjs/validatorjs.d.ts @@ -0,0 +1,72 @@ +// Type definitions for validatorjs v3.7.0 +// Project: https://github.com/skaterdav85/validatorjs +// Definitions by: Karol Janyst +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace ValidatorJS { + + interface ParsedRule { + attribute: Array<{ name: string, value: any }> + } + + interface ValidationErrors { + [field: string]: string + } + + interface ErrorMessages { + [key: string]: string + } + + interface AttributeNames { + [attribute: string]: string + } + + type AttributeFormatter = (attribute: any) => any + + interface Errors { + errors : ValidationErrors + add (attribute: string, message: string): void + get (attribute: string): Array + first (attribute: string): string | boolean + all (): ValidationErrors + has (attribute: string): boolean + } + + interface ValidatorStatic { + new (data: A, rules: any, customMessages?: ErrorMessages): Validator + setMessages (lang: string, messages: ErrorMessages): any + getMessages (lang: string): ErrorMessages + useLang (lang: string): void + getDefaultLang (): string + setAttributeFormatter (func: AttributeFormatter): void + stopOnError (attributes: boolean | Array): void + register (name: string, fn: Function, message: string): void + registerAsync (name: string, fn: Function, message: string): void + } + + interface Validator { + lang: string + input: A + messages: ErrorMessages + errors: Errors + errorCount: number + hasAsync: boolean + rules: any + numericRules: Array + attributeFormatter: AttributeFormatter + check (): boolean + checkAsync (passes?: Function, fails?: Function): void + setAttributeNames (attributes: AttributeNames): void + setAttributeFormatter (func: AttributeFormatter): void + getRule (name: string): Function + stopOnError (passes?: Function): boolean | void + passes (passes?: Function): boolean | void + fails (fails?: Function): boolean | void + } + +} + +declare module "validatorjs" { + const Validator: ValidatorJS.ValidatorStatic + export = Validator +}