molnarg / js-schema

Simple and intuitive schema validator
http://molnarg.github.com/js-schema/
MIT License
388 stars 45 forks source link

Testing Object w/ Properties #45

Closed jescalan closed 8 years ago

jescalan commented 8 years ago

I may be just missing something in the docs here, but I really am stuck on this one, hoping someone can help me out.

I've got an object like this I want to validate:

var foo = {
  bar: { baz: 12, quux: 'wow' }
}

I want to validate it such that foo can optionally have a bar property or not, but if it does, bar is an object (not anything that is an instance of an object, which includes arrays), and that it has the keys baz and quux. Is there any way to do this with js-schema?

For the key, I assume I want ?bar, but I'm not sure what to put in for the value at all.

molnarg commented 8 years ago

Based on rule 2, Object should be good. https://github.com/molnarg/js-schema/#basic-rules Let me know if that doesn't work.

Jeff Escalante notifications@github.com ezt írta (időpont: 2015. dec. 27., V, 10:27):

I may be just missing something in the docs here, but I really am stuck on this one, hoping someone can help me out.

I've got an object like this I want to validate:

var foo = { bar: { baz: 12, quux: 'wow' } }

I want to validate it such that foo can optionally have a bar property or not, but if it does, bar is an object (not anything that is an instance of an object, which includes arrays), and that it has the keys baz and quux. Is there any way to do this with js-schema?

For the key, I assume I want ?bar, but I'm not sure what to put in for the value at all.

— Reply to this email directly or view it on GitHub https://github.com/molnarg/js-schema/issues/45.

jescalan commented 8 years ago

not anything that is an instance of an object, which includes arrays

Doesn't make the cut here. I ended up using joi, so no need to put in a fix for this unless you want to for your or someone else's needs. Thank you for the quick response though, I really appreciate it!