nativemodels
Advanced tools
@@ -24,4 +24,11 @@ const createType = require('./../createType'); | ||
| }, | ||
| validCheck: (key, value) => { | ||
| if (value === null) { | ||
| throw new Error(`NativeModels - Property ${key} is not a string`); | ||
| } | ||
| return true; | ||
| }, | ||
| }); | ||
| module.exports = string; |
| const defaultRecord = (schema, record) => ({ | ||
| ...Object.keys(schema) | ||
| .filter((key) => schema[key].hasDefault) | ||
| .reduce( | ||
| (result, key) => ({ | ||
| ...result, | ||
| ...{ [key]: schema[key].defaultValue }, | ||
| }), | ||
| {}, | ||
| ), | ||
| ...record, | ||
| ...Object.keys(schema).reduce( | ||
| (result, key) => ({ | ||
| ...result, | ||
| ...(schema[key].hasDefault ? { [key]: record[key] || schema[key].defaultValue } : {}), | ||
| }), | ||
| {}, | ||
| ), | ||
| }); | ||
| module.exports = defaultRecord; |
+12
-6
@@ -13,2 +13,3 @@ { | ||
| "eslint-plugin-jest": "^22.1.3", | ||
| "eslint-plugin-react": "^7.12.4", | ||
| "husky": "^1.2.0", | ||
@@ -32,7 +33,12 @@ "jest": "^23.6.0", | ||
| "lint-staged": { | ||
| "*.js": [ | ||
| "eslint --fix", | ||
| "prettier --config ./.prettierrc.json --write", | ||
| "jest --bail --findRelatedTests", | ||
| "git add" | ||
| "linters": { | ||
| "*.js": [ | ||
| "eslint --fix", | ||
| "prettier --config ./.prettierrc.json --write", | ||
| "jest --bail --findRelatedTests", | ||
| "git add" | ||
| ] | ||
| }, | ||
| "ignore": [ | ||
| "site/**" | ||
| ] | ||
@@ -54,3 +60,3 @@ }, | ||
| }, | ||
| "version": "2.4.0" | ||
| "version": "2.4.1" | ||
| } |
+22
-0
@@ -37,2 +37,6 @@ <h1 align="center"> | ||
| <!-- CircleCI --> | ||
| [](https://circleci.com/gh/Prefinem/nativemodels) | ||
| <!-- CodeCov --> | ||
@@ -155,2 +159,20 @@ | ||
| ### datatypes.from(fromKeys, options) | ||
| When passing in an object that you would like a key renamed, you can set the correct name in the schema and set the from to key name on the original object. This is useful for renaming and recasing. | ||
| ```js | ||
| const { createModel } = require('nativemodels'); | ||
| const { string } = require('nativiemodels/datatypes'); | ||
| const schema = { | ||
| name: string().from('firstName'), | ||
| }; | ||
| const model = createModel(schema); | ||
| const user = model({ firstName: 'john' }); | ||
| console.log(user.name); | ||
| // => 'john' | ||
| ``` | ||
| ## Datatypes | ||
@@ -157,0 +179,0 @@ |
38292
2.43%621
0.65%602
3.79%11
10%