Sha256: fcb6627d5e968498781731c37d3e3ce34b737608c475d3d3bbcd9cd177bd78aa

Contents?: true

Size: 1.55 KB

Versions: 28

Compression:

Stored size: 1.55 KB

Contents

/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Gajus Kuizinas @gajus
*/
"use strict";

const Ajv = require("ajv");
const ajv = new Ajv({
	errorDataPath: "configuration",
	allErrors: true,
	verbose: true
});
require("ajv-keywords")(ajv, ["instanceof"]);
require("../schemas/ajv.absolutePath")(ajv);

const validateSchema = (schema, options) => {
	if (Array.isArray(options)) {
		const errors = options.map(options => validateObject(schema, options));
		errors.forEach((list, idx) => {
			const applyPrefix = err => {
				err.dataPath = `[${idx}]${err.dataPath}`;
				if (err.children) {
					err.children.forEach(applyPrefix);
				}
			};
			list.forEach(applyPrefix);
		});
		return errors.reduce((arr, items) => {
			return arr.concat(items);
		}, []);
	} else {
		return validateObject(schema, options);
	}
};

const validateObject = (schema, options) => {
	const validate = ajv.compile(schema);
	const valid = validate(options);
	return valid ? [] : filterErrors(validate.errors);
};

const filterErrors = errors => {
	let newErrors = [];
	for (const err of errors) {
		const dataPath = err.dataPath;
		let children = [];
		newErrors = newErrors.filter(oldError => {
			if (oldError.dataPath.includes(dataPath)) {
				if (oldError.children) {
					children = children.concat(oldError.children.slice(0));
				}
				oldError.children = undefined;
				children.push(oldError);
				return false;
			}
			return true;
		});
		if (children.length) {
			err.children = children;
		}
		newErrors.push(err);
	}

	return newErrors;
};

module.exports = validateSchema;

Version data entries

28 entries across 27 versions & 9 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/webpack/lib/validateSchema.js
disco_app-0.18.0 test/dummy/node_modules/webpack/lib/validateSchema.js
disco_app-0.18.2 test/dummy/node_modules/webpack/lib/validateSchema.js
disco_app-0.16.1 test/dummy/node_modules/webpack/lib/validateSchema.js
disco_app-0.15.2 test/dummy/node_modules/webpack/lib/validateSchema.js
disco_app-0.18.4 test/dummy/node_modules/webpack/lib/validateSchema.js
disco_app-0.18.1 test/dummy/node_modules/webpack/lib/validateSchema.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/webpack/lib/validateSchema.js
disco_app-0.14.0 test/dummy/node_modules/webpack/lib/validateSchema.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/webpack/lib/validateSchema.js
tang-0.2.1 spec/tang_app/node_modules/webpack/lib/validateSchema.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/webpack/lib/validateSchema.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/webpack/lib/validateSchema.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/webpack/lib/validateSchema.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/webpack/lib/validateSchema.js
tang-0.2.0 spec/tang_app/node_modules/webpack/lib/validateSchema.js
tang-0.1.0 spec/tang_app/node_modules/webpack/lib/validateSchema.js
tang-0.0.9 spec/tang_app/node_modules/webpack/lib/validateSchema.js
enju_library-0.3.8 spec/dummy/node_modules/webpack/lib/validateSchema.js
jester-data-8.0.0 node_modules/webpack/lib/validateSchema.js