Validation().dataMapper()
Sets a data mapper function to map request body values with form fields.
Syntax
Parameters
-
mappingFunction
A function to add that will be called with the following arguments:-
req
A request object. -
form
A form object created with theValidation.profile()
method.
-
Return value
The Validation
object.
Exceptions
-
If anything other than a function is passed in the corresponding error will be thrown.
-
If invoked on a
Validation
which was not created with theValidation.profile()
method, throws the corresponding error.
Description
The main purpose of this method is to provide a way of mapping a request object’s body with form fields specified in the Validation.profile()
method in cases when they do not correspond to each other when a Validation
is used as a middleware function.
Examples
Validating file metadata before uploading to S3 storage
In this example the Validation().dataMapper()
method is used to map the values passed as URL search parameters to the form fields created with the Validation.profile()
method.
The project can be downloaded here.
Directorypublic
Directorylibs
- isomorphic-validation.mjs the library file is copied and imported from here to be available on the client side without using a module bundler
- index.html
- index.js the main frontend script
- validation.js file size and type validation code
- config.js a config for S3 storage
- index.js the main backend script
- repository.js a mock repository API
- …