javascript - How to represent range in mongoose schema? -
background
lets doing schema fruits can buy. in schema, fruit has price range:
let fruit = { name: "banana", price: { currency: "eur", range: [2, 10] } };
to achieve saving above object, trying use following schema:
let fruitschema = { name: {type: string, required: true}, price: { currency: {type: string, required: true}, range: [{ type: number, min: 0 }], validate: [arraysize] } }; const arraysize = function arraylimit(val) { return val.length === 2; };
problem:
my solution range part feels clunky, on complicated , doesn't check if max range value bigger min range value, i.e., doesn't check if 10 > 2.
question:
- how implement price range in schema in mongoose ?
my solution
i decided have both values in schema separately, in example bellow:
income: { currency: { type: string }, range: { min: { type: number, min: 0 }, max: { type: number, min: 0 } } }
problem
however, left problem. how check max >= min
@ times, , min <= max
??
given nor min nor max mandatory, 1 can have object upper range, or lower one.
enter custom validators
to fix validation issue, used custom validator functions. validators missing piece in puzzle, , allowed me enforce max , min restrictions:
validate object min:
validate: { validator: function(val){ const currmax = this.target.income.range.max; return (currmax !== undefined ? val <= currmax : true); }, message: "the min range value {value} must <= max range!" }
validate object max:
validate: { validator: function(val) { const currmin = this.target.income.range.min; return (currmin !== undefined ? val >= currmin : true); }, message: "the max range value {value} must >= min range!" }
final result
thus, final result follows:
income: { currency: { type: string }, range: { min: { type: number, min: 0, validate: { validator: function(val){ const currmax = this.target.income.range.max; return (currmax !== undefined ? val <= currmax : true); }, message: "the min range value {value} must <= max range!" } }, max: { type: number, min: 0, validate: { validator: function(val) { const currmin = this.target.income.range.min; return (currmin !== undefined ? val >= currmin : true); }, message: "the max range value {value} must >= min range!" } } } }
going further
this solution nice, shows advanced knowledge , requirements. in try insert object min > max
, have nice error message value
being inserted.
however, have following questions / suggestions improvement, didn't add because don't yet know how do:
the validator keep inconsistent objects being inserted. however, when not prevent bad updates, i.e., following example work on object min = 10:
objmodel.update( { _id: "58dd26b5476664e33eec4b93" }, { $set: {"income.range.max": 1}} );
to updates on objects using validators, must use save
method, , follow convention described @ end of mongoose docs.
model.findone({ name: 'borne' }, function (err, doc){ doc.name = 'jason borne'; doc.visits.$inc(); doc.save(); });
which trigger validator expected.
- in functions check min , max, can print value being evaluated using
{value}
. however, can use other values of object? when comparing min , max, useful print value being evaluated values being compared to.
hope helps, , inspires!
Comments
Post a Comment