🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

nativemodels

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nativemodels

Native Models for Javascript

Source
npmnpm
Version
0.0.6
Version published
Weekly downloads
189
200%
Maintainers
1
Weekly downloads
 
Created
Source

NativeModels

npm pack age

Version Build Status

Maintainability Test Coverage Greenkeeper badge

Weekly Downloads Monthly Downloads Yearly Downloads

Issues Pull Requests

Dependencies Dev Dependencies

Native Models provides a way to map objects and arrays of objects in a clean and typed way. The main goal is to ensure runtime type checking and consistent models for APIs.

Getting Started

import { arrayModel, objectModel } from 'nativemodels';
import { boolean, computed, date, int, string } from 'nativemodels/datatypes';

const schema = {
	firstName: string().required(),
	lastName: string().required(),
	fullName: computed((record) => `${record.firstName} ${record.lastName}`),
	typeID: int().default(2),
	isAdmin: boolean().default(false),
	accountID: int().nullable(),
	created: date().default(new Date()),
	updated: date().default(new Date()),
};

const userModel = objectModel(schema);

const johnSmith = userModel({
	firstName: 'John',
	lastName: 'Smith',
});
// => { firstName: 'John', lastName: 'Smith', fullName: 'John Smith', ...}

const usersModel = arrayModel(schema);

const users = usersModel([
	{
		firstName: 'John',
		lastName: 'Smith',
	},
	{
		firstName: 'Jane',
		lastName: 'Doe',
	},
]);

const janeDoe = userModel({
	...johnSmith,
	firstName: 'Jane',
	lastName: 'Doe',
});
// => { firstName: 'Jane', lastName: 'Doe', fullName: 'Jane Doe', ...}

Datatype API

Datatype methods that can be called or extended.

datatypes.default(defaultValue)

Sets a default value if no value is set

datatypes.nullable()

Allows the value set to be null (useful for database models)

datatypes.parse(value)

Parses the value being set. Used to extend base datatype

datatypes.required()

Forces the value to be required. Is ignored if default value is set

Datatypes

  • boolean
  • computed
  • date
  • float
  • int
  • string

FAQs

Package last updated on 23 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts