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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sakartvelosoft/type-system

v0.2.5

Published

A generic library for entities construction and validation based on entity types and decorators

Downloads

8

Readme

type-system

This package provides:

  • set of the decorators for common aspects of data entities
  • services to build and validate the data entities

Applying these decorators does not provide the actual functionality for data persistence and other aspects. This package intended to be a part of a broader ecosystem.

Your package must be a typescript with at least following tsconfig.json options to work seamless:

 {  
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
} 

Example of decoration, taken from tests (app-user.ts):

import {dataType, defaultValue, entity, lowerCase, objectId, required, string, trim} from "../";

@entity()
@dataType('AppUser')
export class User {
    @objectId()
    @required()
    id: string;
    @string()
    @required()
    @lowerCase()
    email:string;
    @string()
    @required()
    @trim()
    name: string;
    @string()
    @required()
    @defaultValue('active')
    status:string;
}

Example of usage from tests:

import {createEmptyEntity, createEntity, forType, preprocessEntity, validateEntity} from "../";
import {expect} from "chai";
import {User} from "./app-user";
describe('Testing metadata for types', () => {
    it('registers a type for metadata', () => {

        let userType = forType(User);


        console.info({ dataType: userType.dataType, isEntity: userType.isEntity});
        expect(userType.isEntity).equal(true, 'User type must be marked as entity');
        for(let field of userType.fields.values()) {
            console.info(`field ${field.name}: ${field.type} is ${field.required ?"" : "not"} required`)
            expect(field.required).equal(true, "fields in test type must be marked as required");
        }
        expect(userType.dataType).equal("AppUser");

        let userObj = createEmptyEntity(User);
        userObj.email = "[email protected]";
        userObj.name = "Some value to trim "
        let cleanUser = preprocessEntity(userObj);
        console.info(JSON.stringify(cleanUser));
        expect(cleanUser.status).equal(forType(User).fields.get('status').defaultValue);
        expect(cleanUser.email).equal(userObj.email.toLowerCase(), 'User email must be lower-cased');
        expect(cleanUser.name).equal(userObj.name.trim(), 'User name must be trimmed');
        
        
        let user2:User = createEntity(User, {
            name:"Long user name 2 ",
            email: "[email protected]"
        }, true);
        console.info(user2);
        expect(user2.name).equal("Long user name 2 ".trim(), 'User 2 name must be trimmed');
        expect(user2.email).equal("[email protected]".trim().toLowerCase(), 'User 2 name must be trimmed and lower-cased');
        expect(user2.id).a('string', 'ID must be a string').length(20, 'Expected generate id length is 20');
        

        validateEntity(User, user2);


        return true;
    })
});

Example of hidden values declaration:

import {dataType, defaultValue, entity, lowerCase, objectId, required, string, trim, hiddenFromClient} from "../index";

@entity()
@dataType('AppUser')
export class User {
    @objectId()
    @required()
    id: string;
    @string()
    @required()
    @lowerCase()
    email:string;
    @string()
    @required()
    @trim()
    name: string;
    @string()
    @required()
    @defaultValue('active')
    status:string;
    @hiddenFromClient()
    passwordHash:string;
    @hiddenFromClient()
    passwordSalt:string;
}

Example of hidden values exclusion from tests

import {createEntity, excludeHiddenProperties} from "../";
import {User} from "./app-user";
import {expect} from 'chai'; 

describe('Check hidden fields', () => {
    it('Verify hidden fields excluded from client data', () => {
        let user = createEntity(User, {
            email:"[email protected]",
            name:"Some name to be tested ",
            passwordHash:"CRACK ME!",
            passwordSalt: "VERY SALTY!"
        }, true);
        let clientUser = excludeHiddenProperties(user);
        console.info(clientUser);
        expect(clientUser).not.haveOwnProperty('passwordHash');
        expect(clientUser).not.haveOwnProperty('passwordSalt');
    });
});