-
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 and fix validation rules (#396)
- Loading branch information
1 parent
88f3665
commit 95edb02
Showing
20 changed files
with
189 additions
and
2 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,8 +1,9 @@ | ||
import { decimal as baseDecimal, helpers } from '@vuelidate/validators' | ||
import { and, decimal as baseDecimal, helpers, not } from '@vuelidate/validators' | ||
import { hyphen } from '../validators' | ||
|
||
export function decimal(msg?: string) { | ||
return helpers.withMessage( | ||
() => msg ?? 'The value must be valid decimal numbers.', | ||
baseDecimal | ||
and(not(hyphen), baseDecimal) | ||
) | ||
} |
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 { decimal as baseDecimal, helpers } from '@vuelidate/validators' | ||
|
||
export function decimalOrHyphen(msg?: string) { | ||
return helpers.withMessage( | ||
() => msg ?? 'The value must be valid decimal numbers or just a hyphen.', | ||
baseDecimal | ||
) | ||
} |
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
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,9 @@ | ||
import { helpers } from '@vuelidate/validators' | ||
import { negativeInteger as baseNegativeInteger } from '../validators' | ||
|
||
export function negativeInteger(msg?: string) { | ||
return helpers.withMessage( | ||
() => msg ?? 'The value must be valid negative integer.', | ||
baseNegativeInteger | ||
) | ||
} |
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,9 @@ | ||
import { helpers } from '@vuelidate/validators' | ||
import { positiveInteger as basePositiveInteger } from '../validators' | ||
|
||
export function positiveInteger(msg?: string) { | ||
return helpers.withMessage( | ||
() => msg ?? 'The value must be valid positive integer.', | ||
basePositiveInteger | ||
) | ||
} |
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,9 @@ | ||
import { helpers, or } from '@vuelidate/validators' | ||
import { negativeInteger, zero } from '../validators' | ||
|
||
export function zeroOrNegativeInteger(msg?: string) { | ||
return helpers.withMessage( | ||
() => msg ?? 'The value must be zero or valid negative integer.', | ||
or(zero, negativeInteger) | ||
) | ||
} |
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,9 @@ | ||
import { helpers, or } from '@vuelidate/validators' | ||
import { positiveInteger, zero } from '../validators' | ||
|
||
export function zeroOrPositiveInteger(msg?: string) { | ||
return helpers.withMessage( | ||
() => msg ?? 'The value must be zero or valid positive integer.', | ||
or(zero, positiveInteger) | ||
) | ||
} |
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 hyphen(value: string): boolean { | ||
return 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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
export * from './checked' | ||
export * from './fileExtension' | ||
export * from './hms' | ||
export * from './hyphen' | ||
export * from './maxFileSize' | ||
export * from './maxTotalFileSize' | ||
export * from './month' | ||
export * from './negativeInteger' | ||
export * from './positiveInteger' | ||
export * from './requiredHms' | ||
export * from './requiredYmd' | ||
export * from './ymd' | ||
export * from './zero' |
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 negativeInteger(value: number): boolean { | ||
return Number.isInteger(value) && value < 0 | ||
} |
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 positiveInteger(value: number): boolean { | ||
return Number.isInteger(value) && value > 0 | ||
} |
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 zero(value: number): boolean { | ||
return value === 0 | ||
} |
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
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,22 @@ | ||
import { decimalOrHyphen } from 'sefirot/validation/rules' | ||
|
||
describe('validation/rules/decimalOrHyphen', () => { | ||
test('it validates whether the value is decimalOrHyphen', () => { | ||
const rule = decimalOrHyphen() | ||
expect(rule.$validator(0, null, null)).toBe(true) | ||
expect(rule.$validator(1, null, null)).toBe(true) | ||
expect(rule.$validator(-1, null, null)).toBe(true) | ||
expect(rule.$validator(1.1, null, null)).toBe(true) | ||
expect(rule.$validator(-1.1, null, null)).toBe(true) | ||
expect(rule.$validator('1.00', null, null)).toBe(true) | ||
expect(rule.$validator('-1.00', null, null)).toBe(true) | ||
expect(rule.$validator('-', null, null)).toBe(true) | ||
expect(rule.$validator('abc', null, null)).toBe(false) | ||
expect(rule.$validator(true, null, null)).toBe(false) | ||
}) | ||
|
||
test('it can set custom error message', () => { | ||
const rule = decimalOrHyphen('Custom message.') | ||
expect(rule.$message({ $params: {} })).toBe('Custom message.') | ||
}) | ||
}) |
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,22 @@ | ||
import { zeroOrNegativeInteger } from 'sefirot/validation/rules' | ||
|
||
describe('validation/rules/zeroOrNegativeInteger', () => { | ||
test('it validates whether the value is zeroOrNegativeInteger', () => { | ||
const rule = zeroOrNegativeInteger() | ||
expect(rule.$validator(0, null, null)).toBe(true) | ||
expect(rule.$validator(-1, null, null)).toBe(true) | ||
expect(rule.$validator(-1.0, null, null)).toBe(true) | ||
expect(rule.$validator(-10, null, null)).toBe(true) | ||
|
||
expect(rule.$validator(-1.1, null, null)).toBe(false) | ||
expect(rule.$validator(1, null, null)).toBe(false) | ||
expect(rule.$validator(1.1, null, null)).toBe(false) | ||
expect(rule.$validator(+1, null, null)).toBe(false) | ||
expect(rule.$validator(+1.1, null, null)).toBe(false) | ||
}) | ||
|
||
test('it can set custom error message', () => { | ||
const rule = zeroOrNegativeInteger('Custom message.') | ||
expect(rule.$message({ $params: {} })).toBe('Custom message.') | ||
}) | ||
}) |
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,23 @@ | ||
import { zeroOrPositiveInteger } from 'sefirot/validation/rules' | ||
|
||
describe('validation/rules/zeroOrPositiveInteger', () => { | ||
test('it validates whether the value is zeroOrPositiveInteger', () => { | ||
const rule = zeroOrPositiveInteger() | ||
expect(rule.$validator(0, null, null)).toBe(true) | ||
expect(rule.$validator(1, null, null)).toBe(true) | ||
expect(rule.$validator(+1, null, null)).toBe(true) | ||
expect(rule.$validator(1.0, null, null)).toBe(true) | ||
expect(rule.$validator(+1.0, null, null)).toBe(true) | ||
expect(rule.$validator(10, null, null)).toBe(true) | ||
expect(rule.$validator(+10, null, null)).toBe(true) | ||
|
||
expect(rule.$validator(-1, null, null)).toBe(false) | ||
expect(rule.$validator(1.1, null, null)).toBe(false) | ||
expect(rule.$validator(-1.1, null, null)).toBe(false) | ||
}) | ||
|
||
test('it can set custom error message', () => { | ||
const rule = zeroOrPositiveInteger('Custom message.') | ||
expect(rule.$message({ $params: {} })).toBe('Custom message.') | ||
}) | ||
}) |
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 { hyphen } from 'sefirot/validation/validators' | ||
|
||
describe('validation/validators/hyphen', () => { | ||
it('should validates if the value is true', () => { | ||
expect(hyphen('-')).toBe(true) | ||
expect(hyphen('')).toBe(false) | ||
}) | ||
}) |
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,18 @@ | ||
import { negativeInteger } from 'sefirot/validation/validators' | ||
|
||
describe('validation/validators/negativeInteger', () => { | ||
it('should validates if the value is true', () => { | ||
expect(negativeInteger(-1)).toBe(true) | ||
expect(negativeInteger(-1.0)).toBe(true) | ||
expect(negativeInteger(-10)).toBe(true) | ||
|
||
expect(negativeInteger(0)).toBe(false) | ||
expect(negativeInteger(+0)).toBe(false) | ||
expect(negativeInteger(-0)).toBe(false) | ||
expect(negativeInteger(-1.1)).toBe(false) | ||
expect(negativeInteger(1)).toBe(false) | ||
expect(negativeInteger(1.1)).toBe(false) | ||
expect(negativeInteger(+1)).toBe(false) | ||
expect(negativeInteger(+1.1)).toBe(false) | ||
}) | ||
}) |
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,19 @@ | ||
import { positiveInteger } from 'sefirot/validation/validators' | ||
|
||
describe('validation/validators/positiveInteger', () => { | ||
it('should validates if the value is true', () => { | ||
expect(positiveInteger(1)).toBe(true) | ||
expect(positiveInteger(+1)).toBe(true) | ||
expect(positiveInteger(1.0)).toBe(true) | ||
expect(positiveInteger(+1.0)).toBe(true) | ||
expect(positiveInteger(10)).toBe(true) | ||
expect(positiveInteger(+10)).toBe(true) | ||
|
||
expect(positiveInteger(0)).toBe(false) | ||
expect(positiveInteger(+0)).toBe(false) | ||
expect(positiveInteger(-0)).toBe(false) | ||
expect(positiveInteger(-1)).toBe(false) | ||
expect(positiveInteger(1.1)).toBe(false) | ||
expect(positiveInteger(-1.1)).toBe(false) | ||
}) | ||
}) |
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 { zero } from 'sefirot/validation/validators' | ||
|
||
describe('validation/validators/zero', () => { | ||
it('should validates if the value is true', () => { | ||
expect(zero(0)).toBe(true) | ||
expect(zero(1)).toBe(false) | ||
}) | ||
}) |