Skip to content

Commit faafee9

Browse files
committed
chore: update doc
1 parent 6503e1d commit faafee9

5 files changed

Lines changed: 98 additions & 40 deletions

File tree

‎.eslintrc.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ module.exports = {
22
parser: '@typescript-eslint/parser',
33
extends: ['plugin:@typescript-eslint/recommended'],
44
plugins: ['@typescript-eslint'],
5-
env:{
5+
env: {
66
browser: true,
77
node: true,
88
},
99
rules: {
10-
"@typescript-eslint/ban-ts-comment": "off",
11-
"@typescript-eslint/explicit-module-boundary-types": "off",
12-
"@typescript-eslint/no-explicit-any": "off"
13-
}
10+
'@typescript-eslint/ban-ts-comment': 'off',
11+
'@typescript-eslint/explicit-module-boundary-types': 'off',
12+
'@typescript-eslint/no-explicit-any': 'off',
13+
},
1414
}

‎.travis.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ script:
1616
- codecov
1717
after_success:
1818
- if [ "$TRAVIS_BRANCH" == "master" ] && [ "${TRAVIS_PULL_REQUEST}" == "false" ]; then
19-
npx travis-deploy-once --pro && npm run build
19+
npx travis-deploy-once --pro && npm run build
2020
fi
2121
branches:
2222
except:

‎README.md‎

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
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([
5153
const errorMsg = v.validateOne('abc1234567890') // "Please enter the correct name."
5254
```
5355

56+
You can define the order of validators by array.
57+
5458
or using ES6 Module:
5559

5660
```js
@@ -59,7 +63,7 @@ import Validater from '@flcwly/validater'
5963

6064
The Validater constructor accepts two parameters: `new Validater(rules, options)`.
6165

62-
- rules: ValidaterRule[]
66+
- `rules: ValidaterRule[]`
6367

6468
A array that type is `ValidaterRule`.
6569

@@ -69,7 +73,7 @@ strategy: any // extend strategy for validation
6973
message?: string // rule error message
7074
```
7175

72-
- options
76+
- `options`
7377

7478
```js
7579
type: 'string' | 'boolean' | 'number' // transform type before validating, default is `string`
@@ -104,7 +108,11 @@ const v = new Validater([
104108
const 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

109117
And the message string is parsed and replaced with the value by \$0.
110118

@@ -119,6 +127,56 @@ const v = new Validater([
119127
const 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

124182
You can find all cases in `files:/test/*.spec.js`, And testing Using below script.

‎package.json‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "@flcwly/validater",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"types": "lib/*.d.ts",
5-
"description": "A pure JavaScript library that parses duration time length to format display",
5+
"description": "A excellent and useful JavaScript validation library, it works better with the Validator.js",
66
"main": "lib/index.js",
77
"scripts": {
88
"test": "jest --config jest.config.json",
@@ -14,18 +14,18 @@
1414
},
1515
"author": "Flcwl",
1616
"keywords": [
17-
"time",
18-
"duration",
19-
"format"
17+
"validator",
18+
"validater",
19+
"validation"
2020
],
2121
"license": "MIT",
22-
"homepage": "https://github.com/Flcwl/duration-pretty",
22+
"homepage": "https://github.com/Flcwl/validater",
2323
"bugs": {
24-
"url": "https://github.com/Flcwl/duration-pretty/issues"
24+
"url": "https://github.com/Flcwl/validater/issues"
2525
},
2626
"repository": {
2727
"type": "git",
28-
"url": "git+https://github.com/Flcwl/duration-pretty.git"
28+
"url": "git+https://github.com/Flcwl/validater.git"
2929
},
3030
"devDependencies": {
3131
"@types/jest": "^24.0.15",

‎test/validater.spec.js‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1-
// import Validater from '../src'
2-
// import {
3-
// required,
4-
// min,
5-
// max,
6-
// len,
7-
// pattern,
8-
// nonPattern,
9-
// include,
10-
// exclude,
11-
// equals,
12-
// unequals,
13-
// startsWith,
14-
// endsWith,
15-
// type,
16-
// alpha,
17-
// numeric,
18-
// } from '../lib/validator'
19-
20-
const Validater = require('../lib').default
21-
const {
1+
import Validater from '../src'
2+
import {
223
required,
234
min,
245
max,
@@ -34,7 +15,26 @@ const {
3415
type,
3516
alpha,
3617
numeric,
37-
} = require('../lib/validator')
18+
} from '../lib/validator'
19+
20+
// const Validater = require('../lib').default
21+
// const {
22+
// required,
23+
// min,
24+
// max,
25+
// len,
26+
// pattern,
27+
// nonPattern,
28+
// include,
29+
// exclude,
30+
// equals,
31+
// unequals,
32+
// startsWith,
33+
// endsWith,
34+
// type,
35+
// alpha,
36+
// numeric,
37+
// } = require('../lib/validator')
3838

3939
describe('Test Validater with mock', () => {
4040
const mockValidateRules = [

0 commit comments

Comments
 (0)