1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | 1x 1x 1x 1x 1x 15x 15x 15x 1x 1x 1x 1x 1x 1x 5x 1x 4x 4x 1x 12x 1x 6x 1x 26x 1x 4x 1x 17x 1x 4x 1x 7x 1x 5x 1x 1x 1x 2x 2x 2x 1x 1x 1x 9x 1x 6x 6x 1x 17x 1x 7x 1x 16x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 1x 5x 5x 4x 1x 1x 1x 12x 12x 12x 1x 12x 12x 7x 7x 7x 3x 3x 4x 5x 5x 1x 1x 1x 26x 1x 20x 12x 8x 8x 1x 1x 1x 4x 4x 4x 1x 4x 1x 1x 1x 17x 17x 17x 17x 1x 62x 62x 61x 44x 13x 17x 17x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 4x 4x 3x 2x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 1x 6x 1x 1x 1x 5x 5x 5x 5x 5x 1x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 1x 1x 1x 9x 9x 9x 1x 10x 3x 7x 7x 1x 1x 1x 17x 1x 27x 17x 10x 10x 1x 1x 1x 7x 7x 7x 1x 9x 9x 2x 7x 9x 1x 8x 1x 1x 1x 16x 1x 19x 11x 8x 8x 1x 1x 1x 4x 4x 4x 1x 5x 1x 1x 1x 1x 1x 1x 1x 16x 62x 4x | "use strict"; /* tslint:disable:max-classes-per-file */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var NonEmptyArray_1 = require("fp-ts/lib/NonEmptyArray"); var OptionFn = require("fp-ts/lib/Option"); var ValidationFn = require("fp-ts/lib/Validation"); var Decoder = /** @class */ (function () { function Decoder() { } Decoder.andThen = function (decoderFn, decoder) { return new AndThenD(decoderFn, decoder); }; Decoder.andMap = function (decoder) { return function (fnInDecoder) { return Decoder.map2(pipe, decoder, fnInDecoder); }; }; Decoder.array = function (decoder) { return new ArrayD(decoder); }; Decoder.at = function (keys, decoder) { return keys.reduceRight(function (decoders, key) { return Decoder.field(key, decoders); }, decoder); }; Decoder.boolean = function () { return new BooleanD(); }; Decoder.fail = function (message) { return new FailD(message); }; Decoder.field = function (key, decoder) { return new FieldD(key, decoder); }; Decoder.index = function (key, decoder) { return new IndexD(key, decoder); }; Decoder.map = function (fn, decoder) { return new MapD(fn, decoder); }; Decoder.map2 = function (fn, decoderA, decoderB) { return new Map2D(fn, decoderA, decoderB); }; Decoder.map3 = function (fn, decoderA, decoderB, decoderC) { return new Map3D(fn, decoderA, decoderB, decoderC); }; Decoder.nonEmptyArray = function (decoder) { return Decoder.andThen(function (_a) { var head = _a[0], tail = _a.slice(1); if (head) { return Decoder.succeed(new NonEmptyArray_1.NonEmptyArray(head, tail)); } return Decoder.fail('Array must have at least one value'); }, Decoder.array(decoder)); }; Decoder.null = function (value) { return new NullD(value); }; Decoder.nullable = function (decoder) { var decoders = new NonEmptyArray_1.NonEmptyArray(Decoder.null(OptionFn.none), [Decoder.map(OptionFn.some, decoder)]); return Decoder.oneOf(decoders); }; Decoder.number = function () { return new NumberD(); }; Decoder.oneOf = function (decoders) { return new OneOfD(decoders); }; Decoder.string = function () { return new StringD(); }; Decoder.succeed = function (value) { return new SucceedD(value); }; Decoder.unknown = function () { return new UnknownD(); }; Decoder.prototype.run = function (value) { throw new Error('This method has to be implemented'); }; return Decoder; }()); exports.default = Decoder; var AndThenD = /** @class */ (function (_super) { __extends(AndThenD, _super); function AndThenD(decoderFn, decoder) { var _this = _super.call(this) || this; _this.decoder = decoder; _this.decoderFn = decoderFn; return _this; } AndThenD.prototype.run = function (value) { var _this = this; return this.decoder .run(value) .fold(function (errors) { return ValidationFn.failure(errors); }, function (success) { return _this.decoderFn(success).run(value); }); }; return AndThenD; }(Decoder)); var ArrayD = /** @class */ (function (_super) { __extends(ArrayD, _super); function ArrayD(decoder) { var _this = _super.call(this) || this; _this.decoder = decoder; return _this; } ArrayD.prototype.run = function (valueArray) { var _this = this; if (isArray(valueArray)) { return valueArray.reduce(function (validated, nextValue, indexN) { var validatedValue = _this.decoder.run(nextValue); return validatedValue.fold(function (errorsFromValue) { var errors = errorsFromValue.map(function (errorText) { return errorText + " at index #" + indexN; }); return validated.fold(function (errorsFromValidated) { return ValidationFn.failure(errorsFromValidated.concat(errors)); }, function () { return ValidationFn.failure(errors); }); }, function (successFromValue) { return validated.map(function (successes) { return successes.concat([successFromValue]); }); }); }, ValidationFn.success([])); } var message = "Value must be an array, found \"" + typeof valueArray + "\" instead"; return ValidationFn.failure(NonEmptyArray_1.nonEmptyArray.of(message)); }; return ArrayD; }(Decoder)); var BooleanD = /** @class */ (function (_super) { __extends(BooleanD, _super); function BooleanD() { return _super !== null && _super.apply(this, arguments) || this; } BooleanD.prototype.run = function (value) { if (typeof value === 'boolean') { return ValidationFn.success(value); } var message = "Value must be a boolean, found \"" + typeof value + "\" instead"; return ValidationFn.failure(NonEmptyArray_1.nonEmptyArray.of(message)); }; return BooleanD; }(Decoder)); var FailD = /** @class */ (function (_super) { __extends(FailD, _super); function FailD(message) { var _this = _super.call(this) || this; _this.message = message; return _this; } FailD.prototype.run = function () { return ValidationFn.failure(NonEmptyArray_1.nonEmptyArray.of(this.message)); }; return FailD; }(Decoder)); var FieldD = /** @class */ (function (_super) { __extends(FieldD, _super); function FieldD(fieldS, decoder) { var _this = _super.call(this) || this; _this.decoder = decoder; _this.field = fieldS; return _this; } FieldD.prototype.run = function (valueObject) { var _this = this; if (isObject(valueObject)) { if (this.field in valueObject) { return this.decoder .run(valueObject[this.field]) .mapFailure(function (errors) { return errors.map(function (error) { return error + " at key \"" + _this.field + "\""; }); }); } var noFieldMsg = "Object missing value at key \"" + this.field + "\""; return ValidationFn.failure(NonEmptyArray_1.nonEmptyArray.of(noFieldMsg)); } var notObjectMsg = "Value must be an object, found \"" + typeof valueObject + "\" instead"; return ValidationFn.failure(NonEmptyArray_1.nonEmptyArray.of(notObjectMsg)); }; return FieldD; }(Decoder)); var IndexD = /** @class */ (function (_super) { __extends(IndexD, _super); function IndexD(indexN, decoder) { var _this = _super.call(this) || this; _this.decoder = decoder; _this.indexN = indexN; return _this; } IndexD.prototype.run = function (valueArray) { var _this = this; if (isArray(valueArray)) { if (valueArray[this.indexN]) { return this.decoder .run(valueArray[this.indexN]) .mapFailure(function (errors) { return errors.map(function (error) { return error + " at index #" + _this.indexN; }); }); } var noIndexMsg = "Array missing value at index #" + this.indexN; return ValidationFn.failure(NonEmptyArray_1.nonEmptyArray.of(noIndexMsg)); } var notArrayMsg = "Value must be an array, found \"" + typeof valueArray + "\" instead"; return ValidationFn.failure(NonEmptyArray_1.nonEmptyArray.of(notArrayMsg)); }; return IndexD; }(Decoder)); var MapD = /** @class */ (function (_super) { __extends(MapD, _super); function MapD(fn, decoder) { var _this = _super.call(this) || this; _this.decoder = decoder; _this.fn = fn; return _this; } MapD.prototype.run = function (value) { return this.decoder.run(value).map(this.fn); }; return MapD; }(Decoder)); var Map2D = /** @class */ (function (_super) { __extends(Map2D, _super); function Map2D(fn, decoderA, decoderB) { var _this = _super.call(this) || this; _this.decoderA = decoderA; _this.decoderB = decoderB; _this.fn = fn; return _this; } Map2D.prototype.run = function (value) { var decodedA = this.decoderA.run(value); var decodedB = this.decoderB.run(value); var fn = this.fn; return decodedA.fold(function (errorsA) { return decodedB.fold(function (errorsB) { return ValidationFn.failure(errorsA.concat(errorsB)); }, function () { return ValidationFn.failure(errorsA); }); }, function (successA) { return decodedB.fold(function (errorsB) { return ValidationFn.failure(errorsB); }, function (successB) { return ValidationFn.success(fn(successA, successB)); }); }); }; return Map2D; }(Decoder)); var Map3D = /** @class */ (function (_super) { __extends(Map3D, _super); function Map3D(fn, decoderA, decoderB, decoderC) { var _this = _super.call(this) || this; _this.decoderA = decoderA; _this.decoderB = decoderB; _this.decoderC = decoderC; _this.fn = fn; return _this; } Map3D.prototype.run = function (value) { var decodedA = this.decoderA.run(value); var decodedB = this.decoderB.run(value); var decodedC = this.decoderC.run(value); var fn = this.fn; return decodedA.fold(function (errorsA) { return decodedB.fold(function (errorsB) { return decodedC.fold(function (errorsC) { return ValidationFn.failure(errorsA.concat(errorsB).concat(errorsC)); }, function () { return ValidationFn.failure(errorsA.concat(errorsB)); }); }, function () { return decodedC.fold(function (errorsC) { return ValidationFn.failure(errorsA.concat(errorsC)); }, function () { return ValidationFn.failure(errorsA); }); }); }, function (successA) { return decodedB.fold(function (errorsB) { return decodedC.fold(function (errorsC) { return ValidationFn.failure(errorsB.concat(errorsC)); }, function () { return ValidationFn.failure(errorsB); }); }, function (successB) { return decodedC.fold(function (errorsC) { return ValidationFn.failure(errorsC); }, function (successC) { return ValidationFn.success(fn(successA, successB, successC)); }); }); }); }; return Map3D; }(Decoder)); var NullD = /** @class */ (function (_super) { __extends(NullD, _super); function NullD(value) { var _this = _super.call(this) || this; _this.value = value; return _this; } NullD.prototype.run = function (value) { if (value === null) { return ValidationFn.success(this.value); } var message = "Value must be a null, found \"" + typeof value + "\" instead"; return ValidationFn.failure(NonEmptyArray_1.nonEmptyArray.of(message)); }; return NullD; }(Decoder)); var NumberD = /** @class */ (function (_super) { __extends(NumberD, _super); function NumberD() { return _super !== null && _super.apply(this, arguments) || this; } NumberD.prototype.run = function (value) { if (typeof value === 'number') { return ValidationFn.success(value); } var message = "Value must be a number, found \"" + typeof value + "\" instead"; return ValidationFn.failure(NonEmptyArray_1.nonEmptyArray.of(message)); }; return NumberD; }(Decoder)); var OneOfD = /** @class */ (function (_super) { __extends(OneOfD, _super); function OneOfD(decoders) { var _this = _super.call(this) || this; _this.decoders = decoders; return _this; } OneOfD.prototype.run = function (value) { var firstResult = this.decoders.head.run(value); if (firstResult.isSuccess()) { return firstResult; } return this.decoders.tail.reduce(function (decoded, decoder) { if (decoded.isSuccess()) { return decoded; } return decoder.run(value); }, firstResult); }; return OneOfD; }(Decoder)); var StringD = /** @class */ (function (_super) { __extends(StringD, _super); function StringD() { return _super !== null && _super.apply(this, arguments) || this; } StringD.prototype.run = function (value) { if (typeof value === 'string') { return ValidationFn.success(value); } var message = "Value must be a string, found \"" + typeof value + "\" instead"; return ValidationFn.failure(NonEmptyArray_1.nonEmptyArray.of(message)); }; return StringD; }(Decoder)); var SucceedD = /** @class */ (function (_super) { __extends(SucceedD, _super); function SucceedD(value) { var _this = _super.call(this) || this; _this.value = value; return _this; } SucceedD.prototype.run = function () { return ValidationFn.success(this.value); }; return SucceedD; }(Decoder)); var UnknownD = /** @class */ (function (_super) { __extends(UnknownD, _super); function UnknownD() { return _super !== null && _super.apply(this, arguments) || this; } UnknownD.prototype.run = function (value) { return ValidationFn.success(value); }; return UnknownD; }(Decoder)); function isArray(value) { return Array.isArray(value); } function isObject(value) { return typeof value === 'object'; } function pipe(value, fn) { return fn(value); } |