Meteor-Community-Packages / meteor-autoform

AutoForm is a Meteor package that adds UI components and helpers to easily create basic forms with automatic insert and update events, and automatic reactive validation.
MIT License
1.44k stars 328 forks source link

method-update returns badly formatted keys #1571

Closed pkpowell closed 3 years ago

pkpowell commented 7 years ago

I've been experimenting with simpl-schema + autoform v.6 and a form with method-update will return keys with the question mark formatting from the schema in place. This didn't use to be the case. Is this a bug or do I need to change something in my code?

{
  "_id": "wa25aF7P5xMuibu3C",
  "modifier": {
    "$set": {
      "companyPrefs.$.companyName": "abcdefg",
      "companyPrefs.$.emailBaseFQDN": [
        "abcdefg"
      ]
    }
  }
} undefined

Here's the corresponding schema

const prefs = new simplschema({
    "companyPrefs": Object,
    "companyPrefs.$.companyName": String,
    "companyPrefs.$.emailBaseFQDN": Array,
    "companyPrefs.$.emailBaseFQDN.$": String,
})
cryve commented 7 years ago

As far as a can see in the docs your schema should be:

const prefs = new simplschema({
    companyPrefs: Object,
    "companyPrefs.companyName": String,
    "companyPrefs.emailBaseFQDN": Array,
    "companyPrefs.emailBaseFQDN.$": String,
})
pkpowell commented 7 years ago

Thanks, that helps! Now I'm getting Match error: Unknown key in field $set Previously the autoform-method returned an object (that passed check() and clean()) like this:

 { '$set': 
    { 
      'companyPrefs.companyName': 'abc',
      'companyPrefs.emailBaseFQDN': [ 'xyz.com' ],
      'apiKeys.googleMapsAPI': '123456',
     }
 } 

With autoform v.6 and the new node simpl-schema it it comes with extra _id and 'modifier' keys

{ _id: '3SRKjSxmnnkJ6wMsp',
  modifier: 
   { '$set': 
      { 'companyPrefs.companyName': 'abc',
        'companyPrefs.emailBaseFQDN': [ 'xyz.com' ],
        'apiKeys.googleMapsAPI': '123456',
        } 
} }

So in my method I've tried running checkand clean on doc.modifier:

            let mod = doc.modifier
            check(mod, schemas.prefs)
            schemas.prefs.clean(mod, {
                extendAutoValueContext: {
                    isInsert: false,
                    isUpdate: true,
                    isUpsert: false,
                    isFromTrustedCode: false
                }
            })

but it baulks at Unknown key in field $set

aldeed commented 7 years ago

Yeah, I think I changed it to pass as a single object by default in order to work with mdg:validated-method. The default used to be two separate args. But you should only have to use doc.modifier

peernohell commented 7 years ago

It would be nice to have in the Note about version 6 that the parameter of the method have change. before it was methodName: function (modifier, _id) {} now it's a single object methodName: function ({ modifier: _id }){}. for the error, I'm not sure but I think that node-simple-schema is didn't work with check function like before :/