Null Value Handling
Understanding null value handling in validation
Overview
Validation Rules for Nulls
Required Fields
var constraints = {
email: { required: true, type: "email" }
};
// ❌ FAILS - null is treated as missing
var result = validate(target={ email: javacast("null", "") }, constraints: constraints);
result.hasErrors(); // true - "email is required"
// ❌ FAILS - empty string is treated as missing
var result = validate(target={ email: "" }, constraints: constraints);
result.hasErrors(); // true - "email is required"
// ✅ PASSES - value provided
var result = validate(target={ email: "[email protected]" }, constraints: constraints);
result.hasErrors(); // falsevar constraints = {
email = { required = true, type = "email" }
};
// ❌ FAILS - null is treated as missing
var result = validate(target={ email: javacast("null", "") }, constraints=constraints);
result.hasErrors(); // true - "email is required"
// ❌ FAILS - empty string is treated as missing
var result = validate(target={ email: "" }, constraints=constraints);
result.hasErrors(); // true - "email is required"
// ✅ PASSES - value provided
var result = validate(target={ email: "[email protected]" }, constraints=constraints);
result.hasErrors(); // falseOptional Fields with Type Checking
Null Filtering in validateOrFail()
Nested Structures with Nulls
Array Items with Nulls
Best Practices
1. Null Check Before Processing
2. Use Required for Important Fields
3. Use defaultValue for Optional Fields
4. Clean Results with validateOrFail
Empty vs Null
See Also
Last updated
Was this helpful?