Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Collection validator: JSON Schema #2233

Closed
alexandre-abrioux opened this issue Oct 23, 2020 · 2 comments
Closed

Collection validator: JSON Schema #2233

alexandre-abrioux opened this issue Oct 23, 2020 · 2 comments
Labels
Milestone

Comments

@alexandre-abrioux
Copy link
Contributor

alexandre-abrioux commented Oct 23, 2020

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

Hi,

I don't think it is currently possible to define a JSON schema to validate, on the database side, inserted or updated data. I am talking about the $jsonSchema operator of the validator expression: https://docs.mongodb.com/manual/core/schema-validation/#json-schema

Do you think that it would be something worth adding?

We could even generate that schema with all the metadata that we already have, with an option on the collection:

/**
 * @ODM\Document(
 *      collection="users",
 *      jsonSchemaValidator=true
 * )
 */

; or let the user specify the schema manually if needed:

/**
 * @ODM\Document(
 *      collection="users",
 *      jsonSchemaValidator=true,
 *      jsonSchema="{
            name: {
               bsonType: "string",
               description: "must be a string and is required"
            }
        }"
 * )
 */

I think it could be a nice feature to have. If it is something that you would be interested in I could write a PR, I'm just asking for your opinion about that idea first. Let me know! :)

Thank you!

@malarzm malarzm added the Idea label Oct 24, 2020
@malarzm malarzm added this to the 2.x milestone Oct 24, 2020
@malarzm
Copy link
Member

malarzm commented Oct 24, 2020

@alexandre-abrioux this sound like a nice addition :) If you'd need any help feel free to ping me on our Slack's #mongodb-odm channel.

We could even generate that schema with all the metadata that we already have, with an option on the collection

I think for a first step we should go with user-defined jsonSchema, generating schema based on metadata can be added later. Also I expect schema-generation to be a can of worms if we get to single collection inheritance or discriminated embedded documents so I'd rather defer this until later date and not get you into bigger troubles :)

@alexandre-abrioux
Copy link
Contributor Author

Thanks for the answer! I agree, let's do it by step. I'll start working on adding an user-defined jsonSchema and get back to you as soon as I have a working solution with tests. I'll check Slack if needed :)

@alcaeus alcaeus modified the milestones: 2.x, 2.3.0 May 26, 2021
@alcaeus alcaeus added Feature and removed Idea labels May 26, 2021
@alcaeus alcaeus closed this as completed May 26, 2021
@alcaeus alcaeus modified the milestones: 2.3.0-alpha1, 2.3.0 Sep 9, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants