npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

class-from-any

v0.0.18

Published

Allows use of decorator to get, convert and validate class from raw object data

Readme

class-from-any

Allows use of decorator to get, convert and validate class from raw object data.

Installation

npm install class-from-any --save

Enable esModuleInterop, experimentalDecorators and useDefineForClassFields and disable strictPropertyInitialization in the tsconfig.json.


"compilerOptions": {
    ...
    "esModuleInterop": true,
    "strictPropertyInitialization": false,
    "experimentalDecorators": true,
    "useDefineForClassFields": true,
}

Hello world


import { FromAny, GetFrom, Validate, isString, notEmpty } from "class-from-any";

const worldJSONData = `{"name": "Earth"}`;

class World extends FromAny {
    @GetFrom("name") @Validate(isString, notEmpty) title: string;
}

const world = new World().from(
    JSON.parse(worldJSONData) as Record<string, unknown>
);

Full example

For example, you have this JSON object data:


const worldJSONData = `{
    "name": "Earth",
    "date": 1668271586239,
    "description": "Earth is the third planet from the Sun.",
    "alternativeNames": ["Gaia", "Terra", "Tellus"],        
    "chemicalComposition": {
        "iron": "32%",
        "oxygen": "30%",
        "silicon": "15%",
        "magnesium": "13.9%"
    },
    "star": {
        "name": "The Sun"
    },        
    "satellites": [{
        "name": "Moon"
    }]
}`;

const worldData = JSON.parse(worldJSONData) as Record<string, unknown>;

We want to get and validate classes or interfaces from JSON:


class World {
    title: string; // not name
    description: string;
    date: Date;
    otherNames: string[]; // not alternativeNames
    chemicalComposition: ChemicalComposition;
    satellites: Satellite[];
    star: string; // star.name property in JSON
}

class ChemicalComposition {
    iron: number;
    oxygen: number;
    silicon: number;
    magnesium: number;
}

class Satellite {
    name: string;
}

Note that the class structure does not exactly replicate the JSON available to us. Declare implements from our clear classes or interfaces:


import {
    FromAny,
    GetFrom,
    Validate,
    ChildArray,
    Convert,
    ChildObject,
    IsEqual,
    isString,
    notEmpty,
    isNumber,
    notEmptyArray,
    isObject,
    toInt,
    toFloat,
    toDate
} from "../src";

class ChemicalCompositionFromJSON extends FromAny implements ChemicalComposition {
    @Convert(toInt) @Validate(isNumber, notEmpty) iron: number;
    @Convert(toInt) @Validate(isNumber, notEmpty) oxygen: number;
    @Convert(toInt) @Validate(isNumber, notEmpty) silicon: number;
    @Convert(toFloat) @Validate(isNumber, notEmpty) magnesium: number;
}

class SatelliteFromJSON extends FromAny implements Satellite {
    @Validate(isString, notEmpty) name: string;
}

class WorldFromJSON extends FromAny implements World {
    @GetFrom("name") @Validate(isString, notEmpty) @IsEqual("Earth") title: string;
    @Convert(toDate) @Validate(notEmpty) date: Date;

    @Validate(isString, notEmpty) description: string;

    @GetFrom("alternativeNames")
    @Validate(notEmptyArray)
    otherNames: string[]; // not alternativeNames

    @ChildObject(ChemicalCompositionFromJSON)
    @Validate(isObject)
    chemicalComposition: ChemicalComposition;

    @ChildArray(SatelliteFromJSON) satellites: Satellite[];
    @GetFrom("star.name") @Validate(isString, notEmpty) star: string;
}

Get, convert and validate class from JSON data


const world = new WorldFromJSON().from(worldData);

Under construction

Modules "validate" and "convert" are under construction. We welcome contributors!