phantasy-validation
Version:
Type-safe validation
69 lines (56 loc) • 2.12 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ref = exports.union = exports.maybe = exports.partial = exports.obj = exports.array = exports.enumeration = exports.str = exports.num = exports.bool = exports.any = undefined;
exports.validationError = validationError;
exports.validateJson = validateJson;
exports.validateValue = validateValue;
var _schema = require('validated/schema');
var t = _interopRequireWildcard(_schema);
var _phantasy = require('phantasy');
var _json = require('validated/json5');
var _object = require('validated/object');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
/**
* `validationError :: Error -> mixed -> ValidationError`
*/
function validationError(err, validated) {
return {
message: err.message,
validated: validated,
originalError: err
};
}
/**
* `validateJson :: Node t -> string -> Result t ValidationError`
*/
function validateJson(schema, json) {
return _phantasy.Result.fromThrowable(function () {
return (0, _json.validate)(schema, json);
}).handleError(function (err) {
return _phantasy.Result.Err(validationError(err, json));
});
}
/**
* `validateValue :: Node t -> mixed -> Result t ValidationError`
*/
function validateValue(schema, value) {
return _phantasy.Result.fromThrowable(function () {
return (0, _object.validate)(schema, value);
}).handleError(function (err) {
return _phantasy.Result.Err(validationError(err, value));
});
}
// run-time types
var any = exports.any = t.any;
var bool = exports.bool = t.boolean;
var num = exports.num = t.number;
var str = exports.str = t.string;
var enumeration = exports.enumeration = t.enumeration;
var array = exports.array = t.arrayOf;
var obj = exports.obj = t.object;
var partial = exports.partial = t.partialObject;
var maybe = exports.maybe = t.maybe;
var union = exports.union = t.oneOf;
var ref = exports.ref = t.ref;
;