Env: node 14, latest MongoDB, latest MongoDB node driver.
Validation schema:
{
"validator": {
"$jsonSchema": {
"bsonType": "object",
"properties": {
"int": {
"bsonType": "int"
},
"long": {
"bsonType": "long"
},
"double": {
"bsonType": "double"
},
"decimal": {
"bsonType": "decimal"
}
}
}
},
"validationLevel": "moderate",
"validationAction": "error"
}
Operation:
const foo= 'foo';
const data = {
int: new Int32(foo),
long: new Long(foo),
double: new Double(foo),
decimal: new Decimal128(foo),
};
await db.collection.insertOne(data);
Result: inserted in db:
int = 0; // as valid Int32 bsonType
long = 0; // as valid Long bsonType
double = NaN; // as valid Double bsonType
// decimal throws error:
foo not a valid Decimal128 string TypeError
Question:
If anything you enter is converted into a valid record… what was the validation built for?
All these 4 conversion functions are wrong, the first 3 returns the valid data type to the wrong input, and Decimal128 throws an error … when in fact all 4 should return the wrong input so that it can then be invalidated by the validation rules.