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

default requirement of valid object keys within array? #486

Closed raza2022 closed 1 year ago

raza2022 commented 1 year ago

Hi dev, thanks for a great package, however, I want to confirm one thing about the requirement of nested object keys ([object]) let's see my use case there should be one object existing for that array else it will throw an error but I am surprised that if I passed the empty array [] it validates it too, I see some other way to validate object keys but my general assumption is all keys defined within the object should be validated unless passed the {optional:true}

new SimpleSchema({
        'plan.bonuses': {
            type: Array,
        },
        'plan.bonuses.$': Object,
        'plan.bonuses.$.title': {
            type: String,
        },
    })

so I want it will only validate if I pass the array with at least one object else throw error on []

bonuses: [{
title: 'test'
}]
github-actions[bot] commented 1 year 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.

raza2022 commented 1 year ago

sorry it's very simple maybe I missed it because of working at night it was simply solved by minCount as at first read I thought it was only for singular values, now it's solved like below

bonuses: {
        type: Array,
        minCount: 1,
    },