Runtime object parsing and validation with static TypeScript typing.

Overview

TypeParse

Runtime object transformation, parsing and validation with inferred static TypeScript typing.


Install

Using npm

npm install typeparse

Using yarn

yarn add typeparse

Example

import { TypeParse, Types as T } from "typeparse";

const input = JSON.parse(
  `{
    "id": "12345",
    "userInfo": {
      "name": "John Doe",
      "phone": "+1 234 567 890"
    }
  }`
);

// Parsing configuration
const tp = new TypeParse(
  T.Object({
    id: T.Number("id"),
    name: T.String("userInfo.name"),
    phoneNumber: T.String("userInfo.phone"),
    address: T.String("userInfo.address", { defaultValue: "no-address" }),
    email: T.String("userInfo.email").optional(),
  })
);

const user = tp.parse(input); // User is parsed with inferred type

console.log(user);
// {
//   id: 12345,
//   name: 'John Doe',
//   phoneNumber: '+1 234 567 890',
//   address: 'no-address',
//   email: undefined
// }

Object transformation

In case we need to not only parse an object but also to trasform it (i.e.):

{
  "user": {
    "name": "John",
    "lastName": "Doe",
  },
  "email": "[email protected]",
  "phoneNumbers": ["123-456-7890", "321-654-0987"]
}

To

{
  "name": "John",
  "lastName": "Doe",
  "contactInfo": {
    "email": "[email protected]",
    "phone": "123-456-7890"
  }
}

We can use the path parameter in order to create a new object, specifying the path from the original objects root to define each value.

import { TypeParse, Types as T } from "typeparse";

const input = {
  user: {
    name: "John",
    lastName: "Doe",
  },
  email: "4522 Sigley Road",
  phoneNumbers: ["123-456-7890", "321-654-0987"],
};

const tp = new TypeParse(
  T.Object({
    name: T.String("user.name"),
    lastName: T.String("user.lastName"),
    contactInfo: T.Object({
      email: T.String("email"),
      phone: T.String("phoneNumbers.[0]"),
    }),
  })
);

console.log(tp.parse(input));
// {
//   name: 'John',
//   lastName: 'Doe',
//   contactInfo: {
//     email: '4522 Sigley Road',
//     phone: '123-456-7890'
//   }
// }
You might also like...

Simple, lightweight at-runtime type checking functions, with full TypeScript support

pheno Simple, lightweight at-runtime type checking functions, with full TypeScript support Features Full TypeScript integration: TypeScript understand

Sep 5, 2022

TypeScript type definitions for Bun's JavaScript runtime APIs

Bun TypeScript type definitions These are the type definitions for Bun's JavaScript runtime APIs. Installation Install the bun-types npm package: # ya

Dec 16, 2022

Framework agnostic CLI tool for routes parsing and generation of a type-safe helper for safe route usage. πŸ—ΊοΈ Remix driver included. 🀟

Framework agnostic CLI tool for routes parsing and generation of a type-safe helper for safe route usage. πŸ—ΊοΈ Remix driver included. 🀟

About routes-gen is a framework agnostic CLI tool for routes parsing and generation of a type-safe helper for safe route usage. Think of it as Prisma,

Jan 2, 2023

Command-line toolkit for parsing, compiling, transpiling, optimizing, linking, dataizing, and running EOLANG programs

First, you install npm and Java SE. Then, you install eolang package: $ npm install eolang Then, you write a simple EO program in hello.eo file in th

Nov 17, 2022

Utilities for parsing and manipulating LaTeX ASTs with the Unified.js framework

unified-latex Monorepo for @unified-latex packages. These packages provide a JS/TypeScript interface for creating, manipulating, and printing LaTeX Ab

Dec 27, 2022

Enhanced interval features for Node.js, such as promisified interval and human readable time parsing.

Interval-next Interval-next is a package that extends Javascript's built-in setInterval() capabilities. You have a plain and promisified interval meth

Jul 28, 2022

A TOML parsing tool written in Rust for Node.js

@daydog/toml A TOML parsing tool written in Rust for Node.js Installation @daydog/toml is available via npm. npm install @daydog/toml Usage parse You

Jul 20, 2022

JavaScript library for parsing Dirtywave M8 files, complete with a CLI for interacting with M8 files.

m8-js This repository contains a JavaScript library for parsing Dirtywave M8 files, as well as a CLI for interacting with M8 files. The hopes are not

Dec 17, 2022

A parsing library for CircleCI configuration files, powered by the CircleCI Config SDK

