Add definitions for validatorjs (#12123)

This commit is contained in:
Karol Janyst
2016-10-25 19:42:14 +09:00
committed by Masahiro Wakame
parent 62c6882723
commit 19490da3d7
2 changed files with 112 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
// ValidatorJS Test
// ================================================================================
/// <reference path="validatorjs.d.ts"/>
// Imports
// --------------------------------------------------------------------------------
import * as Validator from "validatorjs"
var rules: any = {
foo: "required"
}
var data: any = {
foo: "bar"
}
var validator: ValidatorJS.Validator<any> = 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<string> = 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")
+72
View File
@@ -0,0 +1,72 @@
// Type definitions for validatorjs v3.7.0
// Project: https://github.com/skaterdav85/validatorjs
// Definitions by: Karol Janyst <https://github.com/LKay>
// 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<string>
first (attribute: string): string | boolean
all (): ValidationErrors
has (attribute: string): boolean
}
interface ValidatorStatic {
new <A>(data: A, rules: any, customMessages?: ErrorMessages): Validator<A>
setMessages (lang: string, messages: ErrorMessages): any
getMessages (lang: string): ErrorMessages
useLang (lang: string): void
getDefaultLang (): string
setAttributeFormatter (func: AttributeFormatter): void
stopOnError (attributes: boolean | Array<string>): void
register (name: string, fn: Function, message: string): void
registerAsync (name: string, fn: Function, message: string): void
}
interface Validator<A> {
lang: string
input: A
messages: ErrorMessages
errors: Errors
errorCount: number
hasAsync: boolean
rules: any
numericRules: Array<string>
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
}