I want to validate my schema using Atlas app services serverless function as I did with Mongoose and MongoDB.
My Schema
{
"title": "user",
"type": "object",
"required": [
"firstName",
"lastName",
"email"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"firstName": {
"bsonType": "string",
"description": "First Name of the user"
},
"lastName": {
"bsonType": "string",
"description": "Last Name of the user"
},
"email": {
"bsonType": "string",
"description": "Email address of the user"
}
},
"errorMessage": {
"required": {
"firstName": "Please enter your first name",
"lastName": "Please enter your last name",
"email": "Please enter your email address"
}
}
}
I presume you want to validate that existing documents in the collection conform to the schema. In addition to this, you can also validate changes to documents by defining a validation expression in the schema.
Please refer to the six steps procedure to Enforce a Schema in the Atlas App Services.
I hope it helps. Let us know if you have any other queries.
Hey @Kushagra_Kesav. Thank you. Yes I love the way that Atlas app services schema validation feature validate all my existing documents in the collection but I really want to validate my schema before inserting single document or multiples documents in my collection. Can I validate my atlas app services schema before insertion? If yes than how can I validate? Are App services have built in schema validation feature like mongoose?
Schemas let you require specific fields, control the type of a field’s value, and validate changes before committing write operations.
In fact, all App Services data access (Data API, Graph QL, Functions, Sync) enforce that documents are valid according to schemabefore inserting or updating any data.
Hey, @Kushagra_Kesav I have this schema in Atlas App Services users collection schema
{
"errorMessage": {
"required": {
"email": "Please enter your email address",
"firstName": "Please enter your first name",
"lastName": "Please enter your last name"
}
},
"properties": {
"_id": {
"bsonType": "objectId"
},
"email": {
"bsonType": "string",
"description": "Email address of the user"
},
"firstName": {
"bsonType": "string",
"description": "First Name of the user"
},
"lastName": {
"bsonType": "string",
"description": "Last Name of the user"
}
},
"required": [
"firstName",
"lastName",
"email"
],
"title": "user",
"type": "object"
}
Now I want to validate my document before insertion. This is my function code
Now In my schema I have set email field required but in my doc I am not passing email field but this method is insert doc into my database instead of giving error that email field is required so this is not validating my doc before insertion.
Atlas app services schema only validate existing documents in my users collection. Please review this reply and I am waiting for your reply.
Hi, I would be happy to help look into this. Do you mind sharing a link to the function in question? There are various settings at play that might be contributing to this and it might reduce the number of back-and-forths for me to just view them.
Am I need to copy the whole function code or just function link? This function I have written on atlas cloud so this will not allow any other user to view function code on my account before logging in. so I am pasting my function code here
Hi, I was asking for the link / url in the UI when you are editing your function so that I can tie it to our internal logging system and see what might be going on. If you have either your URL, Project Id, or Application Id I can use any of those. Only MongoDB employees are able to see this URL.