nativemodels
Advanced tools
+1
-1
@@ -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)); |
+21
-17
| 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)) { |
+1
-1
@@ -58,3 +58,3 @@ { | ||
| }, | ||
| "version": "2.4.1" | ||
| "version": "2.4.2" | ||
| } |
+24
-1
@@ -39,3 +39,3 @@ <h1 align="center"> | ||
| [](https://circleci.com/gh/Prefinem/nativemodels) | ||
| [](https://circleci.com/gh/Prefinem/nativemodels) | ||
@@ -51,2 +51,3 @@ <!-- CodeCov --> | ||
| [](https://codeclimate.com/github/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 @@ |
39184
2.33%626
0.81%625
3.82%