CBValidation

Background

Whenever data is coming from the outside world, it make sense to verify that it is what you expect to arrive. This is both a security issue but also a simple user experience one. Validation cuts a large amount of the "non-happy paths" that your code can take.

CBValidation is the main server side validation engine for ColdBox Applications and allows checking incoming data ( or any data for that matter ) on a wide variety of constraints such as data type, minimum or maximum value, length of string and more.

Validating Data with CBValidation

  1. From CommandBox from the root of the site type install cbvalidation .

  2. Reinit the app by either visiting https://justenough.local/?fwreinit=1 or restarting the server

  3. Add the validation.

        function show(event, rc, prc){
    
            var valid = validateOrFail(target=rc, constraints={
                "userId": {"required":true, "type":"numeric","min":1}
            });
    
            var allUsers = userService.allUsers( valid.userId );
            prc.response.setData(allusers);
        }

Target is the collection of data you want assessed

Contraints are the fields you want validated

Valid is the variable which will hold the validated data. It will only contain the keys which are defined in the constraints. Even if more are defined in the target, they will not be included.

For more information about CBValidation check out the documentation.

Last updated