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

muralian

v1.0.1

Published

Simple JSON Validation engine with direct Data Types

Downloads

6

Readme

Muralian Schema Generation

Introduction

When working with data validation in JavaScript, it's common to use libraries like joi to define schemas and validate data against those schemas. However, manually creating schemas can be tedious and repetitive, especially when dealing with complex data structures.

To simplify the process, we have created a utility function called Muralian that dynamically generates Joi schemas based on a provided structure object. This allows us to define the structure of our data in a more concise and readable manner.

Usage

To use the muralianSchema function, follow these steps:

  1. Install the Joi library by running npm install muralian in your project directory.

  2. Import the Joi library and the muralianSchema function in your JavaScript file:

    import {muralianSchema} from "muralian";
  3. Define the structure of your data using a JavaScript object. The structure object should follow these guidelines:

    • Each key in the structure object represents a field in your data.
    • The value of each key can be either a simple type string ('string', 'number', 'boolean', 'object', 'array') or a detailed type definition object.
    • For detailed type definitions, use the following format:
      {
        type: 'string', // or 'number', 'boolean', 'object', 'array'
        required: true, // optional, indicates if the field is required
        options: {
          min: 2, // optional, minimum length for strings or minimum value for numbers
          max: 50, // optional, maximum length for strings or maximum value for numbers
          email: true, // optional, indicates if the field should be a valid email
        },
      }

    Example structure:

    const structure = {
            name: {
                    type: "string",
                    required: true,
                    options: {min: 2, max: 50},
            },
            age: {type: "number", required: true},
            email: {type: "string", required: true, options: {email: true}},
            isStudent: "boolean",
            address: {type: "object", required: true},
            hobbies: "array",
    };
  4. Call the muralianSchema function, passing the structure object as an argument:

    const schema = muralianSchema(structure);
  5. Use the generated muralian schema to validate your data:

    const {error, value} = schema.validate(data);
    if (error) {
            console.log("Validation failed:", error.details[0].message);
    } else {
            console.log("Validation passed:", value);
    }

Supported Types and Options

The muralianSchema function supports the following types and options:

  • string: Represents a string field.

    • min: Optional, specifies the minimum length of the string.
    • max: Optional, specifies the maximum length of the string.
    • email: Optional, indicates if the string should be a valid email.
  • number: Represents a number field.

  • boolean: Represents a boolean field.

  • object: Represents an object field.

    • The value should be another structure object defining the nested fields.
  • array: Represents an array field.

    • The value should be a structure object defining the items of the array.

Conclusion

By using the muralianSchema function, you can easily generate Joi schemas based on a provided structure object. This approach reduces the amount of boilerplate code required and makes your data validation logic more maintainable.

Remember to handle any validation errors appropriately in your application and provide meaningful error messages to the users.

Happy validating!