|
|
import schemaValidator from 'is-my-json-valid'; |
|
|
import { get } from 'lodash'; |
|
|
|
|
|
export class SchemaError extends Error { |
|
|
constructor( errors ) { |
|
|
super( 'Failed to validate with JSON schema' ); |
|
|
this.schemaErrors = errors; |
|
|
} |
|
|
} |
|
|
|
|
|
export class TransformerError extends Error { |
|
|
constructor( error, data, transformer ) { |
|
|
super( error.message ); |
|
|
this.inputData = data; |
|
|
this.transformer = transformer; |
|
|
} |
|
|
} |
|
|
|
|
|
const defaultTransformer = ( data ) => data; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function makeJsonSchemaParser( |
|
|
schema, |
|
|
transformer = defaultTransformer, |
|
|
schemaOptions = {} |
|
|
) { |
|
|
let transform; |
|
|
let validate; |
|
|
|
|
|
const genParser = () => { |
|
|
const options = Object.assign( { greedy: true, verbose: true }, schemaOptions ); |
|
|
const validator = schemaValidator( schema, options ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ) { |
|
|
|
|
|
console.warn( 'JSON Validation Failure' ); |
|
|
|
|
|
validator.errors.forEach( ( error ) => |
|
|
|
|
|
console.warn( { |
|
|
field: error.field, |
|
|
message: error.message, |
|
|
value: error.value, |
|
|
actualType: error.type, |
|
|
expectedType: get( schema, error.schemaPath ), |
|
|
} ) |
|
|
); |
|
|
|
|
|
if ( undefined !== window ) { |
|
|
|
|
|
console.log( 'updated `lastValidator` and `lastValidated` in console' ); |
|
|
|
|
|
console.log( 'run `lastValidator( lastValidated )` to reproduce failing validation' ); |
|
|
window.lastValidator = validator; |
|
|
window.lastValidated = data; |
|
|
} |
|
|
} |
|
|
|
|
|
throw new SchemaError( validator.errors ); |
|
|
} |
|
|
|
|
|
return filter( data ); |
|
|
}; |
|
|
|
|
|
transform = ( data ) => { |
|
|
try { |
|
|
return transformer( data ); |
|
|
} catch ( e ) { |
|
|
if ( 'development' === process.env.NODE_ENV ) { |
|
|
|
|
|
console.warn( 'Data Transformation Failure' ); |
|
|
|
|
|
|
|
|
console.warn( { |
|
|
inputData: data, |
|
|
error: e, |
|
|
} ); |
|
|
|
|
|
if ( undefined !== window ) { |
|
|
|
|
|
console.log( 'updated `lastTransformer` and `lastTransformed` in console' ); |
|
|
|
|
|
console.log( |
|
|
'run `lastTransformer( lastTransformed )` to reproduce failing transform' |
|
|
); |
|
|
window.lastTransformer = transformer; |
|
|
window.lastTransformed = data; |
|
|
} |
|
|
} |
|
|
|
|
|
throw new TransformerError( e, data, transformer ); |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
return ( data ) => { |
|
|
if ( ! transform ) { |
|
|
genParser(); |
|
|
} |
|
|
|
|
|
return transform( validate( data ) ); |
|
|
}; |
|
|
} |
|
|
|
|
|
export default makeJsonSchemaParser; |
|
|
|