Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@universal-packages/validations

Package Overview
Dependencies
Maintainers
0
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@universal-packages/validations

Simple validation system based on decorators to enable a class to validate a subject properties.

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.6K
increased by11.45%
Maintainers
0
Weekly downloads
 
Created
Source

Validations

npm version Testing codecov

Simple validation system based on decorators to enable a class to validate a subject properties.

Install

npm install @universal-packages/validations

BaseValidation

Extend the base validation to start building a class validation, when running the validation you will get validation record containing errors if any and a valid flag to quick knowing if if was successful.

import { BaseValidation, Validator } from '@universal-packages/validations'

export default class UserValidation extends BaseValidation {
  @Validator('name')
  rightNameSize(value) {
    return value.length > 5 && value.length < 128
  }
}

console.log(await UserValidation.validate({ name: 'sm' }))

// > { errors: { name: ['name failed rightNameSize validation'] }, valid: false }

Initial values

You can pass initial values so that you can reference them in your validators, for example when you don't want to perform heavy validations on values that were already validated.

import { BaseValidation, Validator } from '@universal-packages/validations'

export default class UpdateUserValidation extends BaseValidation {
  @Validator('email')
  alreadyInDb(value, initialName) {
    if (value === initialName) return true
    return !await db.exists({ email: value })
  }
}

const validation = new UpdateUserValidation({ name: 'email' })

console.log(await validation.validate({ name: 'email' }))

// > { errors: {}, valid: true }

Decorators

@Validator(property: string, [options])

The validator decorator enable a class method to act as a validator, the method should return a boolean to tell teh validation if the property is valid or not. The first argument of the decorator is the property to be validated.

You can use several methods to validate a single property:

import { BaseValidation, Validator } from '@universal-packages/validations'

export default class UserValidation extends BaseValidation {
  @Validator('name')
  isAString(value) {
    return typeof value === 'string'
  }

  @Validator('name')
  rightNameSize(value) {
    return value.length > 5 && value.length < 128
  }
}

console.log(await UserValidation.validate({ name: 50 }))

// > { errors: { name: ['name failed isAString validation', 'name failed rightNameSize validation'] }, valid: false }
Options
  • inverse Boolean Inverts the validator validity of the method returns true the property is invalid.

    @Validator('name', { inverse: true })
    isPretty(value) {
      return value === 'ugly'
    }
    
  • message String When the validation fails set the error with a custom message.

    @Validator('name', { message: 'Name is not pretty' })
    isPretty(value) {
      return value !== 'ugly'
    }
    
  • optional Boolean The validation will run only if the property is set (not undefined nor null).

    @Validator('name', { optional: true })
    isStrong(password) {
      return password.length > 69
    }
    
  • priority Number The priority level for the validation, if a validation with a lower number fails validations with a upper number will not run, but all validations in the same priority will run.

    Use this so validations don't throw an error reading an unexpected type.

    @Validator('name')
    isString(value) {
      return typeof value === 'string'
    }
    
    @Validator('name', { priority: 1})
    containsWord(value) {
      return value.indexOf('word') !== -1
    }
    

Typescript

This library is developed in TypeScript and shipped fully typed.

Contributing

The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.

License

MIT licensed.

FAQs

Package last updated on 30 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc