Skip to content

Commit

Permalink
feat(validation): add decimal rule
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Nov 15, 2022
1 parent 82d382b commit 443ab6a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/validation/rules/decimal.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 decimal(msg?: string) {
return helpers.withMessage(
() => msg ?? 'The value must be valid decimal numbers.',
baseDecimal
)
}
1 change: 1 addition & 0 deletions lib/validation/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './requiredHms'
export * from './requiredIf'
export * from './requiredYmd'
export * from './url'
export * from './decimal'
export * from './month'
export * from './ymd'
export * from './rule'
21 changes: 21 additions & 0 deletions tests/validation/rules/decimal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { decimal } from 'sefirot/validation/rules'

describe('validation/rules/decimal', () => {
test('it validates whether the value is decimal', () => {
const rule = decimal()
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('abc', null, null)).toBe(false)
expect(rule.$validator(true, null, null)).toBe(false)
})

test('it can set custome error message', () => {
const rule = decimal('Custom message.')
expect(rule.$message({ $params: {} })).toBe('Custom message.')
})
})

0 comments on commit 443ab6a

Please sign in to comment.