1.0.6 • Published 12 months ago

rc-validator v1.0.6

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
12 months ago

Simple JavaScript validator of Slovak birth numbers (a.k.a. personal numbers)

This library contains a validation methods for slovak birth numbers. It checks following properties:

  • correct position of '/' character
  • illegal characters
  • length
    • 9 chars for year prior 1953 included
    • 10 chars for year after 1953 excluded
  • valid month
  • valid day
  • mod 11 check

Installation

npm i rc-validator or yarn add rc-validator

Usage

import {getValidationError, isValid} from 'rc-validator'

const birthNumber = '700803/8752'

const result = getValidationError(birthNumber)
const valid = isValid(birthNumber)
const slashPresentError = getValidationError(birthNumber, 'SLASH_MUST_NOT_BE_PRESENT')

// result === undefined
// valid === true
// slashPresentError === 'SLASH_MUST_NOT_BE_PRESENT'

Methods

isValid(value: string, slashRule: SlashRule = SlashRule.OPTIONAL): boolean

  • value - input value
  • slashRule - flag declaring how to handle '/' char
  • returns true when input value is valid, otherwise false

getValidationError(value: string, slashRule: SlashRule = SlashRule.OPTIONAL): ValidationError | undefined

  • value - input value
  • slashRule - flag declaring how to handle '/' char
  • returns undefined when input value is valid, otherwise ValidationError enum value

SlashRule

ValueDescriptionNote
SLASH_OPTIONALIt accepts input with or without slashDefault
SLASH_REQUIREDSlash is required
SLASH_MUST_NOT_BE_PRESENTSlash is prohibited, only numbers are accepted

ValidationError

ValueDescriptionNote
TOO_SHORTInput is shorter than 9
TOO_LONGInput is longer than 11
SLASH_REQUIREDSlashRule === REQUIRED, but '/' is not present in input
SLASH_MUST_NOT_BE_PRESENTSlashRule === MUST_NOT_BE_PRESENT, but '/' is present in input
INVALID_SLASH_POSITIONSlash char index is not 6
NOT_A_NUMBERInput contains illegal characters
INVALID_MONTHMonth part is not 1...12
INVALID_DAYDay part is not 1...31Months with 28 or 30 days are not validated by this rule, because mod 11 rule takes care about that
INVALID_YEAR_9_CHARSInput is 9 char long (excluding slash), but year part is > 53
INVALID_YEAR_10_CHARSInput is 10 char long (excluding slash), mod 11 rule not fulfilled and year < 54 or year > 85
MOD_11_CHECK_FAILMod 11 rule not fulfilled

How to run example

cd ./example
yarn
yarn start
1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago