Validate schema with conditions

Hi all,
I have a collection done as from image.
Capture

I would like to implement the following rules using the schema validation:

  • IF “type” = 0 or 2, “parameterDelta” must be empty
  • IF “type” = 1, “parameterValues” must be empty.
  • IF “type” = 1, “baseClxName” must be not null; it must be null in the other cases
    “type” can have the following values only 0, 1 and 2.

I have been able to implement just the “Enum” fot “type”; can someone suggest me how to implement the rest, please?

$jsonSchema: {
properties: {
type: {
enum: [0, 1 ,2],
description: “Can only be either 0, 1 or 2’”
}
}
}

1 Like

Hello @Andrea_Agostini,

You can try this for your validation criteria. Since, you have the type field used to define constraints on other fields the following is possible.

1. Define Schema Types:

let schemaType0Or2 =  {
      required: [ "type", "baseClxName", "parameterDelta", "parameterValues"],
      properties: {
            type: {
                enum: [ 0 , 2 ],
                description: "Can only be 0 or 2"
            },
            baseClxName: {
                bsonType: "null",
                description: "Can only be a null"
            },
            parameterDelta: {
                bsonType: "array",
                maxItems: 0,
                description: "Can only be empty"
            },
            parameterValues: {
                bsonType: "array",
                maxItems: 0,
                description: "Can only be empty"
            },
      }
}

let schemaType1 =  {
      required: [ "type", "baseClxName", "parameterValues" ],
      properties: {
            type: {
                enum: [ 1 ],
                description: "Can only be 1"
            },
            baseClxName: {
                bsonType: "string",
                description: "Can only be a string"
            },
            parameterValues: {
                bsonType: "array",
                maxItems: 0,
                description: "Can only be empty"
            },
      }
}

2. Create Collection using the Schema Validation

db.createCollection( 
    "testCollection", 
    { validator: 
        { $jsonSchema: 
            { oneOf: [ schemaType0Or2, schemaType1 ] } 
        } 
    } 
)

3. Insert Data (and validate)

For example, the following document will pass the validation and gets inserted - as baseClxName is not null and parameterValues is an empty array.

db.test.insertOne({type: 1, baseClxName: "str-1", parameterValues: [] })

These following two will fail, as baseClxName is null and parameterValues is not empty ([ 1, 2 ]), respectively:

db.testCollection.insertOne({type: 1, baseClxName: null, parameterValues: [] })
db.testCollection.insertOne({type: 1, baseClxName: "str-2", parameterValues: [ 1, 2 ] })

Similarly, you can verify for the type field values 0 and 2.

2 Likes

This works, thank you very much!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.