Skip to content

Commit

Permalink
feat(validation): add and fix validation rules (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
nozomuikuta authored Nov 20, 2023
1 parent 88f3665 commit 95edb02
Show file tree
Hide file tree
Showing 20 changed files with 189 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/validation/rules/decimal.ts
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)
)
}
8 changes: 8 additions & 0 deletions lib/validation/rules/decimalOrHyphen.ts
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
)
}
5 changes: 5 additions & 0 deletions lib/validation/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { and, not, or } from '@vuelidate/validators'
export * from './checked'
export * from './decimal'
export * from './decimalOrHyphen'
export * from './email'
export * from './fileExtension'
export * from './hms'
Expand All @@ -11,10 +12,14 @@ export * from './maxValue'
export * from './minLength'
export * from './minValue'
export * from './month'
export * from './negativeInteger'
export * from './positiveInteger'
export * from './required'
export * from './requiredHms'
export * from './requiredIf'
export * from './requiredYmd'
export * from './rule'
export * from './url'
export * from './ymd'
export * from './zeroOrNegativeInteger'
export * from './zeroOrPositiveInteger'
9 changes: 9 additions & 0 deletions lib/validation/rules/negativeInteger.ts
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
)
}
9 changes: 9 additions & 0 deletions lib/validation/rules/positiveInteger.ts
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
)
}
9 changes: 9 additions & 0 deletions lib/validation/rules/zeroOrNegativeInteger.ts
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)
)
}
9 changes: 9 additions & 0 deletions lib/validation/rules/zeroOrPositiveInteger.ts
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)
)
}
3 changes: 3 additions & 0 deletions lib/validation/validators/hyphen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hyphen(value: string): boolean {
return value === '-'
}
4 changes: 4 additions & 0 deletions lib/validation/validators/index.ts
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'
3 changes: 3 additions & 0 deletions lib/validation/validators/negativeInteger.ts
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
}
3 changes: 3 additions & 0 deletions lib/validation/validators/positiveInteger.ts
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
}
3 changes: 3 additions & 0 deletions lib/validation/validators/zero.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function zero(value: number): boolean {
return value === 0
}
1 change: 1 addition & 0 deletions tests/validation/rules/decimal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('validation/rules/decimal', () => {
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(false)
expect(rule.$validator('abc', null, null)).toBe(false)
expect(rule.$validator(true, null, null)).toBe(false)
})
Expand Down
22 changes: 22 additions & 0 deletions tests/validation/rules/decimalOrHyphen.spec.ts
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.')
})
})
22 changes: 22 additions & 0 deletions tests/validation/rules/zeroOrNegativeInteger.spec copy.ts
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.')
})
})
23 changes: 23 additions & 0 deletions tests/validation/rules/zeroOrPositiveInteger.spec.ts
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.')
})
})
8 changes: 8 additions & 0 deletions tests/validation/validators/hyphen.spec.ts
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)
})
})
18 changes: 18 additions & 0 deletions tests/validation/validators/negativeInteger.spec.ts
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)
})
})
19 changes: 19 additions & 0 deletions tests/validation/validators/positiveInteger.spec.ts
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)
})
})
8 changes: 8 additions & 0 deletions tests/validation/validators/zero.spec.ts
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)
})
})

0 comments on commit 95edb02

Please sign in to comment.