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

data-fixer

v5.0.1

Published

a tool to check and fix data with schema, inspired by angular reactive form.

Downloads

6

Readme

Data Fixer

A tool for check and fix data with json schema, inspired by angular reactive form.

Installation

npm:

$ npm install data-fixer

yarn:

$ yarn add data-fixer

Type Definition

This project develop with TypeScript, type is build-in with publish.

Usage

There are three "Controllers" which vctrl for primivite value, actrl for array, dctrl for Dictionary, rctrl for Record, and tctrl for tuple.

Simply provide a json schema and fallback value/function to vctrl, compose one or many vctrl in other *ctrl for structural data.

vctrl demo

import Ajv from 'ajv';
import * as yup from 'yup';
import { ajvAdt, vctrl } from 'data-fixer';

// for Ajv
const isGt0 = vctrl(ajvAdt(new Ajv().compile({ type: 'number', exclusiveMinimum: 0 })), 1);
// since Ajv ValidateFunction already fit the signature of Vtor, if dev in JS, below is OK too.
// but not OK for TS, due Data Fixer only handle for sync validation.
const isGt0 = vctrl(new Ajv().compile({ type: 'number', exclusiveMinimum: 0 }), 1);

// for yup
const isGt0 = vctrl(yupAdt(yup.number().moreThan(0)), 1);

const holder1 = isGt0(0);
holder1.valid; // false
holder1.invalid; // true
holder1.value(); // 1

const holder2 = isGt0(7);
holder2.valid; // true
holder2.invalid; // false
holder2.value(); // 7

// type coercion case
const isGt0 = vctrl(ajvAdt({ type: 'number', exclusiveMinimum: 0 }, { coerceTypes: true }), 1);

const holder1 = isGt0('7');
holder1.valid; // true
holder1.invalid; // false
holder1.value(); // '7', ajv only coerce object property

// fallback function
const fallback = (data: any) => Number.isNaN(+data) ? +data : 1;
const isNumber = vctrl(ajvAdt({ type: 'number' }), fallback);

const holder = isNumber('foo');
holder.valid; // false
holder.invalid; // true
holder.value(); // 1

actrl demo

import { actrl, vctrl, ajvAdt } from 'data-fixer';

const isNumber = vctrl(ajvAdt({ type: 'number' }), 1);
const isAllNumber = actrl(isNumber);

const holder = isAllNumber([1, 2, 3, '4']);
holder.valid; // false
holder.invalid; // true
holder.value(); // [1, 2, 3, 1];

rctrl demo

import { rctrl, vctrl, ajvAdt } from 'data-fixer';

const isNumber = vctrl(ajvAdt({ type: 'number' }), 1);
const isAllNumber = rctrl(isNumber);

// rctrl can handle for object that care only for its value type
const holder = isAllNumber({ a: 1, b: 2, c: 3, d: '4' });
holder.valid; // false
holder.invalid; // true
holder.value(); // { a: 1, b: 2, c: 3, d: 1 }

dctrl demo

import { dctrl, vctrl, ajvAdt } from 'data-fixer';

const isConfig = dctrl({
  version: vctrl(ajvAdt({ type: 'number', const: 2 }), 2),
  menus: dctrl({
    enabled: vctrl({ type: 'boolean' }, false),
    clipboard: vctrl({ type: 'boolean' }, true),
  }),
});

const holder = isConfig({});
holder.valid; // false
holder.invalid; // true
holder.value(); // { version: 2, menus: { enabled: false, clipboard: true } }

tctrl demo

import { tctrl, vctrl, ajvAdt } from 'data-fixer';

const isNumber = vctrl(ajvAdt({ type: 'number' }), 1);
const isString = vctrl(ajvAdt({ type: 'string' }), 'a');
const isNumberString = tctrl(isNumber, isString);

// tctrl can check tuple type array
const holder = isNumberString([1, 1]);
holder.valid; // false
holder.invalid; // true
holder.value(); // [1, 'a']

Infer type in TypeScript

Most of the time the return value of Holder.value() have the correct type, due it infer the type from the alt type.

import Ajv from 'ajv';
import { ajvAdt } from '../adapter';
import { vctrl } from '../vctrl/vctrl';
import { dctrl } from './dctrl';

const ctrl = dctrl({
  version: vctrl(ajvAdt(new Ajv().compile({ type: 'number', const: 2 })), 2),
  menus: dctrl({
    enabled: vctrl(ajvAdt(new Ajv().compile({ type: 'boolean' })), false),
    clipboard: vctrl(ajvAdt(new Ajv().compile({ type: 'boolean' })), true),
  }),
});

const holder = ctrl({ version: 2, menus: { enabled: true, clipboard: true } });

// typeof holder.value()
{
  version: number;
  menus: {
    enabled: boolean;
    clipboard: boolean;
  };
}

// The infer result is good, but the version is actually a constant 2 rather than just number.
// There are two ways you can fix this.

// either
vctrl<2>(ajvAdt(new Ajv().compile({ type: 'number', const: 2 })), 2);
// or
vctrl(ajvAdt(new Ajv().compile({ type: 'number', const: 2 })), 2 as 2);

Caveats

  • Data Fixer is currently only handle for sync validation.

More

Data Fixer let developer choose schema validator by using adapter, currently support Ajv and yup. For how to define the schema, please check the below informations: