Skip to content

Commit

Permalink
Enable deep merging by schema for Model
Browse files Browse the repository at this point in the history
  • Loading branch information
JaapRood committed May 3, 2018
1 parent bf1f4c5 commit fd803ad
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,25 @@ exports.create = (spec) => {
defaults
} = spec

let schema = spec.schema

Invariant(!defaults || Factory.isDefaults(defaults), 'When specifying defaults in the model spec it must be a plain object or Immutable Iterable')
Invariant(!schema || Schema.isSchema(schema), 'When specificying a schema for a model, it must be a valid schema definition')
Invariant(!spec.schema || Schema.isSchema(spec.schema), 'When specificying a schema for a model, it must be a valid schema definition')

if (!schema) schema = {}
if (!spec.schema) spec.schema = {}

const identity = Identity.create(typeName)
const factory = Factory.create(defaults)
const merge = Merge.create(identity, factory)
const schema = {
schema: () => spec.schema
}
const merge = Merge.create(identity, factory, schema)

const modelPrototype = _assign(
Object.create(internals.Model.prototype), // makes `x instanceof Model` work
identity,
factory,
merge,
schema,
{
schema: () => schema,
parse: internals.parse,
serialize: internals.serialize
}
Expand Down Expand Up @@ -161,7 +162,7 @@ internals.serialize = function(model, options) {
} else if (
Immutable.Iterable.isIndexed(modelValue) && _isArray(nestedSchema) ||
Immutable.Iterable.isKeyed(modelValue) && _isPlainObject(nestedSchema)
) {
) {
return this.serialize(modelValue, _assign({}, options, { schema: nestedSchema }))
}
}
Expand Down

0 comments on commit fd803ad

Please sign in to comment.