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

anothersimplejsonvalidator

v1.1.2

Published

A simple and easy to use jsonvalidator

Downloads

13

Readme

Deep Level JSON Validator

A comprehensive tool for validating complex JSON structures

Overview

This validator offers thorough validation of JSON data, ensuring it conforms to specified schemas. It supports nested structures, various data types, custom validation rules, and flexible error handling.

Key Features

  • Deep Validation: Validates nested objects and arrays, ensuring data integrity at every level.
  • Flexible Schema Definition: Defines schemas using a type-safe, declarative API, covering diverse data types like strings, numbers, booleans, objects, arrays, and more.
  • Custom Validation Rules: Enforces specific validation logic with regular expressions for strings and minimum/maximum length for arrays.
  • Error Handling: Provides informative error messages with detailed context for easy debugging, including error types, locations, expected values, and schema details.
  • Exception Handling: Optionally throws errors for immediate interruption of validation upon encountering invalid data.

Usage

  1. Import the validator:

    import { JsonValidator, Schema } from "anothersimplejsonvalidator";
  2. Define the schema:

    const schema = Schema.object({
      id: Schema.string().required(),
      name: Schema.string().required(),
      details: Schema.object({
        price: Schema.number().required(),
        ratings: Schema.array(
          Schema.object({
            rating: Schema.number().required(),
            comment: Schema.string(),
          }).required()
        ),
      }).required(),
      tags: Schema.array(Schema.string()),
    }).required();
  3. Create a validator instance:

     const schema = S.object({
           name: S.string(),
           values: S.array(
             S.array(
               S.array(
                 S.array(S.any(), S.boolean()),
                 S.object({
                   someNumber: S.number(),
                   someString: S.string(),
                   nestedArray: S.array(
                     S.object({
                       nestedBoolean: S.boolean(),
                       nestedString: S.string(),
                       nestedNumber: S.number(),
                       nestedObject: S.object({
                         deeplyNestedString: S.string(),
                         deeplyNestedNumber: S.number(),
                       }),
                     })
                   ),
                 })
               ),
               S.string()
             )
           ),
           isActive: S.boolean(),
           count: S.number(),
           anyValue: S.any(),
         }).setNestedRequired();
  4. Validate JSON data:

    const data = {
      // ... your JSON data
    };
    
    const validationResult = validator.validate(data);
    
    if (validationResult === true) {
      // Data is valid
    } else {
      // Handle errors
      console.error(validationResult); // Array of ErrorController objects
      /**
       [
         Y {
           key: [ 'string' ],
           location: 'JSON.a',
           type: 'expected',
           found: 'number',
           example: null,
           schemaType: null,
           message: 'Expected string but found number at JSON.a'
         }
       ]
       **/
    }

Error Handling

The validate method returns either true if the data is valid, or an array of ErrorController objects containing detailed error information.

Additional Information

  • Supported Data Types: string, number, object, array, boolean, and any.
  • Error Types: Unexpected, Expected, MissingKeys, MissingTypes, Exception, StringRegexMissmatch, NumberMinExpected, NumberMaxExpected.
  • Custom Validation Rules: Regular expressions for strings, minimum/maximum length for arrays.

License

MIT