validation.ts
Advanced tools
Comparing version 0.0.25 to 0.0.26
@@ -7,2 +7,4 @@ import { Result, Ok, Err, Option, None, Some } from 'space-lift'; | ||
filter(fn: (value: T) => boolean): Validator<T>; | ||
flatMap<B>(fn: (value: T) => Result<string, B>): Validator<B>; | ||
transform<B>(fn: (result: Validation<T>) => Result<string | ValidationError[], B>): Validator<B>; | ||
tagged<TAG extends string>(this: Validator<string>): Validator<TAG>; | ||
@@ -49,16 +51,8 @@ tagged<TAG extends number>(this: Validator<number>): Validator<TAG>; | ||
} | ||
export declare class MappedValidator<A, B> extends Validator<B> { | ||
export declare class TransformValidator<A, B> extends Validator<B> { | ||
private validator; | ||
private f; | ||
constructor(validator: Validator<A>, f: (a: A) => B); | ||
constructor(validator: Validator<A>, f: (a: Validation<A>) => Result<string | ValidationError[], B>); | ||
validate(v: Value, config?: Configuration, c?: Context): Result<ValidationError[], B>; | ||
} | ||
export declare function map<A, B>(validator: Validator<A>, f: (a: A) => B): MappedValidator<A, B>; | ||
export declare class FilteredValidator<A> extends Validator<A> { | ||
private validator; | ||
private predicate; | ||
constructor(validator: Validator<A>, predicate: (a: A) => boolean); | ||
validate(v: Value, config?: Configuration, c?: Context): Result<ValidationError[], A>; | ||
} | ||
export declare function filter<A>(validator: Validator<A>, predicate: (a: A) => boolean): FilteredValidator<A>; | ||
export declare class ArrayValidator<A> extends Validator<A[]> { | ||
@@ -171,12 +165,9 @@ private validator; | ||
export declare function recursion<T>(definition: (self: Validator<T>) => Any): Validator<T>; | ||
export declare class IsoDateValidator extends Validator<Date> { | ||
validate(v: Value, _config?: Configuration, c?: Context): Result<ValidationError[], Date>; | ||
} | ||
export declare function errorDebugString(errors: ValidationError[]): string; | ||
declare const nullValidation: NullValidator; | ||
declare const undefinedValidation: UndefinedValidator; | ||
export { nullValidation as null, undefinedValidation as undefined }; | ||
declare const nullValidator: NullValidator; | ||
declare const undefinedValidator: UndefinedValidator; | ||
export { nullValidator as null, undefinedValidator as undefined }; | ||
export declare const string: StringValidator; | ||
export declare const number: NumberValidator; | ||
export declare const boolean: BooleanValidator; | ||
export declare const isoDate: IsoDateValidator; | ||
export declare const isoDate: Validator<Date>; |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -37,7 +37,13 @@ extendStatics(d, b); | ||
Validator.prototype.map = function (fn) { | ||
return new MappedValidator(this, fn); | ||
return this.flatMap(function (v) { return space_lift_1.Ok(fn(v)); }); | ||
}; | ||
Validator.prototype.filter = function (fn) { | ||
return new FilteredValidator(this, fn); | ||
return this.flatMap(function (v) { return fn(v) ? space_lift_1.Ok(v) : space_lift_1.Err("The value " + pretty(v) + " failed a predicate\""); }); | ||
}; | ||
Validator.prototype.flatMap = function (fn) { | ||
return this.transform(function (r) { return r.isOk() ? fn(r.get()) : r; }); | ||
}; | ||
Validator.prototype.transform = function (fn) { | ||
return new TransformValidator(this, fn); | ||
}; | ||
Validator.prototype.tagged = function () { | ||
@@ -156,7 +162,7 @@ return this; | ||
//-------------------------------------- | ||
// map | ||
// transform | ||
//-------------------------------------- | ||
var MappedValidator = /** @class */ (function (_super) { | ||
__extends(MappedValidator, _super); | ||
function MappedValidator(validator, f) { | ||
var TransformValidator = /** @class */ (function (_super) { | ||
__extends(TransformValidator, _super); | ||
function TransformValidator(validator, f) { | ||
var _this = _super.call(this) || this; | ||
@@ -167,48 +173,17 @@ _this.validator = validator; | ||
} | ||
MappedValidator.prototype.validate = function (v, config, c) { | ||
TransformValidator.prototype.validate = function (v, config, c) { | ||
if (config === void 0) { config = defaultConfig; } | ||
if (c === void 0) { c = rootContext; } | ||
return this.validator.validate(v, config, c).map(this.f); | ||
}; | ||
return MappedValidator; | ||
}(Validator)); | ||
exports.MappedValidator = MappedValidator; | ||
function map(validator, f) { | ||
return new MappedValidator(validator, f); | ||
} | ||
exports.map = map; | ||
//-------------------------------------- | ||
// filter | ||
//-------------------------------------- | ||
var FilteredValidator = /** @class */ (function (_super) { | ||
__extends(FilteredValidator, _super); | ||
function FilteredValidator(validator, predicate) { | ||
var _this = _super.call(this) || this; | ||
_this.validator = validator; | ||
_this.predicate = predicate; | ||
return _this; | ||
} | ||
FilteredValidator.prototype.validate = function (v, config, c) { | ||
var _this = this; | ||
if (config === void 0) { config = defaultConfig; } | ||
if (c === void 0) { c = rootContext; } | ||
var validated = this.validator.validate(v, config, c); | ||
return validated.flatMap(function (v) { | ||
if (_this.predicate(v)) | ||
return validated; | ||
var predicateName = _this.predicate.name; | ||
if (!predicateName) { | ||
var functionStr = _this.predicate.toString(); | ||
predicateName = functionStr.length > 60 ? functionStr.slice(0, 60) + '...' : functionStr; | ||
} | ||
return failure(c, "The value " + pretty(v) + " failed the predicate \"" + predicateName + "\""); | ||
}); | ||
var transformed = this.f(validated); | ||
if (transformed.isOk()) | ||
return success(transformed.get()); | ||
var error = transformed.get(); | ||
if (typeof error === 'string') | ||
return failure(c, error); | ||
return space_lift_1.Err(error); | ||
}; | ||
return FilteredValidator; | ||
return TransformValidator; | ||
}(Validator)); | ||
exports.FilteredValidator = FilteredValidator; | ||
function filter(validator, predicate) { | ||
return new FilteredValidator(validator, predicate); | ||
} | ||
exports.filter = filter; | ||
exports.TransformValidator = TransformValidator; | ||
//-------------------------------------- | ||
@@ -266,3 +241,3 @@ // array | ||
if (v.length !== this.validators.length) | ||
return failure(c, "Expected a Tuple" + this.validators.length + " but got a Tuple" + v.length); | ||
return failure(c, "Expected Tuple" + this.validators.length + ", got Tuple" + v.length); | ||
var validatedArray = []; | ||
@@ -383,3 +358,3 @@ var errors = []; | ||
var error = domainValidation.get(); | ||
pushAll(errors, error.map(function (e) { return ({ context: context, message: "Error validating the key. " + e.message }); })); | ||
pushAll(errors, error.map(function (e) { return ({ context: context, message: "key error: " + e.message }); })); | ||
} | ||
@@ -391,3 +366,3 @@ if (codomainValidation.isOk()) { | ||
var error = codomainValidation.get(); | ||
pushAll(errors, error.map(function (e) { return ({ context: context, message: "Error validating the value. " + e.message }); })); | ||
pushAll(errors, error.map(function (e) { return ({ context: context, message: "value error: " + e.message }); })); | ||
} | ||
@@ -420,3 +395,3 @@ }; | ||
? success(v) | ||
: failure(c, "Expected literal value " + this.value + " but found " + pretty(v)); | ||
: failure(c, "Expected literal " + this.value + " but got " + pretty(v)); | ||
}; | ||
@@ -446,3 +421,3 @@ return LiteralValidator; | ||
if (validation.isOk()) { | ||
result = __assign({}, result, validation.get()); | ||
result = __assign(__assign({}, result), validation.get()); | ||
} | ||
@@ -589,23 +564,2 @@ else { | ||
//-------------------------------------- | ||
// isoDate | ||
//-------------------------------------- | ||
var IsoDateValidator = /** @class */ (function (_super) { | ||
__extends(IsoDateValidator, _super); | ||
function IsoDateValidator() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
IsoDateValidator.prototype.validate = function (v, _config, c) { | ||
if (_config === void 0) { _config = defaultConfig; } | ||
if (c === void 0) { c = rootContext; } | ||
if (typeof v !== 'string') | ||
return typeFailure(v, c, 'string'); | ||
var date = new Date(v); | ||
return isNaN(date.getTime()) | ||
? failure(c, "Expected an ISO date but got: " + pretty(v)) | ||
: success(date); | ||
}; | ||
return IsoDateValidator; | ||
}(Validator)); | ||
exports.IsoDateValidator = IsoDateValidator; | ||
//-------------------------------------- | ||
// util | ||
@@ -626,9 +580,14 @@ //-------------------------------------- | ||
//-------------------------------------- | ||
var nullValidation = new NullValidator(); | ||
exports["null"] = nullValidation; | ||
var undefinedValidation = new UndefinedValidator(); | ||
exports.undefined = undefinedValidation; | ||
var nullValidator = new NullValidator(); | ||
exports["null"] = nullValidator; | ||
var undefinedValidator = new UndefinedValidator(); | ||
exports.undefined = undefinedValidator; | ||
exports.string = new StringValidator(); | ||
exports.number = new NumberValidator(); | ||
exports.boolean = new BooleanValidator(); | ||
exports.isoDate = new IsoDateValidator(); | ||
exports.isoDate = exports.string.flatMap(function (str) { | ||
var date = new Date(str); | ||
return isNaN(date.getTime()) | ||
? space_lift_1.Err("Expected an ISO date but got: " + pretty(str)) | ||
: space_lift_1.Ok(date); | ||
}); |
@@ -7,2 +7,4 @@ import { Result, Ok, Err, Option, None, Some } from 'space-lift'; | ||
filter(fn: (value: T) => boolean): Validator<T>; | ||
flatMap<B>(fn: (value: T) => Result<string, B>): Validator<B>; | ||
transform<B>(fn: (result: Validation<T>) => Result<string | ValidationError[], B>): Validator<B>; | ||
tagged<TAG extends string>(this: Validator<string>): Validator<TAG>; | ||
@@ -49,16 +51,8 @@ tagged<TAG extends number>(this: Validator<number>): Validator<TAG>; | ||
} | ||
export declare class MappedValidator<A, B> extends Validator<B> { | ||
export declare class TransformValidator<A, B> extends Validator<B> { | ||
private validator; | ||
private f; | ||
constructor(validator: Validator<A>, f: (a: A) => B); | ||
constructor(validator: Validator<A>, f: (a: Validation<A>) => Result<string | ValidationError[], B>); | ||
validate(v: Value, config?: Configuration, c?: Context): Result<ValidationError[], B>; | ||
} | ||
export declare function map<A, B>(validator: Validator<A>, f: (a: A) => B): MappedValidator<A, B>; | ||
export declare class FilteredValidator<A> extends Validator<A> { | ||
private validator; | ||
private predicate; | ||
constructor(validator: Validator<A>, predicate: (a: A) => boolean); | ||
validate(v: Value, config?: Configuration, c?: Context): Result<ValidationError[], A>; | ||
} | ||
export declare function filter<A>(validator: Validator<A>, predicate: (a: A) => boolean): FilteredValidator<A>; | ||
export declare class ArrayValidator<A> extends Validator<A[]> { | ||
@@ -171,12 +165,9 @@ private validator; | ||
export declare function recursion<T>(definition: (self: Validator<T>) => Any): Validator<T>; | ||
export declare class IsoDateValidator extends Validator<Date> { | ||
validate(v: Value, _config?: Configuration, c?: Context): Result<ValidationError[], Date>; | ||
} | ||
export declare function errorDebugString(errors: ValidationError[]): string; | ||
declare const nullValidation: NullValidator; | ||
declare const undefinedValidation: UndefinedValidator; | ||
export { nullValidation as null, undefinedValidation as undefined }; | ||
declare const nullValidator: NullValidator; | ||
declare const undefinedValidator: UndefinedValidator; | ||
export { nullValidator as null, undefinedValidator as undefined }; | ||
export declare const string: StringValidator; | ||
export declare const number: NumberValidator; | ||
export declare const boolean: BooleanValidator; | ||
export declare const isoDate: IsoDateValidator; | ||
export declare const isoDate: Validator<Date>; |
@@ -7,3 +7,3 @@ var __extends = (this && this.__extends) || (function () { | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -35,7 +35,13 @@ extendStatics(d, b); | ||
Validator.prototype.map = function (fn) { | ||
return new MappedValidator(this, fn); | ||
return this.flatMap(function (v) { return Ok(fn(v)); }); | ||
}; | ||
Validator.prototype.filter = function (fn) { | ||
return new FilteredValidator(this, fn); | ||
return this.flatMap(function (v) { return fn(v) ? Ok(v) : Err("The value " + pretty(v) + " failed a predicate\""); }); | ||
}; | ||
Validator.prototype.flatMap = function (fn) { | ||
return this.transform(function (r) { return r.isOk() ? fn(r.get()) : r; }); | ||
}; | ||
Validator.prototype.transform = function (fn) { | ||
return new TransformValidator(this, fn); | ||
}; | ||
Validator.prototype.tagged = function () { | ||
@@ -149,7 +155,7 @@ return this; | ||
//-------------------------------------- | ||
// map | ||
// transform | ||
//-------------------------------------- | ||
var MappedValidator = /** @class */ (function (_super) { | ||
__extends(MappedValidator, _super); | ||
function MappedValidator(validator, f) { | ||
var TransformValidator = /** @class */ (function (_super) { | ||
__extends(TransformValidator, _super); | ||
function TransformValidator(validator, f) { | ||
var _this = _super.call(this) || this; | ||
@@ -160,46 +166,17 @@ _this.validator = validator; | ||
} | ||
MappedValidator.prototype.validate = function (v, config, c) { | ||
TransformValidator.prototype.validate = function (v, config, c) { | ||
if (config === void 0) { config = defaultConfig; } | ||
if (c === void 0) { c = rootContext; } | ||
return this.validator.validate(v, config, c).map(this.f); | ||
}; | ||
return MappedValidator; | ||
}(Validator)); | ||
export { MappedValidator }; | ||
export function map(validator, f) { | ||
return new MappedValidator(validator, f); | ||
} | ||
//-------------------------------------- | ||
// filter | ||
//-------------------------------------- | ||
var FilteredValidator = /** @class */ (function (_super) { | ||
__extends(FilteredValidator, _super); | ||
function FilteredValidator(validator, predicate) { | ||
var _this = _super.call(this) || this; | ||
_this.validator = validator; | ||
_this.predicate = predicate; | ||
return _this; | ||
} | ||
FilteredValidator.prototype.validate = function (v, config, c) { | ||
var _this = this; | ||
if (config === void 0) { config = defaultConfig; } | ||
if (c === void 0) { c = rootContext; } | ||
var validated = this.validator.validate(v, config, c); | ||
return validated.flatMap(function (v) { | ||
if (_this.predicate(v)) | ||
return validated; | ||
var predicateName = _this.predicate.name; | ||
if (!predicateName) { | ||
var functionStr = _this.predicate.toString(); | ||
predicateName = functionStr.length > 60 ? functionStr.slice(0, 60) + '...' : functionStr; | ||
} | ||
return failure(c, "The value " + pretty(v) + " failed the predicate \"" + predicateName + "\""); | ||
}); | ||
var transformed = this.f(validated); | ||
if (transformed.isOk()) | ||
return success(transformed.get()); | ||
var error = transformed.get(); | ||
if (typeof error === 'string') | ||
return failure(c, error); | ||
return Err(error); | ||
}; | ||
return FilteredValidator; | ||
return TransformValidator; | ||
}(Validator)); | ||
export { FilteredValidator }; | ||
export function filter(validator, predicate) { | ||
return new FilteredValidator(validator, predicate); | ||
} | ||
export { TransformValidator }; | ||
//-------------------------------------- | ||
@@ -256,3 +233,3 @@ // array | ||
if (v.length !== this.validators.length) | ||
return failure(c, "Expected a Tuple" + this.validators.length + " but got a Tuple" + v.length); | ||
return failure(c, "Expected Tuple" + this.validators.length + ", got Tuple" + v.length); | ||
var validatedArray = []; | ||
@@ -370,3 +347,3 @@ var errors = []; | ||
var error = domainValidation.get(); | ||
pushAll(errors, error.map(function (e) { return ({ context: context, message: "Error validating the key. " + e.message }); })); | ||
pushAll(errors, error.map(function (e) { return ({ context: context, message: "key error: " + e.message }); })); | ||
} | ||
@@ -378,3 +355,3 @@ if (codomainValidation.isOk()) { | ||
var error = codomainValidation.get(); | ||
pushAll(errors, error.map(function (e) { return ({ context: context, message: "Error validating the value. " + e.message }); })); | ||
pushAll(errors, error.map(function (e) { return ({ context: context, message: "value error: " + e.message }); })); | ||
} | ||
@@ -406,3 +383,3 @@ }; | ||
? success(v) | ||
: failure(c, "Expected literal value " + this.value + " but found " + pretty(v)); | ||
: failure(c, "Expected literal " + this.value + " but got " + pretty(v)); | ||
}; | ||
@@ -431,3 +408,3 @@ return LiteralValidator; | ||
if (validation.isOk()) { | ||
result = __assign({}, result, validation.get()); | ||
result = __assign(__assign({}, result), validation.get()); | ||
} | ||
@@ -569,23 +546,2 @@ else { | ||
//-------------------------------------- | ||
// isoDate | ||
//-------------------------------------- | ||
var IsoDateValidator = /** @class */ (function (_super) { | ||
__extends(IsoDateValidator, _super); | ||
function IsoDateValidator() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
IsoDateValidator.prototype.validate = function (v, _config, c) { | ||
if (_config === void 0) { _config = defaultConfig; } | ||
if (c === void 0) { c = rootContext; } | ||
if (typeof v !== 'string') | ||
return typeFailure(v, c, 'string'); | ||
var date = new Date(v); | ||
return isNaN(date.getTime()) | ||
? failure(c, "Expected an ISO date but got: " + pretty(v)) | ||
: success(date); | ||
}; | ||
return IsoDateValidator; | ||
}(Validator)); | ||
export { IsoDateValidator }; | ||
//-------------------------------------- | ||
// util | ||
@@ -605,8 +561,13 @@ //-------------------------------------- | ||
//-------------------------------------- | ||
var nullValidation = new NullValidator(); | ||
var undefinedValidation = new UndefinedValidator(); | ||
export { nullValidation as null, undefinedValidation as undefined }; | ||
var nullValidator = new NullValidator(); | ||
var undefinedValidator = new UndefinedValidator(); | ||
export { nullValidator as null, undefinedValidator as undefined }; | ||
export var string = new StringValidator(); | ||
export var number = new NumberValidator(); | ||
export var boolean = new BooleanValidator(); | ||
export var isoDate = new IsoDateValidator(); | ||
export var isoDate = string.flatMap(function (str) { | ||
var date = new Date(str); | ||
return isNaN(date.getTime()) | ||
? Err("Expected an ISO date but got: " + pretty(str)) | ||
: Ok(date); | ||
}); |
{ | ||
"name": "validation.ts", | ||
"version": "0.0.25", | ||
"version": "0.0.26", | ||
"description": "Validation for TypeScript", | ||
@@ -19,3 +19,3 @@ "license": "MIT", | ||
"mocha": "2.2.5", | ||
"typescript": "3.1.1" | ||
"typescript": "3.7.4" | ||
}, | ||
@@ -22,0 +22,0 @@ |
# validation.ts | ||
Validation for TypeScript | ||
Validation for TypeScript | ||
This module helps validating incoming JSON, url params, etc in a type safe manner. | ||
## validate | ||
@@ -137,8 +139,11 @@ | ||
## map, filter | ||
## map, filter, flatMap | ||
```ts | ||
import { map, filter, string } from 'validation.ts' | ||
import { string, Ok } from 'validation.ts' | ||
const validator = string.filter(str => str.length > 3).map(str => `${str}...`) | ||
const validator = string | ||
.filter(str => str.length > 3) | ||
.map(str => `${str}...`) | ||
.flatMap(str => Ok(str.toLowerCase())) | ||
``` | ||
@@ -145,0 +150,0 @@ |
210
76276
1488