@hsui/sdk
Version:
Hundsun frontend framework JSSDK
34 lines • 1.04 kB
JavaScript
import jsonpointer from './jsonpointer';
import validator from './is-json-valid';
export function validate(schema, data) {
var it = validator(schema, {
verbose: true
});
it(data);
var errors = it.errors;
if (errors) {
errors = errors.map(function (_ref) {
var field = _ref.field,
message = _ref.message,
value = _ref.value,
type = _ref.type,
schemaPath = _ref.schemaPath;
var property = field.replace('data.', '');
if (Array.isArray(type)) {
type = type.join();
}
if (message === 'is required') {
return "".concat(property, " is required");
}
if (message === 'is the wrong type') {
return "".concat(property, " is the wrong type, expect ").concat(type);
}
if (message === 'must be an enum value') {
var enumvalue = jsonpointer.get(schema, schemaPath.concat('enum')).join();
return "".concat(property, " must be one of ").concat(enumvalue);
}
return message;
});
}
return errors;
}