Schema Validation - case insensitive "pattern"

Hi all,
I would like to perform a “pattern” validation in the schema validation, but I cannot find a proper documentation of the operator.
In details, the validation that I am doing now is below, but it is case-sensitive
“pattern”: “.clx$”
Can someone help me, please?

Regards,
Andrea

Hello @Andrea_Agostini,

The regular expression pattern for matching something like ‘f1.clx’, ‘file2.CLX’, etc., would be: /.\.clx$/i

This will match any string that ends with “.clx”, has a character or more before the “.clx” and the whole string can be in any case (case-insensitive).

The query operator used is the following:

Hello @Prasad_Saya ,
thank you very much for your quick answer, but it doesn’t work (or I am doing something wrong).
I get the error below:
“specifiedAs” : { “pattern” : “/.\.clx$/i” }, “reason” : “regular expression did not match”, “consideredValue” : “R01_JM5_F1_OUT01.spec”

I have defined the schema validator as below (copy-paste from Compass):
Capture
It is also very strange because the \ is in black, as it is invalid but I am not able to sort this out.

Regards,
Andrea

1 Like

@Andrea_Agostini, can you give me examples of what is a valid match and invalid match for the regex pattern. You need to tell about the strings you are trying to match (not the regex pattern).

1 Like

Hi @Prasad_Saya ,
I have 2 collections; in both I have the “name” key. In one collection the value of “name” must be a valid file name (without path, just the name) including the extension .clx (case insensitive).
In the second collection it is the same but the file extension is .spec (still case insensitive).
Example of file name are: R01_JM5_F1_OUT01.spec or E2_v21b2_R08_JM43_N8-2_P42S4.clx.
There is not a pattern in the file name; it is just a valid Windows file name.

Thank you for the help,
Andrea

@Andrea_Agostini, the following schema validator with the validation pattern can be used to validate the file names with extensions “clx” or “spec”, in a case insensitive way.

let schema =  {
      required: [ "name" ],
      properties: {
            name: {
                bsonType: "string",
                pattern: ".\\.([cC][lL][xX]|[sS][pP][eE][cC])",
                description: "Can only have file names with extension clx or spec (case insensitive)"
            }
      }
}

In case you want validation for only the extension “clx”, use the following pattern: ".\\.[cC][lL][xX]"
And, only for the extension “spec”, use this one: ".\\.[sS][pP][eE][cC]"

This works! Thank you very much! Really appreciated.

1 Like

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