Nested Struct-Array Field Names

CBValidation provides powerful shortcuts for validating nested data structures without requiring complex constraint definitions. Using dot notation for structs and asterisk (*) notation for arrays, you can validate deeply nested objects with clean, readable constraint definitions.

Why Use Field Name Shortcuts?

Traditional nested validation would require defining complex constraint structures that mirror your data. Field name shortcuts allow you to:

  • Simplify constraint definitions - Use flat, dot-notation paths instead of nested structures

  • Improve readability - Clear field paths like user.profile.email are self-documenting

  • Reduce complexity - Avoid deeply nested constraint objects

  • Handle dynamic structures - Validate arrays of unknown length with * notation

Nested Struct Shorthand

For a nested struct, this is done by defining the field as a dot-delimited field name following the nested structure.

var validationResult = validate(
    target = {
        "address": {
            "streetOne" : "123 Elm Street",
            "streetTwo" : "",
            "city"      : "Anytown",
            "state"     : "IL",
            "zip"       : "60606"
        }
    },
    constraints = {
        "address": { "required": true, "type": "struct" },
        "address.streetOne": { "required": true, "type": "string" },
        "address.streetTwo": { "required": false, "type": "string" },
        "address.city": { "required": true, "type": "string" },
        "address.state": { "required": true, "type": "string", "size": 2 },
        "address.zip": { "required": true, "type": "numeric", "size": 5 }
    }
);

This can be continued as many levels deep as necessary.

Array Validation Shortcuts

Simple Array Validation

Use the asterisk (*) notation to validate all items in an array:

Array of Structs

Validate objects within arrays by combining dot notation with asterisk:

Complex Nested Arrays

Handle deeply nested structures with multiple array levels:

Combined Struct and Array Examples

User Profile with Multiple Addresses

E-commerce Order Structure

Error Message Context

When validation fails on nested fields, the error messages include the full field path for precise error identification:

Best Practices

1. Use Descriptive Field Names

3. Validate Array Container and Items

Common Use Cases

API Request Validation

Perfect for validating complex JSON payloads:

Form Data Processing

Handle complex form submissions with nested data:

Configuration Validation

Validate application configuration files:

Field name shortcuts make CBValidation incredibly powerful for handling real-world data structures while keeping your constraint definitions clean and maintainable.

Last updated

Was this helpful?