https://github.com/wspringer/njoi
An attempt to get some sensible documentation from Joi.
https://github.com/wspringer/njoi
Last synced: 4 months ago
JSON representation
An attempt to get some sensible documentation from Joi.
- Host: GitHub
- URL: https://github.com/wspringer/njoi
- Owner: wspringer
- Created: 2019-12-22T22:08:37.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-28T14:33:32.000Z (about 3 years ago)
- Last Synced: 2025-10-21T16:47:06.609Z (8 months ago)
- Language: JavaScript
- Size: 914 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.js.md
Awesome Lists containing this project
README
```javascript --hide
runmd.onRequire = function(path) {
if (path === 'njoi') {
return './lib/njoi.js';
} else {
return path;
}
}
```
# README
An experiment to render a sensible JSON-alike structure from the Joi definition
to be copied in to (Github) fenced markdown sections.
If this is the Joi schema you're defining:
```javascript --run simple
const Joi = require('@hapi/joi');
const schema = Joi.object().keys({
name: Joi.string().description('The given name').required(),
age: Joi.number().description('The age').default(5),
tags: Joi.array().items(Joi.string()).description('A set of tags to be a associated'),
address: Joi.object().keys({
street: Joi.string().description('The street'),
houseNumber: Joi.string().description('The housenumber.'),
type: Joi.string().valid('condo', 'appartment', 'mansion'),
city: Joi.string().description('The city')
}).label('Address'),
education: Joi.array().items(Joi.object().keys({
school: Joi.string().description('The school attended'),
degree: Joi.boolean().description('Got the degree or not')
}).label('Education'))
}).label('Person');
```
… then you will be able to generate something a little easier on the eyes using:
```javascript --run simple
const njoi = require('njoi');
console.log(njoi.jsonish()(schema));
```
Or one including comments:
```javascript --run simple
console.log(njoi.jsonish({comments: true})(schema));
```
Or a markdown breakdown using:
```javascript --run simple
console.log(njoi.markdown()(schema));
```
Or a JSDoc breakdown using:
```javascript --run simple
console.log(njoi.jsdoc()(schema));
```
If you want, then can pass in a callback to have the ability to render some
additional lines:
```javascript --run simple
const envVariable = (node, context) => {
if (context && context.indexOf('[]') < 0) {
const name = context.split('.').map(str => str.toUpperCase()).join('_');
return `Use the \`${name}\` environment variable to override this setting.`
} else {
return void 0;
}
}
console.log(njoi.markdown({extra: envVariable})(schema));
```