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

simp-validator

v1.3.0

Published

[![GitHub release](https://img.shields.io/github/release/dncgmh/simp-validator?include_prereleases=&sort=semver&color=blue)](https://github.com/dncgmh/simp-validator/releases/) [![issues - simp-validator](https://img.shields.io/github/issues/dncgmh/simp-v

Downloads

26

Readme

Simp Validator

GitHub release issues - simp-validator CircleCI codecov npm License

The Simp Validator is a lightweight JavaScript library for validating data against a schema. It's simple, flexible, and easily transferable over the network. It can be used in both browser and Node.js environments, making it versatile for various development scenarios. Its key feature is the ability to validate data on both the frontend and backend using a single schema, which can be easily migrated or serialized/deserialized without impacting the validation process.

Installation

You can install the Simp Validator using npm:

npm install simp-validator

Usage

To use the Simp Validator in your project, follow these steps:

  1. Import the necessary functions and types from the library:
import { validate, schemaValidate, Rule, Schema, toSchema } from 'simp-validator';

The validate function is used to validate a single value against a rule. The schemaValidate function is used to validate an entire data object against a schema. The Rule and Schema types are used to define validation rules and schemas, respectively. The toSchema function is used to generate a schema from a TypeScript interface.

  1. Define your validation rules using the Rule type:
const rule: Rule = {
  type: 'string',
  min: 2,
  max: 10,
  pattern: '^[a-zA-Z]+$',
  len: 5,
};

The Rule type is an object that defines the validation rules for a single value. The type property is required and specifies the data type of the value being validated. Other properties depend on the data type being validated. In this example, we're validating a string value that is required, must be between 2 and 10 characters long, must match the pattern /^[a-zA-Z]+$/, and must be exactly 5 characters long.

  1. Validate a single value using the validate function:
const value = 'hello';
const result = validate(value, rule);

The validate function takes a value and a rule, and returns a ValidationResult object. The ValidationResult object contains a success property indicating whether the validation was successful, and a message property containing an error message if the validation failed.

  1. Validate an entire data object against a schema using the schemaValidate function:
const data = {
  name: 'John',
  age: 25,
  email: '[email protected]',
  hobbies: ['reading', 'swimming'],
};

const schema: Schema = {
  name: { type: 'string' },
  age: { type: 'number', min: 18 },
  email: { type: 'string', pattern: '^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$' },
  hobbies: {
    type: 'array',
    items: { type: 'string', pattern: '^[a-zA-Z]+$' },
    min: 1,
    max: 5,
  },
};

const validationResult = schemaValidate(data, schema);

The schemaValidate function takes a data object and a schema, and returns a SchemaValidationResult object. The SchemaValidationResult object contains a success property indicating whether the validation was successful, a data property containing the parsed data if the validation was successful, and a details property containing an object with error messages for each field if the validation failed.

In this example, we're validating an object with several fields. The name field is a required string. The age field is a number that must be at least 18. The email field is a string that must match a specific pattern. The hobbies field is an array of strings that must match a specific pattern, must have at least one item, must have no more than five items, and must have unique items

  1. Check the validation result:
if (validationResult.success) {
  // Validation succeeded
  console.log('Validation passed!');
} else {
  // Validation failed
  console.error('Validation failed!');
  // Value validation error message
  console.error('Error messages:', validationResult.message);
  // Schema validation error object
  console.error('Error messages:', validationResult.details);
}

Check the success property of the ValidationResult or SchemaValidationResult object to determine whether the validation was successful. If the validation failed, check the message or details property for error messages.

  1. Generate a schema from a TypeScript interface using the toSchema function:
const rules: Rule[] = [
  { name: 'username', type: 'string' },
  { name: 'age', type: 'number', optional: true },
  { name: 'email', type: 'string', pattern: '^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}$' },
  { name: 'hobbies', type: 'array', items: { type: 'string', pattern: '^[a-zA-Z]+$' } },
];

const { schema, message } = toSchema(rules);

if (schema) {
  console.log('Schema:', schema);
} else {
  console.error('Error:', message);
}

This code creates an array of Rule objects, calls the toSchema function with that array, and then logs the resulting schema object to the console if there were no errors. If there was an error, it logs the error message to the console instead.

Supported Data Types

The Simp Validator supports the following data types for validation:

  • String
  • Number
  • Boolean
  • Array
  • Date

Each data type has its own set of validation rules that can be customized according to your needs.

API

validate(value: any, rule: Rule): ValidationResult

The validate function is used to validate a value against a given rule.

  • value: The value to validate.
  • rule: The rule to validate against.

Returns a ValidationResult object containing the result of the validation.

schemaValidate(data: any, schema: Schema): SchemaValidationResult

The schemaValidate function is used to validate an entire data object against a schema.

  • data: The data object to validate.
  • schema: The schema to validate against.

Returns a SchemaValidationResult object containing the result of the schema validation.

Rule

The Rule type represents a rule for validating a value. It has the following properties:

  • type: The allowed data type of the value.
  • optional: A boolean indicating whether the value is optional. Defaults to false.
  • valid: An array of valid values.
  • Additional properties depending on the data type:
    • For string: min, max, pattern, len
    • For number: integer, min, max
    • For boolean: No additional properties
    • For array: items, min, max, len
    • For date: min, max
  • name (required for schema conversion): The name of the field.
  • description: The description of the field.
  • message: The custom error message to display if the validation fails.

Schema

The Schema type represents a schema for validating an entire data object. It is a record where the keys are the field names, and the values are the validation rules.

ValidationResult

Validate a value against a rule. Returns a ValidationResult object containing the result of the validation.

  • success: A boolean indicating whether the validation was successful.
  • message: An error message if the validation failed.

SchemaValidationResult

Validate an entire data object against a schema. Returns a SchemaValidationResult object containing the result of the schema validation.

  • success: A boolean indicating whether the validation was successful.
  • data: The parsed data if the validation was successful.
  • details: An object containing error messages for each field if the validation failed.