22
33[ ![ Build Status] ( https://travis-ci.org/Flcwl/validater.svg?branch=master )] ( https://travis-ci.org/github/Flcwl/validater )
44[ ![ GitHub license] ( https://img.shields.io/badge/license-MIT-blue.svg )] ( https://github.com/Flcwl/validater/blob/master/LICENSE )
5- [ ![ npm version] ( https://img.shields.io/npm/v/validater.svg?style=flat )] ( https://www.npmjs.com/package/validater )
5+ [ ![ npm version] ( https://img.shields.io/npm/v/@flcwly/ validater.svg?style=flat )] ( https://www.npmjs.com/package/validater )
66
77> A excellent and useful JavaScript validation library, it works better with the Validator.js.
88
9+ ---
10+
911## Getting Started
1012
1113### Installation
@@ -51,6 +53,8 @@ const v = new Validater([
5153const errorMsg = v .validateOne (' abc1234567890' ) // "Please enter the correct name."
5254```
5355
56+ You can define the order of validators by array.
57+
5458or using ES6 Module:
5559
5660``` js
@@ -59,7 +63,7 @@ import Validater from '@flcwly/validater'
5963
6064The Validater constructor accepts two parameters: ` new Validater(rules, options) ` .
6165
62- - rules: ValidaterRule[ ]
66+ - ` rules: ValidaterRule[] `
6367
6468A array that type is ` ValidaterRule ` .
6569
@@ -69,7 +73,7 @@ strategy: any // extend strategy for validation
6973message?: string // rule error message
7074```
7175
72- - options
76+ - ` options `
7377
7478``` js
7579type: ' string' | ' boolean' | ' number' // transform type before validating, default is `string`
@@ -104,7 +108,11 @@ const v = new Validater([
104108const errorMsg = v .validateOne (' ' ) // "The value is incorrect"
105109```
106110
107- The message string's relationship for overriding is ` message > defaultRuleMessage > defaultMessage ` .
111+ The message string's relationship for overriding is:
112+
113+ ``` console
114+ message > defaultRuleMessage > defaultMessage
115+ ```
108116
109117And the message string is parsed and replaced with the value by \$ 0.
110118
@@ -119,6 +127,56 @@ const v = new Validater([
119127const errorMsg = v .validateOne (' 1a1' ) // "1a1 error"
120128```
121129
130+ ---
131+
132+ ## Instance Functions
133+
134+ - ` addRules: (rules?: ValidaterRule[] | undefined) => void; `
135+
136+ Traverse rules to register validators generated based on rules.
137+
138+ - ` validate: (value: unknown, ruleName?: string | undefined) => string | void; `
139+
140+ Validate a value with special ruleName.
141+
142+ - ` validateOne: (value: unknown) => string | void; `
143+
144+ Validate a value base on array order starts at 0.
145+
146+ - ` validateAll: (value: unknown) => this; `
147+
148+ Validate a value for all rules, then will return ` this ` .
149+
150+ - ` hasError: () => boolean; `
151+
152+ Check if the error exists, return ` true ` when error.
153+
154+ - ` getError: (ruleName?: string | undefined) => any; `
155+
156+ Get error to special ruleName, return all error as object when ` ruleName === undefined ` .
157+
158+ ---
159+
160+ ## with validator.js
161+
162+ It works better with the [ Validator.js] ( https://github.com/validatorjs/validator.js ) .
163+
164+ ``` js
165+ import Validator from ' validator'
166+
167+ Validater .extend (' isEmail' , Validator .isEmail )
168+
169+ const v = new Validater ([
170+ {
171+ name: ' isEmail' ,
172+ message: ' "$0" error' ,
173+ },
174+ ])
175+ const errorMsg = v .validateOne (' @mail.com' ) // ""@mail.com" error"
176+ ```
177+
178+ ---
179+
122180## Tests
123181
124182You can find all cases in ` files:/test/*.spec.js ` , And testing Using below script.
0 commit comments