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

schema-crafterjs

v1.0.7

Published

A Lightweight Form Schema Validation Package.

Downloads

3

Readme

SchemaCrafterJs

SchemaCrafterJs is a lightweight and strongly-typed form validation package designed for use with TypeScript. It allows you to define schemas for your form fields and validate payloads against these schemas. This package has one dependency and no dependents, making it easy to integrate into your projects.

Installation

To install SchemaCrafterJs, you can use npm:

npm install schema-crafterjs

Usage

// Import the SchemaBuilder class
import SchemaBuilder from "schema-crafterjs";
// or
const SchemaBuilder = require("schema-crafterjs");

// Create a new instance of SchemaBuilder
const schema = new SchemaBuilder();

Methods

build()

The build() method is used to define the schema for your form fields. It takes an object with keys representing field names and options representing field constraints. Available options include:

  • required: Specifies if the field is required.
  • min: Specifies the minimum value for numeric fields.
  • max: Specifies the maximum value for numeric fields.
  • matches: Specifies a regular expression pattern that the field must match.
  • string: Specifies if the field value must be a string.
  • number: Specifies if the field value must be a number.
  • boolean: Specifies if the field value must be a boolean.
  • date: Specifies if the field value must be a date.
  • email: Specifies if the field value must be a valid email address.
  • password: Specifies if the field value must meet password requirements (uppercase, lowercase, and numbers).

validate(payload)

The validate() method is used to validate a payload against the defined schema. It expects a payload object that matches the schema defined using the build() method. Validation is performed based on the constraints specified in the schema.

peek()

The peek() method returns the schema object. This is useful for development purposes when you need to inspect the defined schema.

peekError()

The peekError() method returns the current error object. This is useful for development purposes.

Example

// Define schema
async function buildSchema() {
  try {
    await schema.build({
      fullName: { required: [true, "Full name is required"], string: [true, "Full Name Must Be String."], matches: [true, new RegExp(/^[A-Za-z]*$/), "Full Name Must Only Be Letters."] },
      age: { number: [true, "Age must be a number"], min: [18, "Must be at least 18 years old"] },
      email: { email: [true, "Invalid email address"] },
      password: { password: [true, "Password must contain uppercase, lowercase, and numbers"], min: {8, "Password Must Have A Min Length of 8 Characters."} }
    });
  } catch (err) {
    //if error is thrown it will be caught here.
    //can expect errors to be typeerrors or errors.
    //wrapping in try catch block allows for graceful error handling in the development process.
  }
}

// Validate payload
const payload = {
  fullName: "John Doe",
  age: 25,
  email: "[email protected]",
  password: "Password123"
};
async function submitForm(payload) {
  try {
    const result = await schema.validate(payload);
    //will return undefined if valid;
  } catch (err) {
    //errors are thrown on in valid submission, therefore, a try catch block is sufficient to handle these errors. On submission success, the result will be undefined;
    //can expect error to be instance of Error.
    //result will be errors objects in array.
    console.log(err);
  }
}
submitForm(payload);

Contributions

  • This is an open source npm package.
  • Contributions are welcome.
  • Here is the link to the main github containing tests: schemacrafter github
  • Here is a working example with React: React Working Example
  • Here is another working example with tests: tests

License

This package is licensed under the ISC License.