nativemodels
Advanced tools
@@ -16,3 +16,3 @@ 'use strict'; | ||
| const date = () => _extends({}, _base2.default, { | ||
| parse(value) { | ||
| parse(value, name) { | ||
| if (value instanceof Date) { | ||
@@ -22,3 +22,3 @@ return value; | ||
| throw new Error('Not a date'); | ||
| throw new Error(`Property ${name} is not a date`); | ||
| } | ||
@@ -25,0 +25,0 @@ }); |
@@ -16,5 +16,5 @@ 'use strict'; | ||
| const number = () => _extends({}, _base2.default, { | ||
| parse(value) { | ||
| parse(value, name) { | ||
| if (value === true || value === false || value === '') { | ||
| throw new Error('Not a number'); | ||
| throw new Error(`Property ${name} is not a number`); | ||
| } | ||
@@ -26,3 +26,3 @@ | ||
| throw new Error('Not a number'); | ||
| throw new Error(`Property ${name} is not a number`); | ||
| } | ||
@@ -29,0 +29,0 @@ }); |
+27
-39
@@ -6,54 +6,42 @@ "use strict"; | ||
| }); | ||
| var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
| /* eslint consistent-return:off */ | ||
| const objectModel = exports.objectModel = schema => (record = {}) => { | ||
| const recordKeys = Object.keys(record); | ||
| const schemaKeys = Object.keys(schema); | ||
| const parsedRecord = {}; | ||
| const requiredCheck = (schema, record) => Object.keys(schema).forEach(key => { | ||
| if (schema[key].isRequired && !record[key]) { | ||
| throw new Error(`Property: '${key}' is required`); | ||
| } | ||
| }); | ||
| schemaKeys.forEach(key => { | ||
| if (schema[key].isRequired && !record[key]) { | ||
| throw new Error(`Property: '${key}' is required`); | ||
| } | ||
| if (schema[key].hasDefault && !record[key]) { | ||
| parsedRecord[key] = schema[key].defaultValue; | ||
| } | ||
| }); | ||
| const defaultRecord = (schema, record) => Object.keys(schema).reduce((result, key) => _extends({}, result, schema[key].hasDefault && !record[key] ? { [key]: schema[key].defaultValue } : {}), {}); | ||
| recordKeys.forEach(key => { | ||
| if (schemaKeys.indexOf(key) > -1) { | ||
| parsedRecord[key] = schema[key].parse(record[key]); | ||
| const parseRecord = (schema, record, defaultedRecord) => Object.keys(record).reduce((result, key) => _extends({}, result, schema[key] ? { [key]: schema[key].parse(record[key], key) } : {}), defaultedRecord); | ||
| const handler = schema => ({ | ||
| get: (target, property) => { | ||
| if (schema[property]) { | ||
| return schema[property].fn ? schema[property].fn(target) : target[property]; | ||
| } | ||
| }); | ||
| schemaKeys.forEach(key => { | ||
| if (schema[key].fn) { | ||
| parsedRecord[key] = schema[key].fn(parsedRecord); | ||
| return false; | ||
| }, | ||
| set: (target, property, value) => { | ||
| if (!schema[property]) { | ||
| throw new Error(`${property} is not a property of model`); | ||
| } | ||
| }); | ||
| return new Proxy(parsedRecord, { | ||
| get: (target, property) => { | ||
| if (schema[property]) { | ||
| if (schema[property].fn) { | ||
| return schema[property].fn(target); | ||
| } | ||
| target[property] = value; | ||
| return target[property]; | ||
| } | ||
| return true; | ||
| } | ||
| }); | ||
| return false; | ||
| }, | ||
| set: (target, property, value) => { | ||
| if (schema[property]) { | ||
| target[property] = value; | ||
| const objectModel = exports.objectModel = schema => (record = {}) => { | ||
| requiredCheck(schema, record); | ||
| return true; | ||
| } | ||
| throw new Error(`${property} is not a property of model`); | ||
| } | ||
| }); | ||
| return new Proxy(parseRecord(schema, record, defaultRecord(schema, record)), handler(schema)); | ||
| }; | ||
| exports.default = objectModel; |
+4
-2
@@ -20,5 +20,7 @@ { | ||
| "gulp-eslint-if-fixed": "^1.0.0", | ||
| "husky": "^0.14.3", | ||
| "jest": "^23.5.0", | ||
| "lint-staged": "^7.2.0", | ||
| "pkg-ok": "^2.2.0" | ||
| "pkg-ok": "^2.2.0", | ||
| "prettier": "^1.14.2" | ||
| }, | ||
@@ -59,3 +61,3 @@ "engines": { | ||
| }, | ||
| "version": "0.0.1" | ||
| "version": "0.0.2" | ||
| } |
10423
4.59%16
14.29%225
-5.86%