Giter Club home page Giter Club logo

typescript-schema-to-joi's Introduction

Typescript schema to Joi

The purpose of this package is to convert a typescript type into a joi schema.

To install this package, run npm install typescript-schema-to-joi

This package is really easy to use, see example below:

    import tsToJoi from 'typescript-schema-to-joi';

    const config = {
        path: './folder/toto.ts', // The path to the file in which the type is
        tsconfig: './tsconfig.json', // The path to your typescript config
        type: 'MyType', // The name of the typ
    };

    const schema = tsToJoi(config);

    const payload = {
      test: true
    }

    const { error } = schema.validate(payload)
    if (error) {
      console.log('invalid format')
    } else {
      console.log('format is valid')
    }

You also have the possiblity to have json type descriptor generated by giving true as second parameter:

  tsToJoi(
    config,
    true,
    './path/to/write/file.json' // optional third param, it will write the file in the same folder as the type by default
  )

typescript-schema-to-joi's People

Contributors

mtbrault avatar

Stargazers

Benjamin Besse avatar  avatar

Watchers

 avatar

typescript-schema-to-joi's Issues

Patch a schema to allow empty strings

Hello,

I have a type like that:

export type PatchDocumentParams = {
  name?: string
  date?: number
  notes?: string
}

and a schema loaded like that:

const PatchDocumentInputSchema = tsToJoi({
  path: "./src/models/payloads.ts",
  tsconfig: "./tsconfig.json",
  type: "PatchDocumentParams"
})

The problem is: when I receive a payload with:

{ "notes": "" }

I get a validation error: "notes" is not allowed to be empty

How can I path the schema to allow a field to be empty?

Thank you!

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.