Skip to content

Instantly share code, notes, and snippets.

@vnglst

vnglst/example.js

Last active Jan 30, 2018
Embed
What would you like to do?
JSON Schema function validation
const api = {};
const ajv = new Ajv({
coerceTypes: true,
useDefaults: true,
allErrors: true,
});
api.api = (schema, handler) => {
const validator = ajv.compile(schema);
return (event, callback) => {
const data = event.data;
const valid = validator(data);
if (!valid) {
// callback or promise
if (!callback) {
return Bluebird.reject(new errors.INVALID_DATA(validator.errors));
}
return callback(new errors.INVALID_DATA(validator.errors));
}
// callback or promise
if (!callback) {
return Bluebird.promisify(handler)(event);
}
return handler(event, callback);
};
};
fnChecker = (schema, fn) => {
let _schema = schemas[schema];
return function (...args) {
if (schema.validate(args)) {
return fn.apply(fn, args);
}
throw new SchemaValidationError(schema.errors);
}
}
const schemas = {
'callUser': {
...
}
};
const callUser = createRemoteUserApiMethod('callUser', (name, language) => {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment