I’ve created a Schema in an external file with all the mandatory and optional fields and on my client.js I have an array with the XLSX fields that were inputed by the user. My objective is to validade my array against my schema, output an error if any madatory field is missing or if any field has a wrong type of input. Here’s my code to change the input value if it’s wrong, in this case, if the input is a string “Yes” then evaluates it as true, else is false:
myField: {
type: (Boolean),
optional: true,
custom: function() {
let value = this.value;
if (value === "Yes") {
return value = true;
}
else { return value = false }
}
},
I’ve searched the simpl-schema docs on how to this, but I can’t manage to get it working with the examples provided, I’m stuck on this.
Thanks in advance.
You have to use autoValue
not custom
.
custom
is for field validation and not for changing the value.
const SimpleSchema = require( 'simpl-schema').default;
const schema = new SimpleSchema(
{
myBool:{
type: Boolean,
optional: true,
autoValue(){
if(this.value == "Yes"){
return true
} else if(this.value == "No") {
return false
}
}
}
}
)
const validationContext = schema.newContext()
//clean is called in Collection2 before validating
const res = schema.clean({
myBool: "Yes"
})
console.log("cleaned Object",res)
validationContext.validate(res)
console.log(validationContext.validationErrors())
Personally I wouldn't do the yes/true, no/false transformation in the schema.