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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@incrowd/model-validator

v0.2.0

Published

schema validated models for use in incrowd projects

Readme

Incrowd Model Validator

A tool for validating objects against a schema

Motivation

To provide a highly portable validator that can be applied to validate data in frontend projects.

getting started

npm i @incrowd/model-validator

usage

in es6

import ModelValidator from '@incrowd/model-validator'

ModelValidator.setConfig({defaultRequire: false}) /*optional*/

const myModel = new ModelValidator(schema, data)

in vue

in main.js

import ModelValidator from '@incrowd/model-validator'

Vue.use(ModelValidator)

in vue module


<template>
	<div>
		{{name}}
	</div>
</template>

<script>
import ModelValidator from '@incrowd/model-validator'

const schema = {
	name: 'string'
}
const data = {
	name: 'this is a string'
}

export default {
	name: 'component',
	data: () => new ModelValidator(schema, data)
}

</script>

Creating a schema

Schemas can be given as either a string containing just the schema type (setting all other values to their default) or as an object in the following structure:

schema = {
	fieldName: {
		type:     'number',
		required: true, /*optional*/
		arrayOf:  true, /*optional*/
	}
}

Schema types:

| Type | Extra Fields | Description | Example Schema | Example Data | |---------|--------------|---------------------------------------------------|--------------------------------------------------------------------------|---------------------------------------------| |Any | None | Allows any data to be entered. | "any" | 23 | |string | None | Accepts string literals. | "string" | "Example" | |number | None | Accepts number literals. | "number" | 12 | |boolean| None | Accepts boolean values. | "boolean" | true | |date | None | Accepts date strings and Date objects. | "date" | "2021-02-16T15:15:19+00:00" | |color | None | Accepts hex color strings | "color" | "#FFFFFF" | |enum | enum | Accepts values in given enum string. | {type: "enum", enum: [12, 'value', 'something else]} | "value" | |object | schema | Accepts an object that adheres to a child schema. | {type: "object", schema: {childField: "any", booleanChild: "boolean"}} | {childField: 42, booleanChild: false} | |url | None | Accepts a valid url. | "url" | "http://www.example.com/path/to/resource" |

Generic Fields:

these can be applied to any of the above types.

| Field | Description | Example value | |------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------| | required | Makes the field required. the model validator will throw an error if field is not present. | 'true' | | arrayOf | The field expects an array of values that conform to the schema. If required, length must be at least 1 | 'true' | | default | Default value for the field to be used if no value is given. Value does not have to match required type. for example, a string field could have a default of false | "default value" | | alias | An alternate path to find the given data. allows dot notation to be used to fetch data from deeper in the given structure. | "childobject.anotherfield.value" |

Example Schema with valid data

schema = {
	label:       {type: 'string', required: true},
	publishDate: 'date',
	listOfLinks: {
		type:    'url',
		arrayOf: true,
	},
	theme:       {
		type:   'object',
		schema: {
			color1: 'color',
			color2: 'color'
		}
	},
	people:      {
		type:    'object',
		arrayOf: true,
		schema:  {
			name:           {type: 'string', required: true},
			age:            'number',
			favouriteColor: {
				type: 'enum',
				enum: ["Red", "Green", "Blue", "Purple", "Orange", "Pink", "Turquoise"]
			}
		}
	}
}

data = {
	label:       'valid data!',
	publishDate: "Tuesday, 16-Feb-21 16:07:56 UTC",
	listOfLinks: [
		'http://www.example.com',
		"https://www.google.com",
		"http://www.thisisalink.co.uk/path/to/page",
	],
	theme:       {
		color1: '#F00',
		color2: '#3c5afe',
	},
	people:      [
		{
			name:           'John Doe',
			age:            42,
			favouriteColor: "Green"
		},
		{
			name:           'Jane Doe',
			age:            36,
			favouriteColor: "Orange"
		}
	]
}

validatedData = new ModelValidator(schema, data)

Accessing and Updating Data

Data Retrieval

There are two primary ways of retrieving data. by exporting the fields you want (recommended), or by directly accessing the model.

Exporting a model's data

Assuming the schema and data are the same as in the previous example:

let data = new ModelValidator(schema, originalData);

filteredData = data.toObject({
	label:  true,
	people: {
		name: true
	}
})

this will return filtered data to include only the label and each person's name:

filteredData == {
	label:  'valid data!',
	people: [
		{name: 'John Doe'},
		{name: 'Jane Doe'},
	]
}

If you wish to simplify the data returned for a child object, you can use the flatten option in toObject:

let data = new ModelValidator(schema, originalData);

filteredData = data.toObject({
	label:  true,
	people: {
		name: 'flatten'
	}
})

Please note, however, that doing this will prevent the exported data from being re-imported, as it no longer conforms to the expected structure.

filteredData == {
	label:  'valid data!',
	people: [
		'John Doe',
		'Jane Doe',
	]
}

Re-importing exported data

ModelValidator.prototype.update allows updating of an entire structure at once. this means that you can do the following to mutate data:

let data = new ModelValidator(schema, originalData);

filteredData = data.toObject({
	label: true,
	theme: {
		color1: true
	}
})

filteredData.label = 'this is a new label';
filteredData.theme.color1 = '#fe5a3c'

data.update = filteredData;

you can then expect the modelValidator to hold the following:

data == {
	label:       'this is a new label',
	publishDate: "Tuesday, 16-Feb-21 16:07:56 UTC",
	listOfLinks: [
		'http://www.example.com',
		"https://www.google.com",
		"http://www.thisisalink.co.uk/path/to/page",
	],
	theme:       {
		color1: '#fe5a3c',
		color2: '#3c5afe',
	},
	people:      [
		{
			name:           'John Doe',
			age:            42,
			favouriteColor: "Green"
		},
		{
			name:           'Jane Doe',
			age:            36,
			favouriteColor: "Orange"
		}
	]
}