-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validation): add maxValue, minValue, month, and rule validator
- Loading branch information
Showing
7 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |