🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
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 - npm Package Compare versions

Comparing version
2.4.1
to
2.4.2
+1
-1
createModel.js

@@ -30,3 +30,3 @@ const defaultOptions = require('./lib/defaultOptions');

const proxyTarget = parseRecord(schema, builtRecord);
const proxyTarget = parseRecord(schema, builtRecord, modelOptions.allowNulls);

@@ -33,0 +33,0 @@ return new Proxy(proxyTarget, proxyHandler(schema, options, context));

const createType = require('./../createType');
const { isString } = require('./../lib/checks/types');
const strictCheck = (key, value, options) => {
if (isString(value)) {
if (options.length && value.length > options.length) {
throw new Error(`NativeModels - Property ${key} is longer than ${options.length}`);
}
return true;
}
throw new Error(`NativeModels - Property ${key} is not a string`);
};
const validCheck = (key, value) => {
if (value === null) {
throw new Error(`NativeModels - Property ${key} is not a string`);
}
return true;
};
const string = (options = {}) =>

@@ -14,21 +33,6 @@ createType({

},
strictCheck: (key, value) => {
if (isString(value)) {
if (options.length && value.length > options.length) {
throw new Error(`NativeModels - Property ${key} is longer than ${options.length}`);
}
return true;
}
throw new Error(`NativeModels - Property ${key} is not a string`);
},
validCheck: (key, value) => {
if (value === null) {
throw new Error(`NativeModels - Property ${key} is not a string`);
}
return true;
},
strictCheck: (key, value) => strictCheck(key, value, options),
validCheck,
});
module.exports = string;
const defaultOptions = {
// Allow nulls on all columns
allowNulls: false,
// Ignores case when initializing object from model

@@ -3,0 +5,0 @@ caseSensitive: true,

const parseValue = require('./parseValue');
const parseRecord = (schema, record) =>
const parseRecord = (schema, record, allowNulls) =>
Object.keys(record).reduce(
(result, key) => ({
...result,
...(schema[key] ? { [key]: parseValue(schema[key], key, record[key]) } : {}),
...(schema[key] ? { [key]: parseValue(schema[key], key, record[key], allowNulls) } : {}),
}),

@@ -9,0 +9,0 @@ {},

@@ -0,1 +1,2 @@

/* eslint-disable max-params */
const invalidTypeCheck = require('./checks/invalidType');

@@ -18,4 +19,4 @@ const { isNull } = require('./checks/types');

const parseValue = (type, key, value) => {
if (type.allowNull && isNull(value)) {
const parseValue = (type, key, value, allowNulls) => {
if ((type.allowNull || allowNulls) && isNull(value)) {
return null;

@@ -22,0 +23,0 @@ } else if (invalidTypeCheck(type, key, value)) {

@@ -58,3 +58,3 @@ {

},
"version": "2.4.1"
"version": "2.4.2"
}

@@ -39,3 +39,3 @@ <h1 align="center">

[![Build Status](https://flat.badgen.net/circleci/Prefinem/nativemodels)](https://circleci.com/gh/Prefinem/nativemodels)
[![Build Status](https://flat.badgen.net/circleci/github/Prefinem/nativemodels)](https://circleci.com/gh/Prefinem/nativemodels)

@@ -51,2 +51,3 @@ <!-- CodeCov -->

[![Coverage](https://flat.badgen.net/codeclimate/coverage/Prefinem/nativemodels)](https://codeclimate.com/github/Prefinem/nativemodels)
[![Lines of Code](https://flat.badgen.net/codeclimate/loc/Prefinem/nativemodels)](https://codeclimate.com/github/Prefinem/nativemodels)

@@ -405,2 +406,3 @@ Native Models provides a way to map objects in a clean and typed way. The main goal is to ensure runtime type checking and consistent models for APIs.

const defaultOptions = {
allowNulls: false, // Allow nulls on all columns
caseSensitive: true, // Ignores case when initializing object from model

@@ -412,2 +414,23 @@ strict: false, // Throws an error if key is not in schema

### allowNulls
The allowNulls option `default: false` will allow you to accept null for any field in your schema document. Useful for importing data from database records
```js
const { createModel } = require('nativemodels');
const { string } = require('nativemodels/datatypes');
const options = {
allowNulls: true,
};
const schema = {
foo: string(),
};
const model = createModel(schema, options);
const data = model({ foo: null });
// => { foo: null }
```
### caseSensitive

@@ -414,0 +437,0 @@