Negotiate Pushing Milestone
Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

joi-schema-to-file

0.0.6 • Public • Published

joi-schema-to-file

Take any JavaScript object and output a joi schema to an importable module.

Thanks to Alan Shaw for the underlying work on joi-machine, which this module wraps.

Install

npm i joi-schema-to-file

Usage

import { generateSchema } from 'joi-schema-to-file'
 
// My JavaScript object
const myObj = {
  stringKey: 'string',
  numberKey: 1,
  objectKey: {
    str: 'str'
  },
  arrayKey:[
    'string1',
    'string2'
  ]
}
 
// Specificy the full path to the file you want to create
 
const fileNameAndPath = `${__dirname}/import-export-output.js`
await generateSchema(myObj, {
  stdOut: false,
  fileNameAndPath,
  jsModuleOptions: {
    importType: 'import',
    joiOrHapiJoi: 'joi',
    exportType: 'export',
    schemaName: 'myObjSchema'
  }
});
 
const fileNameAndPath = `${__dirname}/require-module-output.js`
await generateSchema(myObj, {
  stdOut: false,
  fileNameAndPath,
  jsModuleOptions: {
    importType: 'require',
    joiOrHapiJoi: '@hapi/joi',
    exportType: 'module',
    schemaName: 'myObjSchema'
  }
});
 
  const fileNameAndPath = `${__dirname}/schema-only-output.js`
  await generateSchema(myObj, {
    stdOut: false,
    fileNameAndPath,
  });
})

Generates the following three files in the specified directory with the following:

import Joi from 'joi'
 
export const myObjSchema = Joi.object().keys({
    stringKey: Joi.string(),
    numberKey: Joi.number().integer(),
    objectKey: Joi.object().keys({
        str: Joi.string()
    }),
    arrayKey: Joi.array().items(Joi.string(), Joi.string())
})
const Joi = require('@hapi/joi')
 
exports.myObjSchema = Joi.object().keys({
    stringKey: Joi.string(),
    numberKey: Joi.number().integer(),
    objectKey: Joi.object().keys({
        str: Joi.string()
    }),
    arrayKey: Joi.array().items(Joi.string(), Joi.string())
})
Joi.object().keys({
    stringKey: Joi.string(),
    numberKey: Joi.number().integer(),
    objectKey: Joi.object().keys({
        str: Joi.string()
    }),
    arrayKey: Joi.array().items(Joi.string(), Joi.string())
})

One test included in the github repo that generates the above.

Keywords

none

Install

npm i joi-schema-to-file

DownloadsWeekly Downloads

0

Version

0.0.6

License

ISC

Unpacked Size

7.23 kB

Total Files

4

Last publish

Collaborators

  • avatar