Reputation: 11
I have json object from angular as {"weight": "50"}
but LoopBack 4 needs {"weight": 50}
. How can I achieve this? I do not want to make any change in angular. Is there any settings in LoopBack 4?
Please help.
Upvotes: 0
Views: 173
Reputation: 10795
Hello from the LoopBack team 👋
LoopBack uses the popular library AJV to validate incoming requests. It is possible to configure AJV to coerce types during validation, e.g. convert string "50"
to a number 50
for a numeric property, see Coercing data types.
To enable coercion, modify your main application file (src/application.ts
) as follows:
Import RestBindings
from @loopback/rest
:
import {RestApplication, RestBindings} from '@loopback/rest';
Add the following line at the end of your application constructor:
this.bind(RestBindings.REQUEST_BODY_PARSER_OPTIONS).to({
validation: {coerceTypes: true},
})
Please note this setup will enable coercion for all model properties and all value types. See AJV Coercion Rules to better understand what will happen to different value types.
Upvotes: 1