@commodo/fields
Advanced tools
Comparing version 0.2.6 to 0.2.7
@@ -16,4 +16,2 @@ "use strict"; | ||
var _ramda = require("ramda"); | ||
var _createField = _interopRequireDefault(require("./createField")); | ||
@@ -30,8 +28,10 @@ | ||
rest = (0, _objectWithoutProperties2.default)(_ref, ["list"]); | ||
return (0, _ramda.compose)((0, _withFieldDataTypeValidation.default)(value => { | ||
return typeof value === "boolean"; | ||
}))((0, _createField.default)(_objectSpread({}, rest, { | ||
const field = (0, _createField.default)(_objectSpread({}, rest, { | ||
list, | ||
type: "boolean" | ||
}))); | ||
})); | ||
(0, _withFieldDataTypeValidation.default)(value => { | ||
return typeof value === "boolean"; | ||
})(field); | ||
return field; | ||
}; | ||
@@ -38,0 +38,0 @@ |
@@ -22,4 +22,2 @@ "use strict"; | ||
var _ramda = require("ramda"); | ||
var _createField = _interopRequireDefault(require("./createField")); | ||
@@ -65,13 +63,7 @@ | ||
return (0, _ramda.compose)((0, _withFieldDataTypeValidation.default)(value => { | ||
if (typeof value === "object") { | ||
if ((0, _fields.hasFields)(value)) { | ||
return value instanceof instanceOf; | ||
} | ||
return true; | ||
} | ||
return false; | ||
}), (0, _repropose.withProps)(instance => { | ||
const field = (0, _createField.default)(_objectSpread({}, rest, { | ||
list, | ||
type: "fields" | ||
})); | ||
(0, _repropose.withProps)(instance => { | ||
const { | ||
@@ -177,6 +169,15 @@ setValue, | ||
}; | ||
}))((0, _createField.default)(_objectSpread({}, rest, { | ||
list, | ||
type: "fields" | ||
}))); | ||
})(field); | ||
(0, _withFieldDataTypeValidation.default)(value => { | ||
if (typeof value === "object") { | ||
if ((0, _fields.hasFields)(value)) { | ||
return value instanceof instanceOf; | ||
} | ||
return true; | ||
} | ||
return false; | ||
})(field); | ||
return field; | ||
}; | ||
@@ -183,0 +184,0 @@ |
@@ -16,4 +16,2 @@ "use strict"; | ||
var _ramda = require("ramda"); | ||
var _createField = _interopRequireDefault(require("./createField")); | ||
@@ -30,8 +28,10 @@ | ||
rest = (0, _objectWithoutProperties2.default)(_ref, ["list"]); | ||
return (0, _ramda.compose)((0, _withFieldDataTypeValidation.default)(value => { | ||
return typeof value === "number" && value > -Infinity && value < Infinity; | ||
}))((0, _createField.default)(_objectSpread({}, rest, { | ||
const field = (0, _createField.default)(_objectSpread({}, rest, { | ||
list, | ||
type: "number" | ||
}))); | ||
})); | ||
(0, _withFieldDataTypeValidation.default)(value => { | ||
return typeof value === "number" && value > -Infinity && value < Infinity; | ||
})(field); | ||
return field; | ||
}; | ||
@@ -38,0 +38,0 @@ |
@@ -16,4 +16,2 @@ "use strict"; | ||
var _ramda = require("ramda"); | ||
var _createField = _interopRequireDefault(require("./createField")); | ||
@@ -30,8 +28,10 @@ | ||
rest = (0, _objectWithoutProperties2.default)(_ref, ["list"]); | ||
return (0, _ramda.compose)((0, _withFieldDataTypeValidation.default)(value => { | ||
return typeof value === "string"; | ||
}))((0, _createField.default)(_objectSpread({}, rest, { | ||
let field = (0, _createField.default)(_objectSpread({}, rest, { | ||
list, | ||
type: "string" | ||
}))); | ||
})); | ||
(0, _withFieldDataTypeValidation.default)(value => { | ||
return typeof value === "string"; | ||
})(field); | ||
return field; | ||
}; | ||
@@ -38,0 +38,0 @@ |
{ | ||
"name": "@commodo/fields", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"main": "index.js", | ||
@@ -17,3 +17,3 @@ "repository": { | ||
"dependencies": { | ||
"repropose": "^1.0.0" | ||
"repropose": "^1.0.2" | ||
}, | ||
@@ -32,3 +32,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "2b3250e5ee7de76475e48a509309acee3a7cbacb" | ||
"gitHead": "c953bb97266286551493d864eb91263174a88c5a" | ||
} |
@@ -15,60 +15,76 @@ "use strict"; | ||
const withFields = fields => { | ||
return (0, _ramda.compose)((0, _repropose.withStaticProps)({ | ||
__withFields: true // For satisfying hasFields helper function. | ||
return baseFn => { | ||
let fn = (0, _repropose.withProps)(props => { | ||
if (props.__withFields) { | ||
return {}; | ||
} | ||
}), (0, _repropose.withProps)(instance => { | ||
let fieldsList = fields; | ||
return { | ||
__withFields: { | ||
fields: {}, | ||
processing: { | ||
validation: false, | ||
dirty: false | ||
} | ||
}, | ||
if (typeof fields === "function") { | ||
fieldsList = fields(instance); | ||
} | ||
getFields() { | ||
return this.__withFields.fields; | ||
}, | ||
for (let newFieldName in fieldsList) { | ||
const valueFactory = fieldsList[newFieldName]; | ||
instance.__withFields.fields[newFieldName] = new valueFactory(newFieldName, instance); | ||
Object.defineProperty(instance, newFieldName, { | ||
get() { | ||
if (this.__withFields.fields[newFieldName].get) { | ||
return this.__withFields.fields[newFieldName].get.call(this, this.__withFields.fields[newFieldName]); | ||
getField(name) { | ||
return this.__withFields.fields[name]; | ||
}, | ||
populate(data) { | ||
if (data && typeof data === "object") { | ||
const values = this.getFields(); | ||
for (let valueKey in values) { | ||
const value = values[valueKey]; | ||
if (!value || value.skipOnPopulate || !(valueKey in data)) { | ||
continue; | ||
} | ||
values[valueKey].setValue(data[valueKey]); | ||
} | ||
} | ||
return this.__withFields.fields[newFieldName].getValue(); | ||
return this; | ||
}, | ||
set(value) { | ||
if (this.__withFields.fields[newFieldName].set) { | ||
this.__withFields.fields[newFieldName].set.call(this, this.__withFields.fields[newFieldName], value); | ||
} else { | ||
this.__withFields.fields[newFieldName].setValue(value); | ||
async validate() { | ||
if (this.__withFields.processing.validation) { | ||
return; | ||
} | ||
} | ||
}); | ||
} | ||
this.__withFields.processing.validation = true; | ||
const invalidFields = {}; | ||
const fields = this.getFields(); | ||
return {}; | ||
}), (0, _repropose.withProps)(props => { | ||
if (props.__withFields) { | ||
return {}; | ||
} | ||
for (let name in fields) { | ||
const field = fields[name]; | ||
return { | ||
__withFields: { | ||
fields: {}, | ||
processing: { | ||
validation: false, | ||
dirty: false | ||
} | ||
}, | ||
try { | ||
await field.validate(); | ||
} catch (e) { | ||
invalidFields[name] = { | ||
code: e.code || _fields.WithFieldsError.VALIDATION_FAILED_INVALID_FIELD, | ||
data: e.data || null, | ||
message: e.message | ||
}; | ||
} | ||
} | ||
getFields() { | ||
return this.__withFields.fields; | ||
}, | ||
this.__withFields.processing.validation = false; | ||
getField(name) { | ||
return this.__withFields.fields[name]; | ||
}, | ||
if (Object.keys(invalidFields).length > 0) { | ||
throw new _fields.WithFieldsError("Validation failed.", _fields.WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS, { | ||
invalidFields | ||
}); | ||
} | ||
}, | ||
populate(data) { | ||
if (data && typeof data === "object") { | ||
clean() { | ||
const values = this.getFields(); | ||
@@ -78,78 +94,67 @@ | ||
const value = values[valueKey]; | ||
value && value.isDirty() && value.clean(); | ||
} | ||
}, | ||
if (!value || value.skipOnPopulate || !(valueKey in data)) { | ||
continue; | ||
} | ||
values[valueKey].setValue(data[valueKey]); | ||
isDirty() { | ||
if (this.__withFields.processing.dirty) { | ||
return false; | ||
} | ||
} | ||
return this; | ||
}, | ||
this.__withFields.processing.dirty = true; | ||
const fields = this.getFields(); | ||
async validate() { | ||
if (this.__withFields.processing.validation) { | ||
return; | ||
} | ||
for (let valueKey in fields) { | ||
const field = fields[valueKey]; | ||
this.__withFields.processing.validation = true; | ||
const invalidFields = {}; | ||
const fields = this.getFields(); | ||
for (let name in fields) { | ||
const field = fields[name]; | ||
try { | ||
await field.validate(); | ||
} catch (e) { | ||
invalidFields[name] = { | ||
code: e.code || _fields.WithFieldsError.VALIDATION_FAILED_INVALID_FIELD, | ||
data: e.data || null, | ||
message: e.message | ||
}; | ||
if (field && field.isDirty()) { | ||
this.__withFields.processing.dirty = false; | ||
return true; | ||
} | ||
} | ||
} | ||
this.__withFields.processing.validation = false; | ||
if (Object.keys(invalidFields).length > 0) { | ||
throw new _fields.WithFieldsError("Validation failed.", _fields.WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS, { | ||
invalidFields | ||
}); | ||
this.__withFields.processing.dirty = false; | ||
return false; | ||
} | ||
}, | ||
clean() { | ||
const values = this.getFields(); | ||
}; | ||
})(baseFn); | ||
fn = (0, _repropose.withProps)(instance => { | ||
let fieldsList = fields; | ||
for (let valueKey in values) { | ||
const value = values[valueKey]; | ||
value && value.isDirty() && value.clean(); | ||
} | ||
}, | ||
if (typeof fields === "function") { | ||
fieldsList = fields(instance); | ||
} | ||
isDirty() { | ||
if (this.__withFields.processing.dirty) { | ||
return false; | ||
} | ||
for (let newFieldName in fieldsList) { | ||
const valueFactory = fieldsList[newFieldName]; | ||
instance.__withFields.fields[newFieldName] = new valueFactory(newFieldName, instance); | ||
Object.defineProperty(instance, newFieldName, { | ||
get() { | ||
if (this.__withFields.fields[newFieldName].get) { | ||
return this.__withFields.fields[newFieldName].get.call(this, this.__withFields.fields[newFieldName]); | ||
} | ||
this.__withFields.processing.dirty = true; | ||
const fields = this.getFields(); | ||
return this.__withFields.fields[newFieldName].getValue(); | ||
}, | ||
for (let valueKey in fields) { | ||
const field = fields[valueKey]; | ||
if (field && field.isDirty()) { | ||
this.__withFields.processing.dirty = false; | ||
return true; | ||
set(value) { | ||
if (this.__withFields.fields[newFieldName].set) { | ||
this.__withFields.fields[newFieldName].set.call(this, this.__withFields.fields[newFieldName], value); | ||
} else { | ||
this.__withFields.fields[newFieldName].setValue(value); | ||
} | ||
} | ||
} | ||
this.__withFields.processing.dirty = false; | ||
return false; | ||
}); | ||
} | ||
}; | ||
})); | ||
return {}; | ||
})(fn); | ||
fn = (0, _repropose.withStaticProps)({ | ||
__withFields: true // For satisfying hasFields helper function. | ||
})(fn); | ||
return fn; | ||
}; | ||
}; | ||
@@ -156,0 +161,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
66981
744
Updatedrepropose@^1.0.2