CircleCI Config Parser A parsing library for CircleCI configuration files, powered by the CircleCI Config SDK Used by the CircleCI Visual Config Edito

Dec 4, 2022
Releases(1.0.1)
  • 1.0.1(Apr 28, 2022)

    What's Changed

    • Fix README by @Kenneth-hv in https://github.com/Kenneth-hv/typeparse/pull/13
    • V1.0.1 by @Kenneth-hv in https://github.com/Kenneth-hv/typeparse/pull/14

    Full Changelog: https://github.com/Kenneth-hv/typeparse/compare/1.0.0...1.0.1

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Apr 28, 2022)

    What's Changed

    • V1.0.0 by @Kenneth-hv in https://github.com/Kenneth-hv/typeparse/pull/11
    • v1.0.0 by @Kenneth-hv in https://github.com/Kenneth-hv/typeparse/pull/12

    Full Changelog: https://github.com/Kenneth-hv/typeparse/compare/0.3.0...1.0.0

    Source code(tar.gz)
    Source code(zip)
  • 0.3.0(Apr 5, 2022)

    What's Changed

    • V0.3.0 - Unions by @Kenneth-hv in https://github.com/Kenneth-hv/typeparse/pull/9
    • V0.3.0 by @Kenneth-hv in https://github.com/Kenneth-hv/typeparse/pull/10

    Full Changelog: https://github.com/Kenneth-hv/typeparse/compare/0.2.0...0.3.0

    Source code(tar.gz)
    Source code(zip)
  • 0.2.0(Apr 5, 2022)

    • Added support for type Any.

    What's Changed

    • 0.2.0 by @Kenneth-hv in https://github.com/Kenneth-hv/typeparse/pull/7
    • 0.2.0 by @Kenneth-hv in https://github.com/Kenneth-hv/typeparse/pull/8

    Full Changelog: https://github.com/Kenneth-hv/typeparse/compare/0.1.0...0.2.0

    Source code(tar.gz)
    Source code(zip)
Owner
Kenneth Herrera
Kenneth Herrera
An open-source Typing-effect Library, That enables potential users to add a typing effect to mere DOM Elements.

Typing Effect Library An open-source Typing-effect Library I created. That enables potential users to add a typing effect to mere DOM Elements. Tool P

Okoye Charles 14 Oct 3, 2022
A minimal typing practice website with no features except typing. Inspired from Monkeytype. Do ⭐ this repository.

SenpaiType Introduction SenpaiType is a minimal typing practice website with no features except typing. Inspired from Monkeytype. Contributing Raise i

Pruthviraj Jadhav 5 Nov 30, 2022
This is a Google Apps Script library for parsing the form object from HTML form and appending the submitted values to the Spreadsheet.

HtmlFormApp Overview This is a Google Apps Script library for parsing the form object from HTML form and appending the submitted values to the Spreads

Kanshi TANAIKE 18 Oct 23, 2022
Fix for Object.hasOwnProperty, which normally just returns a boolean, which is not good when you care about strong typing.

Welcome to ts-has-own-property ?? Fix for Object.hasOwnProperty, which normally just returns a boolean, which is not good when you care about strong t

Funtal Foundation 1 Jul 4, 2022
Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Welcome to ts-object-keys ?? Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Funtal Foundation 1 Jul 4, 2022
RenderIf is a function that receives a validation as a parameter, and if that validation is true, the content passed as children will be displayed. Try it!

RenderIf RenderIf is a function that receives a validation as a parameter, and if that validation is true, the content passed as children will be disp

Oscar Cornejo Aguila 6 Jul 12, 2022
✏️ A small jQuery extension to turn a static HTML table into an editable one. For quickly populating a small table with JSON data, letting the user modify it with validation, and then getting JSON data back out.

jquery-editable-table A small jQuery extension to turn an HTML table editable for fast data entry and validation Demo ?? https://jsfiddle.net/torrobin

Tor 7 Jul 31, 2022
Type Safe Object Notation & Validation

tson Type Safe Object Notation & Validation ?? Work in Progress, not ready for production... Features ?? Functional ?? Immutable βœ… Well tested Why? Af

null 9 Aug 10, 2022
Gofiber with NextJS Static HTML is a small Go program to showcase for bundling a static HTML export of a Next.js app

Gofiber and NextJS Static HTML Gofiber with NextJS Static HTML is a small Go program to showcase for bundling a static HTML export of a Next.js app. R

Mai 1 Jan 22, 2022
Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator Types generator is a utility tool that will help User to create TS Interfaces from JSON. All you have to do is paste your single objec

Vineeth.TR 16 Dec 6, 2022