totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

What should I do when some schema property is only required with certain filter ? #559

Closed ckpiggy closed 7 years ago

ckpiggy commented 7 years ago

For example: A user schema got 3 properties : id, name, email, and the 3 properties are all required only when creating. I want to allow user only change email or name.

NEWSCHEMA('User').make(function(schema) {
    schema.define('id', Number, true, 'U')
    schema.define('name', String, true, 'C')
    schema.define('email', String, true, 'C')
    schema.define('name, String, false, 'U')
    schema.define('email', String, false, 'U')
})

these code wont work when I trying to update name with put body {id: 'myId', name: 'new name'}, framework send errors to me.

[
    {
        "name": "email",
        "error": "The field \"email\" is invalid.",
        "path": "User.email"
    }
]

Is there any option to setup these properties? Or I should just simply create another schema for update operation?

petersirka commented 7 years ago

Show me your route declaration please with this schema.

ckpiggy commented 7 years ago

controllers/user.js

exports.install = function () {
  F.route('/api/user', save, ['post', 'json', '*User#C'])
  F.route('/api/user', save, ['put', 'json', '*User#U'])
}
function save(){
  this.$save()
}

models/user.js

NEWSCHEMA('User').make(function(schema) {
  schema.define('id', Number, true, 'U')
  schema.define('name', String, true, 'C')
  schema.define('email', String, true, 'C')
  schema.define('name, String, false, 'U')
  schema.define('email', String, false, 'U')

  schema.setSave(function ($){
    /*...some code...*/
  })
})
petersirka commented 7 years ago

I know so... You can't declare two same fields with different filter. This is not possbible. I recommend to set schema.setValidate() for create or create two schemas create and update. I recommend to create two schemas.

ckpiggy commented 7 years ago

Thanks !!