npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

alfil

v0.7.0

Published

Minimal and lightweight data validation Javascript library.

Downloads

6

Readme

Alfil

Alfil is a lightweight zero-dependency Javascript library for data validation.

INSTALLATION

Install Alfil using npm:

npm i alfil

Or import the library from the UNPKG CDN inside a <script> tag:

<script src="https://unpkg.com/alfil@latest">

🚀 QUICK USAGE

With these easy steps you can have your Alfil validators up and running in a blink of an eye:

  1. Require Alfil (only if you installed it via npm):
const Alfil = require("alfil");
  1. Create your validator schema. For example:
const myValidator = Alfil.createValidator({
  firstName: Alfil.string().required(),
  website: Alfil.string().url(),
  prizes: Alfil.number().integer(),
  isFamous: Alfil.bool(),
});
  1. Then, you can use your validator to validate your data. Continuing with the previous example, let's suppose you have the following data object:
const myDummyData = {
  firstName: "James",
  website: "https://james.com/",
  prizes: 24,
  isFamous: true,
};

// Now run the validations against your dummy data
const validationResults = myValidator.validate(myDummyData);
  1. This last line of code execute your validations. The validate() method returns an object with all your validation results. This object has an isValid property with a boolean value, so you can make decisions depending on it:
if (validationResults.isValid) {
  // Do something if dummy data is valid
} else {
  // Otherwise, do something else
}

ALFIL TYPES

Alfil provides different types of data to shape your validators. Every type has it's own methods and properties, so let's dive into the details:

Alfil.string(options)

This may be the most common type you're going to use. As it's name suggest, this type will expect a String value when your validation process runs. Next, we'll see it's methods and options:

| Method | Params | Explanation | | -------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | required | - | Sets the data to be required, that means your validation will fail against an empty string (you can change this behaviour by setting a custom emptyValue, check it out here). | | email | - | This method will expect a valid email address. Alfil uses an own regular expression to validate. If you want to use a different RegExp, you can use the match() method, that means your validation will fail against an empty string (you can change this behaviour by setting a custom emptyValue, check it out here). | | password | opts?: { type: string } | This method will expect a "safe" password. The type property of the opts parameter let you decide which kind of password you prefer. Alfil provides you with 2 types of password: "complex" and "moderate", the latter being the default. Like the email() method, you can use your own RegExp using match(). | | username | - | This method will expect a valid username. | | url | opts?: { type: string } | This method will expect a valid url. The type property of the opts parameter let you decide what kind of url you prefer. Alfil provides you with 2 types of url: "requiredProtocol" and "optionalProtocol", the latter being the default. | | match | regex: Regex | This method will test the value against the regular expression provided as an argument. | | min | minVal: number | This method will check if the length of the value is greater than or equal to minVal. | | max | maxVal: number | This method will check if the length of the value is less than or equal to maxVal. | | enum | arrOptions: string[] | This method will expect that the value is equal to one of the strings provided inside the arrOptions Array. |

Options. the Alfil's String type accepts an options object as an argument. You can use the following options to configure the default behaviour: |Option|Type|Default|Explanation| |-|-|-|-| |trim|boolean|false|If this option is set to true, Alfil will apply thetrim() method to the value before executing the validators, except for the password method defined in the previous table. | |emptyValue|string|null|""|This option defines the value that has to be taken as an empty value.

Alfil.number(options)

This type will expect a Number value when your validations run. Let's see it's methods and options: |Method|Params|explanation| |-|-|-| |required|-|Sets the data to be required, that means your validation will fail against a null value (like the Alfil's string you can change this behaviour by setting a custom emptyValue property inside the options object).| |min|minVal|This method will check if the number is greater than or equal to minVal.| |max|maxVal|This method will check if the number is less than or equal to maxVal.| |integer|-|Will check if the number is an integer.| |float|-|Will check if the number is a floating point number.|

Options. The Alfil's Number type accepts an options object as an argument. You can use the following options to configure the default behaviour: |Option|Type|Default|Explanation| |-|-|-|-| |emptyValue|string|null|number|null|This option defines the value that has to be taken as an empty value.|

Alfil.bool()

This type will expect a Boolean value when your validations runs. Let's see it's methods: |Method|Params|explanation| |-|-|-| |true|-|This method will check if the value is equal to true| |false|-|This method will check if the value is equal to false|

THE VALIDATOR SCHEMA

The createValidator method from Alfil returns a validator schema. This let's you run your validations against your data objects and thus getting the validation results:

const myValidatorSchema = Alfil.createValidator({...});

Run your validations using the validatemethod. This method returns the validation results object, so you can store it in a variable, for example:

const myResults =  myValidatorSchema.validate(myData);

Or you can access these results later using the getResults method:

const myResults =  myValidatorSchema.getResults();

keep in mind that if you don't run your validations (with the validate method) before getting the results, this will throw an Error.

🤝 CONTRIBUTING

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

AUTHOR

👤 Manuel Pascual

SHOW YOUR SUPPORT

Give a ⭐️ if this project helped you! And maybe you could BUY ME A COFFEE 😀

📝 LICENSE

This project is ISC licensed.