longshotlabs / simpl-schema

A JavaScript schema validation package that supports direct validation of MongoDB update modifier objects
https://www.npmjs.com/package/simpl-schema
MIT License
560 stars 114 forks source link

How to validate Objects in Object #413

Closed Nedogin closed 8 months ago

Nedogin commented 4 years ago

I have the following Meteor collection and want to attach this schema

Devices.schema = new SimpleSchema({
     iBID: { type: String, unique: true, required: true },
     name: { type: String, required: true },
     model: { type: String, required: true },
     roomID: { type: String, required: true },
     props: { type: Object, required: true },
});

my problem is, that the object looks like this

{
   "_id":"vxMhMrnxMTwE6Fcqm",
   "iBID":"0_userdata.0.mockSensor2",
   "name":"Mocksensor 2",
   "model":"ikea",
   "roomID":"asdawdasdwadsdw",
   "props": {
      "open":{
         "id":"0_userdata.0.mockSensor2.open",
         "value":2
      },
      "reachable":{
         "id":"0_userdata.0.mockSensor2.reachable",
         "value":false
      }
   }
}

and i want to validate the objects inside the object "props". The on thing is that for example "open" in "props" can change. So its not always open and reachable.

One more thing is that i tried it with the code

"props.$": Object,
"props.$.id": String,
"props.$.value": SimpleSchema.oneOf(String, Boolean, Number),

in the schema but its not working. What am i missing? When i set blackbox: true then i can pass in anything.

Maybe im just confused. Can anyone help me :)

github-actions[bot] commented 4 years ago

Thank you for submitting an issue!

If this is a bug report, please be sure to include, at minimum, example code showing a small schema and any necessary calls with all their arguments, which will reproduce the issue. Even better, you can link to a saved online code editor example, where anyone can immediately run the code and see the issue.

If you are requesting a feature, include a code example of how you imagine it working if it were implemented.

If you need to edit your issue description, click the [...] and choose Edit.

Be patient. This is a free and freely licensed package that I maintain in my spare time. You may get a response in a day, but it could also take a month. If you benefit from this package and would like to see more of my time devoted to it, you can help by sponsoring.

aldeed commented 8 months ago

Closing old issues