UNPKG

io-ts-types

Version:

A collection of codecs and combinators for use with io-ts

50 lines (49 loc) 1.73 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonFromString = exports.Json = exports.JsonRecord = exports.JsonArray = void 0; /** * @since 0.5.14 */ var t = __importStar(require("io-ts")); /** * @since 0.5.15 */ exports.JsonArray = t.recursion('JsonArray', function () { return t.readonlyArray(exports.Json); }); /** * @since 0.5.15 */ exports.JsonRecord = t.recursion('JsonRecord', function () { return t.record(t.string, exports.Json); }); /** * @since 0.5.15 */ exports.Json = t.union([t.boolean, t.number, t.string, t.null, exports.JsonArray, exports.JsonRecord], 'Json'); /** * @since 0.5.14 */ exports.JsonFromString = new t.Type('JsonFromString', exports.Json.is, function (s, c) { try { return t.success(JSON.parse(s)); } catch (e) { return t.failure(s, c); } }, function (json) { return JSON.stringify(json); });