GithubHelp home page GithubHelp logo

muralian's Introduction

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!

muralian's People

Contributors

murugancmi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.