Socket
Socket
Sign inDemoInstall

aproba

Package Overview
Dependencies
0
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    aproba

A rediculously light-weight argument validator


Version published
Weekly downloads
18M
decreased by-10.16%
Maintainers
1
Install size
9.08 kB
Created
Weekly downloads
 

Package description

What is aproba?

The aproba package is a lightweight argument validation library for Node.js. It allows developers to assert types and values of arguments in functions, ensuring that the functions are called with the expected arguments. This can help catch bugs early in the development process by providing a simple and concise way to validate function inputs.

What are aproba's main functionalities?

Type validation

This feature allows you to validate the types of arguments passed to a function. In the code sample, 'N' stands for number and 'S' for string, ensuring the first argument is a string and the second is a number.

"use strict";
const A = require('aproba');

function exampleFunction(a, b) {
  A('NS', arguments);
  // Function logic here
}

exampleFunction('hello', 123);

Optional arguments

aproba supports optional arguments in its type validation. In the example, the '?' after 'S' indicates that the second argument is optional. This means the function can be called with just the first argument.

"use strict";
const A = require('aproba');

function exampleFunction(a, b) {
  A('NS?', arguments);
  // Function logic here
}

exampleFunction('hello');

Other packages similar to aproba

Readme

Source

aproba

A rediculously light-weight function argument validator

var validate = require("aproba")

function myfunc(a, b, c) {
  // `a` must be a string, `b` a number, `c` a function
  validate('SNF', arguments) // [a,b,c] is also valid
}

myfunc('test', 23, function () {}) // ok
myfunc(123, 23, function () {}) // type error
myfunc('test', 23) // missing arg error
myfunc('test', 23, function () {}, true) // too many args error

Valid types are:

typedescription
  • | matches any type A | instanceof Array OR an arguments object S | typeof == string N | typeof == number F | typeof == function O | typeof == object and not type A and not type E B | typeof == boolean E | instanceof Error OR null

Validation failures throw one of three exception types, distinguished by a code property of EMISSINGARG, EINVALIDTYPE or ETOOMANYARGS.

If an error argument is found and is not null then the remaining arguments will not be validated.

Why this exists

I wanted a very simple argument validator. It needed to do two things:

  1. Be more concise and easier to use than assertions

  2. Not encourage an infinite bikeshed of DSLs

This is why types are specified by a single character and there's no such thing as an optional argument.

This is not intended to validate user data. This is specifically about asserting the interface of your functions.

If you need greater validation, I encourage you to write them by hand or look elsewhere.

Keywords

FAQs

Last updated on 09 Apr 2015

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc