Skip to content

start refactoring#2

Open
jonschlinkert wants to merge 8 commits into
masterfrom
dev
Open

start refactoring#2
jonschlinkert wants to merge 8 commits into
masterfrom
dev

Conversation

@jonschlinkert

@jonschlinkert jonschlinkert commented Mar 31, 2017

Copy link
Copy Markdown
Owner

cc @doowb

@jonschlinkert

jonschlinkert commented Apr 1, 2017

Copy link
Copy Markdown
Owner Author

@doowb, any thoughts on how to handle this case (where config.foo is already defined as a non-object value)?

var Schema = require('..');

var foo = new Schema()
  .field('a', {default: 'aaa'})
  .field('b', {default: 'bbb'})
  .field('c', {default: 'ccc'})

var config = {foo: 'bar'};
var schema = new Schema()
  .field('foo', foo)
  .normalize(config)

console.log(config)

Currently (in the refactor), when nested schemas are used, the value of config.foo would just be overwritten. I suppose the user could work around that by calling foo.normalize() inside field foo's field.normalize() method, e.g.:

var Schema = require('..');

var foo = new Schema()
  .field('a', {default: 'aaa'})
  .field('b', {default: 'bbb'})
  .field('c', {default: 'ccc'})

var config = {foo: 'bar'};
var schema = new Schema()
  .field('foo', {
    normalize: function(val, key, config, schema) {
      // decide what to do with val if not an object, or...
      return foo.normalize.apply(foo, arguments);
    }
  })
  .normalize(config)

console.log(config)

edit: I think I have a better solution. If a normalize function is passed on the schema options, that would be called on the config value itself.

var foo = new Schema({
  normalize: function() {
    // do stuff
  }})
  .field('a', {default: 'aaa'})
  .field('b', {default: 'bbb'})
  .field('c', {default: 'ccc'})

@doowb

doowb commented Apr 1, 2017

Copy link
Copy Markdown
Collaborator

edit: I think I have a better solution.

Yeah, I think this is how I envisioned it. Like if "foo" was "author" in a package.json and the "author" field was a string, but there was an "author" schema specified for it, that schema could transform the string into an object.

@jonschlinkert

Copy link
Copy Markdown
Owner Author

that schema could transform the string into an object.

yeah, it would have worked that way already. but this way it would also have the option of returning the string unchanged.

@doowb

doowb commented Apr 1, 2017

Copy link
Copy Markdown
Collaborator

Yeah, I think that makes sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants