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

@fortress-validator/fortress

v1.0.7

Published

A powerful and flexible form validation library.

Downloads

583

Readme

Fortress Validator

A powerful and flexible form validation library.

Usage

Using with ES Modules

Import the module and use it in your code:

import { FormValidator } from '@fortress-validator/fortress';

const validator = new FormValidator();

window.onload = () => {
  document.querySelector('input').addEventListener('input', (e) => {
    const result = validator
      .defineField('Input')
      .required()
      .alphaDash()
      .validate(e.target.value);

    console.log(result);
  });
};

Using with UMD Modules

Include the UMD script in your HTML file and use it:

<script src="https://unpkg.com/@fortress-validator/fortress/dist/index.umd.js"></script>
<script>
const validator = new window.Fortress.FormValidator();

window.onload = () => {
  document.querySelector('input').addEventListener('input', (e) => {
    const result = validator
      .defineField('Input')
      .required()
      .alphaDash()
      .validate(e.target.value);

    console.log(result);
  });
};
</script>

Available Rules

| Name | Description | | --- | --- | | accepted | Passes if the field's value is considered accepted (i.e., 'y', 'yes', 'on', '1', 'true'). | | after | Passes if the field's value is a date that occurs after the specified date. | | alpha | Passes if the field's value contains only letters. | | alphaDash | Passes if the field's value contains only letters, numbers, dashes and underscores. | | alphaDashDot | Passes if the field's value contains only letters, numbers, dashes, underscores and dots. | | alphaNum | Passes if the field's value contains only letters and numbers. | | array | Passes if the field's value is an array. | | ascii | Passes if the field's value contains only ASCII characters and symbols. | | before | Passes if the field's value is a date that occurs before the specified date. | | between | Passes if the field's value is between the specified minimum and maximum values. | | boolean | Passes if the field's value is a boolean. | | containsAll | Passes if the field's value contains all the specified values. | | containsAny | Passes if the field's value contains at least one of the specified values. | | declined | Passes if the field's value is considered declined (i.e., 'n', 'no', 'off', '0', 'false'). | | different | Passes if the field's value is different from the specified value in the given field. | | distinct | Passes if all the items in the array field's value are unique. | | date | Passes if the field's value matches the specified date format. | | email | Passes if the field's value is a valid email address. | | endsWith | Passes if the field's value ends with one of the specified values. | | equals | Passes if the field's value is equal to the specified value. | | file | Passes if the field's value is a file. | | in | Passes if the field's value is one of the specified values. | | integer | Passes if the field's value is an integer. | | iso8601 | Passes if the field's value is a valid ISO 8601 date. | | json | Passes if the field's value is a valid JSON string. | | jsonSchema | Passes if the field's value matches the specified JSON schema. | | lowercase | Passes if the field's value contains only lowercase characters. | | max | Passes if the field's value does not exceed the specified maximum. | | min | Passes if the field's value is at least the specified minimum. | | notEquals | Passes if the field's value is not equal to the specified value. | | notIn | Passes if the field's value is not one of the specified values. | | number | Passes if the field's value is a number. | | numeric | Passes if the field's value contains only numeric characters. | | regex | Passes if the field's value matches the specified regular expression. | | required | Passes if the field's value is not empty. | | requiredWhen | Passes if the field's value is not empty when the specified condition is true. | | same | Passes if the field's value is the same as the specified value in the given field. | | size | Passes if the field's value matches the specified size. | | startsWith | Passes if the field's value starts with one of the specified values. | | string | Passes if the field's value is a string. | | unique | Passes if the field's value contains only unique items, with optional ignored values. | | uppercase | Passes if the field's value contains only uppercase characters. | | url | Passes if the field's value is a valid URL. |

Available Methods

| Name | Description | | --- | --- | | collect | Collects the rule functions. | | validate | Validates the field's value. | | apply | Applies the specified rule with the given arguments, useful for applying custom rules. | | when | Determines whether to apply or skip validation based on the provided conditions. |

Available Plugins

Custom Plugin

A custom plugin is composed of rules and locales, allowing you to define custom validation logic and locale-specific messages.

const validator = new FormValidator({
  plugins: [
    {
      rules: {
        multipleOf: (({ factor }: { factor: number }) => (input: unknown) => Number(input) % factor === 0) as Rule<unknown>,
      },
      locales: {
        en: {
          multipleOf: (field, args) => {
            const { factor } = args as { factor: number };
            return `The ${field} field must be a multiple of ${factor}.`;
          },
        },
      },
    },
  ],
})
  .defineField('Input')
  .apply('multipleOf', { factor: 2 });

const result = validator.validate(1);

console.log(result);
// Output:
// The input field must be a multiple of 2.

Development

To start a local development server, run:

npm run dev

Testing

To run the tests for this package, run:

npm run test