Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const genParser = () => {
const options = Object.assign( { greedy: true, verbose: true }, schemaOptions );
const validator = schemaValidator( schema, options );
// filter out unwanted properties even though we may have let them slip past validation
// note: this property does not nest deeply into the data structure, that is, properties
// of a property that aren't in the schema could still come through since only the top
// level of properties are pruned
const filter = schemaValidator.filter(
Object.assign(
{},
schema,
schema.type && schema.type === 'object' && { additionalProperties: false }
)
);
validate = data => {
if ( ! validator( data ) ) {
if ( 'development' === process.env.NODE_ENV ) {
// eslint-disable-next-line no-console
console.warn( 'JSON Validation Failure' );
validator.errors.forEach( error =>
// eslint-disable-next-line no-console
console.warn( {
function validate (data, schema = {}, options = {}) {
const schemaObj = schema.type ? schema : Object.assign({}, schemaPattern, {properties: schema});
const formats = options.formats ? Object.assign({}, customFormats, options.formats) : customFormats;
const validator = createValidator(schemaObj, { formats });
if (options.filter) {
const filter = createValidator.filter(schemaObj);
data = filter(data);
}
const validatedData = validator(data);
if (!validatedData) {
return {
valid: false,
errors: _parseValidatorErrors(validator.errors, {
title: schema.title,
debug: options.debug
})
};
}
if (validatedData && options.filterReadonly) {
const readonlyProperties = traverse(schemaObj).reduce(function (memo, value) {
validator.filter = ( schema, options ) => {
throwOnInvalidSchema( schema );
return imjv.filter( schema, options );
};