Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@ianwalter/nrg-validation

ianwalter70SEE LICENSE IN LICENSE4.1.5

Pragmatic validation for Node.js

schema, validation

readme

@ianwalter/nrg-validation

Pragmatic validation for Node.js

Example Usage

Implementation

import {
  SchemaValidator,
  isEmail,
  isString,
  isStrongPassword,
  isPhone,
  canBeEmpty,
  Validation,
  ValidationError
} from '@ianwalter/nrg-validation'

// Custom validator example:
const msg = 'Occupation must contain software.'
const mustContainSoftware = occupation => (
  new Validation(occupation.toLowerCase().includes('software') || msg)
)

// Create the validator.
const registrationValidator = new SchemaValidator({
  email: { isEmail },
  name: { isString },
  password: { isStrongPassword, message: 'Your password must be stronger.' },
  occupation: { mustContainSoftware },
  phone: { isPhone, canBeEmpty, name: 'telephone number' }
})

try {
  // Validate the input.
  await registrationValidator.validate(req.body).orThrow()

  // Continue to do something here.
} catch (error) {
  if (error instanceof ValidationError) {
    // If the error is a ValidationError, respond with the invalid results.
    res.status(400).json(error.feedback)
  } else {
    res.status(500).end()
  }
}

Input (req.body):

{
  "email": "hahahaha",
  "name": "",
  "password": "qwerty",
  "occupation": "CEO",
  "phone": "777"
}

Output (validation):

{
  email: ['A valid email address is required.'],
  name: ['Name is required.'],
  password: ['Your password must be stronger.'],
  occupation: ['Occupation must contain software.'],
  phone: ['A valid telephone number is required.']
}

Bundled Validators

Basic Types

Name Descriptionn
isString Validates whether input is a String.

Advanced Types

Name Descriptionn
isTimestamp Validates whether input is a valid ISO8601 timestamp.
Valid example: '2018-01-01T00:00:00.000Z'
isEmail Validates whether input is a valid email address.
Valid example: sharonjones@hotmail.com
Invalid example: ting#fastmail.com
isPhone Validates whether input is a valid phone number.
isStrongPassword Validates whether input is a strong (has a score of 3 or 4)
password using zxcvbn
Valid example: 'Dmbu5bc5yeCRwsRD'
Invalid example: 'qwerty'
isUrl Validates whether input is a valid URL.
isZip Validates wehther input is a valid zip code.

Non-validator Options

Name Description
name Allows you to customize the name of the field as it appears in
the error message. If this is not specified, the error message
will use a default specified by the validator used or the object
key.
message Allows you to set the error message.
isOptional Allows you to mark the field as optional/not required.

License

Hippocratic License - See LICENSE

 

Created by Ian Walter

changelog

@ianwalter/nrg-validation

4.1.5

Patch Changes

  • ce7178f: Bumping generates/logger to 2.0.4

4.1.4

Patch Changes

  • b662194: Updating generates/logger to 2.0.2

4.1.3

Patch Changes

  • 4918ade: Update dependency date-fns to ^2.26.0
  • 34a5d2b: Update dependency libphonenumber-js to ^1.9.43

4.1.2

Patch Changes

  • 3f212a5: Update dependency @generates/dotter to ^2.0.3
  • f732f35: Update dependency libphonenumber-js to ^1.9.38

4.1.1

Patch Changes

  • 31d0dc4: Update dependency libphonenumber-js to ^1.9.37
  • b5e7c6e: Update dependency decamelize to v6
  • b8b758b: Update dependency date-fns to ^2.25.0

4.1.0

Minor Changes

  • 7643b38: Adding isUrl validator and toUrl modifier

Patch Changes

  • 124e18e: Update dependency @generates/dotter to ^2.0.1

4.0.4

Patch Changes

  • 41cef5a: Making isPhone country default to US

4.0.3

Patch Changes

  • 2ad9d13: Including index.js in npm files

4.0.2

Patch Changes

  • 4e7eddd: Adding sideEffects: false to package.json

4.0.1

Patch Changes

  • 8b3a325: Fixing release workflow and getApp logic

4.0.0

Major Changes

  • 4ae762c: Switching to ESM by default, adding isUsState validator

Minor Changes

  • c15a8bc: Renaming isUsState to isShortUsState and adding isShortUsZip
  • 1b6a754: Adding date validators

Patch Changes

  • dcc8781: Update dependency date-fns to ^2.22.1

3.0.3

Patch Changes

  • 814ec93: Using dotter.has with schemavalidator data
  • 4038918: Fixing isObject validator

3.0.2

Patch Changes

  • 903a7a1: Update dependency @generates/logger to v1

3.0.1

Patch Changes

  • a5c5ccb: Update dependency date-fns to ^2.19.0
  • 8778497: Update dependency libphonenumber-js to ^1.9.14

3.0.0

Major Changes

  • 2326aa1: Improve validator args and replace isOptional/ignoreEmpty with canBeEmpty

2.1.2

Patch Changes

  • a869815: Update dependency @generates/logger to ^0.1.2

2.1.1

Patch Changes

  • 157b94b: Removing debug statements
  • 7adf205: Fixing nrg-validation nested data issue
  • 50d5911: Update dependency @generates/logger to ^0.1.1

2.1.0

Minor Changes

  • c431216: Adding isInteger, isArray, and support for nested SchemaValidators

Patch Changes

  • 8bb3a79: Update dependency libphonenumber-js to ^1.9.10
  • bb35bbb: Update dependency date-fns to ^2.17.0

2.0.0

Major Changes

  • 2089049: Fixing phone validation and changing schemavalidator validate api to pass additional args

1.5.0

Minor Changes

  • 69d0d80: Passing input to validation

1.4.0

Minor Changes

  • 6ab64fd: Update dependency @generates/logger to ^0.1.0

Patch Changes

  • b75deea: Update dependency @generates/merger to ^0.1.3

1.3.0

Minor Changes

  • f66d0b4: Adding ignoreEmpty option

1.2.1

Patch Changes

  • 1e3e0ab: Fixing modifiers for falsy data / isOptional

1.2.0

Minor Changes

  • 045087e: Adding modifiers