New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

json-joi-converter

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-joi-converter

Converts json from and to Joi objects.

latest
Source
npmnpm
Version
17.13.7
Version published
Weekly downloads
1.5K
-0.84%
Maintainers
1
Weekly downloads
 
Created
Source

Json Joi

Build Status Node version required Latest Stable Version

json-Joi-converter

json-Joi-converter is a robust TypeScript module built on top of the popular Joi library. This package seamlessly converts Joi schemas to JSON and back, enabling effortless creation of Joi schemas to and from JSON.

Why Choose json-Joi-converter?

Do you need to share the same Joi schema between your backend and frontend? This module makes it simple!

  • Generate a JSON representation of your Joi schema on the backend.
  • Use that JSON to recreate the Joi schema on the frontend, ensuring consistent validation across your application.

About Joi

Joi is known as "the most powerful schema description language and data validator for JavaScript." json-Joi-converter extends its capabilities to make schema sharing and reuse even more efficient.

See Joi API for documentation and api.

Try demo

https://siavashg.com/json-joi-demo/

Installation

npm install json-joi-converter

Usage

import Joi, {fromJson, toJson, Schema} from 'json-joi-converter';

const json: Schema = {
  type: "object",
  properties: {
    a: {
      type: "number",
      min: 100,
      max: 1000,
      required: true
    }
  }
};

// fromJson(json) is equal to following

const joi = Joi.object({
  a: Joi.number().min(100).max(1000).required()
});

// VALID
assert.deepEqual(toJson(joi), toJson(fromJson(json)));


Joi Reference & Functions

{
  a: {
    type: "number"
  },
  b: {
    type: "number",
    min: {
      $ref: "a",
      adjust: "value => value + 1"
    }
  }
}

// where adjust is a stringed function

// is equal to
Joi.object({
  a: Joi.number(),
  b: Joi.number.min(Joi.ref("a", {
    adjust: value => value + 1
  }))
})

Joi RegExp

let json: ObjectSchema = {
  type: "object",
  properties: {
    a: {
      type: "string",
      pattern: "/a/"
    },
    b: {
      type: "string",
      regex: {$regex: "/a/", flags: "i"}
    },
    c: {
      type: "string",
      regex: {pattern: {$regex: "/a/"}}
    }
  }
};

const converted = (toJson(fromJson(json)) as any);

assert.equal(converted.properties?.a?.pattern?.regex?.$regex, '\\/a\\/');
assert.equal(converted.properties?.b?.pattern?.regex?.$regex, '\\/a\\/');
assert.equal(converted.properties?.b?.pattern?.regex?.flags, 'i');
assert.equal(converted.properties?.c?.pattern?.regex?.$regex, '\\/a\\/');

Joi Replace

{
  type: "string",
  replace: {find: {$regex: "a", flags: "gi"}, replace: "b"}
}

// is equal to
Joi.string().replace(/a/gi, "b")

{
  type: "string",
  replace: [
    {find: {$regex: "a", flags: "gi"}, replace: "b"},
    {find: "a", replace: "b"}
  ]
}

// is equal to
Joi.string().replace(/a/gi, "b").replace("a", "b")

Keywords

joi

FAQs

Package last updated on 20 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts