Skip to content

Commit

Permalink
feat(validation): add maxValue, minValue, month, and rule validator
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Oct 20, 2022
1 parent 6a6cb7f commit 9e77710
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/validation/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
export { or, and, not } from '@vuelidate/validators'

export * from './checked'
export * from './email'
export * from './fileExtension'
export * from './hms'
export * from './maxLength'
export * from './minLength'
export * from './maxValue'
export * from './minValue'
export * from './required'
export * from './requiredHms'
export * from './requiredIf'
export * from './requiredYmd'
export * from './url'
export * from './month'
export * from './ymd'
export * from './rule'
10 changes: 10 additions & 0 deletions lib/validation/rules/maxValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { helpers, maxValue as baseMaxValue } from '@vuelidate/validators'

export function maxValue(value: number, msg?: string) {
return helpers.withMessage(
({ $params }) => {
return msg ?? `The value must be less or equal to ${($params as any).max}.`
},
baseMaxValue(value)
)
}
10 changes: 10 additions & 0 deletions lib/validation/rules/minValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { helpers, minValue as baseMinValue } from '@vuelidate/validators'

export function minValue(value: number, msg?: string) {
return helpers.withMessage(
({ $params }) => {
return msg ?? `The value must be greater or equal to ${($params as any).min}.`
},
baseMinValue(value)
)
}
11 changes: 11 additions & 0 deletions lib/validation/rules/month.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { helpers } from '@vuelidate/validators'
import { month as baseMonth } from '../validators/month'

export function month(msg?: string) {
return helpers.withMessage(
() => msg ?? 'The month is invalid.',
(value: number) => {
return !helpers.req(value) || baseMonth(value)
}
)
}
8 changes: 8 additions & 0 deletions lib/validation/rules/rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { helpers } from '@vuelidate/validators'

export function rule(validation: (value: any) => boolean, msg?: string) {
return helpers.withMessage(
() => msg ?? 'The value is invalid.',
(value: any) => !helpers.req(value) || validation(value)
)
}
3 changes: 3 additions & 0 deletions lib/validation/validators/month.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function month(value: number): boolean {
return value > 0 && value < 13
}
11 changes: 11 additions & 0 deletions tests/validation/validators/month.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { month } from 'sefirot/validation/validators/month'

describe('validation/validators/month', () => {
test('validate given month is valid month', () => {
expect(month(0)).toBe(false)
expect(month(1)).toBe(true)
expect(month(6)).toBe(true)
expect(month(12)).toBe(true)
expect(month(13)).toBe(false)
})
})

0 comments on commit 9e77710

Please sign in to comment.