Skip to content

Commit 7cbc2a4

Browse files
committed
add more rules
1 parent dcb4ba1 commit 7cbc2a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+713
-118
lines changed

README.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ A lightweight, Laravel-inspired validation package for Vue 3. Effortlessly valid
1818
- [Validate field](#validate-field)
1919
- [Validate object](#validate-object)
2020
- [Rules](#rules)
21+
- [Presence and requirement](#presence-and-requirement)
22+
- [Value comparison](#value-comparison)
23+
- [Size and range](#size-and-range)
24+
- [Data types](#data-types)
25+
- [Special formats](#special-formats)
26+
- [List membership](#list-membership)
27+
- [Acceptance state](#acceptance-state)
2128
- [Demo](#demo)
2229
- [Example](#example)
2330
- [Faq](#faq)
@@ -185,14 +192,43 @@ function send() {
185192

186193
# Rules
187194

188-
- [included](./lib/rules/included.ts)
189-
- [index](./lib/rules/index.ts)
190-
- [is](./lib/rules/is.ts)
191-
- [max](./lib/rules/max.ts)
192-
- [min](./lib/rules/min.ts)
193-
- [required](./lib/rules/required.ts)
194-
- [sameAs](./lib/rules/sameAs.ts)
195-
- [url](./lib/rules/url.ts)
195+
## Presence and requirement
196+
### required
197+
### requiredIf
198+
### requiredIfAccepted
199+
### requiredIfDeclined
200+
201+
## Value comparison
202+
### confirmed
203+
### same
204+
### is
205+
### greaterThan
206+
### greaterThanOrEqual
207+
### lessThan
208+
### lessThanOrEqual
209+
210+
## Size and range
211+
### between
212+
### max
213+
### min
214+
215+
## Data types
216+
### boolean
217+
### decimal
218+
### integer
219+
220+
## Special formats
221+
### email
222+
### regex
223+
### url
224+
225+
## List membership
226+
### included
227+
### notIncluded
228+
229+
## Acceptance state
230+
### accepted
231+
### declined
196232

197233

198234
# Demo

lib/localization/en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const messages: Messages = {
77
is: ({ field, options }) => `The ${field} is not equal to ${options.value}`,
88
min: ({ field, options }) => `The ${field} field must be at least ${options.length}`,
99
required: ({ field }) => `The ${field} is required`,
10-
sameAs: ({ field, options }) => `The ${field} is ${options.label}`
10+
sameAs: ({ field, options }) => `The ${field} is not same as ${options.label ?? options.path}`
1111
},
1212
attributes: {
1313
email: 'Email',

lib/rules/accepted.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Rule } from '../types'
2+
import { isAccepted, isEmpty } from '../utils'
3+
4+
export function accepted(): Rule {
5+
return {
6+
name: 'accepted',
7+
validate: ({ value }) => {
8+
return isEmpty(value) || isAccepted(value)
9+
}
10+
}
11+
}

lib/rules/between.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { Rule } from '../types'
2+
import { isBetween, isEmpty } from '../utils'
3+
4+
export function between(min: number, max: number): Rule {
5+
return {
6+
name: 'between',
7+
validate: ({ value }) => {
8+
return isEmpty(value) || isBetween(value, min, max)
9+
},
10+
options: { min, max }
11+
}
12+
}

lib/rules/boolean.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Rule } from '../types'
2+
import { isBoolean, isEmpty } from '../utils'
3+
4+
export function boolean(): Rule {
5+
return {
6+
name: 'boolean',
7+
validate: ({ value }) => {
8+
return isEmpty(value) || isBoolean(value)
9+
}
10+
}
11+
}

lib/rules/confirmed.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import get from 'lodash/get'
2+
3+
import type { Rule } from '../types'
4+
import { isEmpty } from '../utils'
5+
6+
export function confirmed(): Rule {
7+
return {
8+
name: 'confirmed',
9+
validate: ({ value, path, payload }) => {
10+
const confirmedValue = get(payload, path + '_confirmed')
11+
return isEmpty(confirmedValue) || isEmpty(value) || value == confirmedValue
12+
}
13+
}
14+
}

lib/rules/decimal.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Rule } from '../types'
2+
import { isDecimal, isEmpty } from '../utils'
3+
4+
export function decimal(): Rule {
5+
return {
6+
name: 'decimal',
7+
validate: ({ value }) => {
8+
return isEmpty(value) || isDecimal(value)
9+
}
10+
}
11+
}

lib/rules/declined.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Rule } from '../types'
2+
import { isDeclined, isEmpty } from '../utils'
3+
4+
export function declined(): Rule {
5+
return {
6+
name: 'declined',
7+
validate: ({ value }) => {
8+
return isEmpty(value) || isDeclined(value)
9+
}
10+
}
11+
}

lib/rules/email.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Rule } from '../types'
2+
import { isEmail, isEmpty } from '../utils'
3+
4+
export function email(): Rule {
5+
return {
6+
name: 'email',
7+
validate: ({ value }) => {
8+
return isEmpty(value) || isEmail(value)
9+
}
10+
}
11+
}

lib/rules/greaterThan.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { Rule } from '../types'
2+
import { isEmpty, isGreaterThan } from '../utils'
3+
4+
export function greaterThan(threshold: number): Rule {
5+
return {
6+
name: 'greaterThan',
7+
validate: ({ value }) => {
8+
return isEmpty(value) || isGreaterThan(value, threshold)
9+
},
10+
options: { threshold }
11+
}
12+
}

0 commit comments

Comments
 (0)