Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbes7878 committed Jan 2, 2020
0 parents commit e2f1e96
Show file tree
Hide file tree
Showing 28 changed files with 2,812 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"root": true,
"extends": "@hobbes7878/eslint-config",
"env": {
"browser": false,
"node": true,
"mocha": true
}
}
155 changes: 155 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@

# Created by https://www.gitignore.io/api/macos,node,linux
# Edit at https://www.gitignore.io/?templates=macos,node,linux

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# rollup.js default build output
dist/

# Uncomment the public line if your project uses Gatsby
# https://nextjs.org/blog/next-9-1#public-directory-support
# https://create-react-app.dev/docs/using-the-public-folder/#docsNav
# public

# Storybook build outputs
.out
.storybook-out

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# Temporary folders
tmp/
temp/

# End of https://www.gitignore.io/api/macos,node,linux
n
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# json-schema-prompts


```javascript

```
21 changes: 21 additions & 0 deletions async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const prompts = require('prompts');

const asyncFunc = async() => {
const response = await prompts({
type: 'number',
name: 'value',
message: 'How old are you?',
validate: value => value < 18 ? 'Nightclub is 18+ only' : true,
});

console.log('Answered!');

console.log(response); // => { value: 24 }
};

const runner = async() => {
await asyncFunc();
console.log('WAITS');
};

runner();
Empty file added cli.js
Empty file.
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const Ajv = require('ajv');
const evaluateError = require('./lib/evaluateError');

const ajv = new Ajv({ allErrors: true, verbose: true });

const handleErrors = async({ errors }, data, testValues) => {
// Execute each handler in series
for (const error of errors) {
await evaluateError(error, data, testValues);
}
};

const promptSchema = async(schema, data, testValues = {}, testOpts = { returnError: false, returnAfter: 0 }, maxRecursion = 20) => {
let valid = false;
let i = 0;
while (!valid && i < maxRecursion) {
valid = ajv.validate(schema, data);
if (!valid) {
await handleErrors(ajv, data, testValues);
if (testOpts.returnError && testOpts.returnAfter === i) return ajv.errors;
}
i += 1;
}
return data;
};

module.exports = promptSchema;
40 changes: 40 additions & 0 deletions lib/evaluateError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const handleMissingProperty = require('./handlers/missingProperty');
const handleInvalidNumber = require('./handlers/invalidNumber');
const handleInvalidString = require('./handlers/invalidString');
const handleInvalidBoolean = require('./handlers/invalidBoolean');
const handleMissingItem = require('./handlers/missingItem');
const handleInvalidNull = require('./handlers/invalidNull');

module.exports = async(error, data, testValues) => {
const { params, parentSchema, keyword } = error;
// Handles missing number, integer, string, boolean, array, object or null
if (params.missingProperty) {
return handleMissingProperty(error, data, testValues);
}
// Handles invalid number or integer
if (
parentSchema.type === 'number' ||
parentSchema.type === 'integer'
) {
return handleInvalidNumber(error, data, testValues);
}
// Handles invalid string
if (parentSchema.type === 'string') {
return handleInvalidString(error, data, testValues);
}
// Handles invalid boolean
if (parentSchema.type === 'boolean') {
return handleInvalidBoolean(error, data, testValues);
}
// Handles invalid null
if (parentSchema.type === 'null') {
return handleInvalidNull(error, data, testValues);
}
// Handles missing items
if (keyword === 'minItems') {
return handleMissingItem(error, data, testValues);
}
};

// Valid JSON schema types:
// number, integer, string, boolean, array, object or null
26 changes: 26 additions & 0 deletions lib/handlers/invalidBoolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const prepareObjectPath = require('../utils/prepareObjectPath');
const set = require('lodash/set');
const chalk = require('chalk');

module.exports = async(error, data, testValues) => {
const prompts = require('prompts');
const { dataPath, parentSchema } = error;
const updatePath = prepareObjectPath(dataPath);

if (updatePath in testValues) {
prompts.inject([testValues[updatePath]]);
}

const prompt = Object.assign(
{
type: 'confirm',
message: `Should ${chalk.green(updatePath)} be true?\n`,
},
parentSchema.prompt || {},
{ name: updatePath }
);

const answers = await prompts([prompt]);

set(data, updatePath, answers[updatePath]);
};
9 changes: 9 additions & 0 deletions lib/handlers/invalidNull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const prepareObjectPath = require('../utils/prepareObjectPath');
const set = require('lodash/set');

module.exports = async(error, data, testValues) => {
const { dataPath } = error;
const updatePath = prepareObjectPath(dataPath);

set(data, updatePath, null);
};
26 changes: 26 additions & 0 deletions lib/handlers/invalidNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const prepareObjectPath = require('../utils/prepareObjectPath');
const set = require('lodash/set');
const chalk = require('chalk');

module.exports = async(error, data, testValues) => {
const prompts = require('prompts');
const { dataPath, message, parentSchema } = error;
const updatePath = prepareObjectPath(dataPath);

if (updatePath in testValues) {
prompts.inject([testValues[updatePath]]);
}

const prompt = Object.assign(
{
type: 'number',
message: `What should the value of ${chalk.green(updatePath)} be?\n(${message})`,
},
parentSchema.prompt || {},
{ name: updatePath }
);

const answers = await prompts([prompt]);

set(data, updatePath, answers[updatePath]);
};
26 changes: 26 additions & 0 deletions lib/handlers/invalidString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const prepareObjectPath = require('../utils/prepareObjectPath');
const set = require('lodash/set');
const chalk = require('chalk');

module.exports = async(error, data, testValues) => {
const prompts = require('prompts');
const { dataPath, message, parentSchema } = error;
const updatePath = prepareObjectPath(dataPath);

if (updatePath in testValues) {
prompts.inject([testValues[updatePath]]);
}

const prompt = Object.assign(
{
type: 'text',
message: `What should the value of ${chalk.green(updatePath)} be?\n(${message})`,
},
parentSchema.prompt || {},
{ name: updatePath }
);

const answers = await prompts([prompt]);

set(data, updatePath, answers[updatePath]);
};
8 changes: 8 additions & 0 deletions lib/handlers/missingArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const prepareObjectPath = require('../utils/prepareObjectPath');
const set = require('lodash/set');

module.exports = (error, data) => {
const { params, dataPath } = error;
const updatePath = prepareObjectPath(dataPath, params.missingProperty);
set(data, updatePath, []);
};
30 changes: 30 additions & 0 deletions lib/handlers/missingBoolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const prepareObjectPath = require('../utils/prepareObjectPath');
const getArrayItemModifier = require('../utils/getArrayItemModifier');
const set = require('lodash/set');
const chalk = require('chalk');

module.exports = async(error, data, testValues) => {
const prompts = require('prompts');
const { params, schema, dataPath } = error;
const updatePath = prepareObjectPath(dataPath, params.missingProperty);

if (updatePath in testValues) {
prompts.inject([testValues[updatePath]]);
}

const prompt = Object.assign(
{
type: 'confirm',
message:
`Should the value of \
${chalk.green(params.missingProperty)} \
be true${getArrayItemModifier(updatePath)}?\n`,
},
schema.prompt || {},
{ name: updatePath }
);

const answers = await prompts([prompt]);

set(data, updatePath, answers[updatePath]);
};
Loading

0 comments on commit e2f1e96

Please sign in to comment.