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

@sodaru/yup-to-json-schema

v2.0.1

Published

Library to convert Yup to JSON Schema

Downloads

80,846

Readme

yup-to-json-schema

Yup is a library to validate the JSON input

JSON-Schema is a schema specification for JSON

NOTE : Upgraded to support Yup Version 1 Version Maping | Yup Major Version | This Library Major Version | Branch in this Library | | ----- | ------ | -----| | 1 | 2 | main | | 0 | 1 | yup_v0 |

This library converts Yup schema to JSON-Schema

Installation

npm i @sodaru/yup-to-json-schema

Usage

Convert YUP to JSON-SCHEMA

import { object, string, array, tuple, date, boolean, number } from 'yup';
import { convertSchema } from '@sodaru/yup-to-json-schema';

const schema = object({
  name: string().label('Name').required(),
  address: tuple([string(), string()]).label('Address'),
  hobbies: array(string()).min(1).label('Hobbies'),
  birthdate: date().label('Birthdate'),
  admin: boolean().label('Admin'),
  rank: number().integer().positive().label('Rank')
});

const jsonSchema = convertSchema(schema);

assert(jsonSchema).toEqual({
  type: 'object',
  required: ['name']
  properties: {
    name: {
      type: 'string',
      title: 'Name'
    },
    address: {
      type: 'array',
      title: 'Address',
      minItems: 2,
      maxItems: 2,
      items: [
        {
          type: 'string'
        },
        {
          type: 'string'
        }
      ]
    },
    hobbies: {
      type: 'array',
      title: 'Hobbies',
      items: {
        type: 'string'
      },
      minItems: 1
    },
    birthdate: {
      type: 'string',
      title: 'Birthdate',
      format: 'date-time'
    },
    admin: {
      type: 'boolean',
      title: 'Admin'
    },
    rank: {
      type: 'integer',
      title: 'Rank',
      exclusiveMinimum: 0
    }
  }
});

Provide additional Schema as meta property in Yup

Add additional JSON Schema information to the jsonSchema property via the meta() method. This will be shallow merged with the result of the conversion.

const schema = object({ ... }).meta({
  jsonSchema: {
    $id: '...'
    description: '...'
  }
});

extend the schema

You can also use the extendSchema to add helpful methods to all of your yup schemas. Use the example, examples, description, and jsonSchema methods to add common proprties to your JSON Schemas.

import {
  object,
  string,
  array,
  tuple,
  date,
  boolean,
  number,
  addMethod,
  Schema
} from 'yup';
import { extendSchema, convertSchema } from '@sodaru/yup-to-json-schema';

extendSchema({ addMethod, Schema });

const schema = object({
  name: string()
    .label('Name')
    .description('Your first name')
    .example('Johnny Cash')
    .required(),
  address: tuple([string(), string()])
    .label('Address')
    .description('Your Address')
    .example(['Nashville', 'TN']),
  hobbies: array(string())
    .label('Hobbies')
    .min(1)
    .description('Your hobbies')
    .example(['Guitar', 'Singing']),
  birthdate: date()
    .label('Birthdate')
    .description('Your birthdate')
    .example(new Date('February 26, 1932')),
  admin: boolean()
    .label('Admin')
    .description('Use is an admin')
    .example(true),
  rank: number()
    .label('Rank')
    .description('Your rank')
    .jsonSchema((jsonSchema) => {
      return {
        ...jsonSchema,
        testProp: true
      };
    })
    .example(27)
    .integer()
    .positive()
});

const jsonSchema = convertSchema(schema);

jsonSchema = {
  type: 'object',
  required: ['name']
  properties: {
    name: {
      type: 'string',
      title: 'Name',
      description: 'Your first name',
      example: 'Johnny Cash'
    },
    address: {
      type: 'array',
      title: 'Address',
      description: 'Your Address',
      example: ['Nashville', 'TN'],
      minItems: 2,
      maxItems: 2,
      items: [
        {
          type: 'string'
        },
        {
          type: 'string'
        }
      ]
    },
    hobbies: {
      type: 'array',
      title: 'Hobbies',
      items: {
        type: 'string'
      },
      minItems: 1,
      description: 'Your hobbies',
      example: ['Guitar', 'Singing']
    },
    birthdate: {
      type: 'string',
      title: 'Birthdate',
      format: 'date-time',
      description: 'Your birthdate',
      example: '1932-02-26T06:00:00.000Z'
    },
    admin: {
      type: 'boolean',
      title: 'Admin',
      description: 'User is an admin',
      example: true
    },
    rank: {
      type: 'integer',
      title: 'Rank',
      exclusiveMinimum: 0,
      description: 'Your rank',
      testProp: true,
      example: 27
    }
  }
};

Provide the data using the ResolveOptions for when and lazy

Use the second argument ResolveOptions to pass context to allow for better when and lazy methods. This argument is passed to the underlying describe(options)

import { object, number } from "yup";
import { convertSchema } from "@sodaru/yup-to-json-schema";

const schema = object({
  rank: number().when(["name"], ([name], schema) => {
    return name === "Johnny Cash" ? schema.min(100) : schema.min(0);
  })
});

const schema = object({
  rank: lazy(({ name }) => {
    return name === "Johnny Cash" ? number().min(100) : number().min(0);
  })
});

const jsonSchema = convertSchema(schema, {
  value: { name: "Johnny Cash", rank: 100 }
});

jsonSchema = {
  type: "object",
  properties: {
    rank: {
      type: "number",
      minimum: 100
    }
  }
};

Contribution

Fork the repo and send the Pull Requests to develop branch

develop is merged to the main branch periodically to make a release

Support

This project is a part of the Open Source Initiative from Sodaru Technologies

Write an email to [email protected] for queries on this project