Skip to main content

addAddressValidationRule

Add a custom validation rule for customer address validation.

Import

import { addAddressValidationRule } from "@evershop/evershop/customer/services";

Syntax

addAddressValidationRule(rule: Validator<Address>): void

Parameters

rule

Type: Validator<Address>

{
id: string; // Unique rule identifier
func: (address: Address) => boolean; // Validation function
errorMessage: string; // Error message when invalid
}

Return Value

Returns void.

Examples

Phone Number Validation

import { addAddressValidationRule } from "@evershop/evershop/customer/services";

addAddressValidationRule({
id: 'telephoneRequired',
func: (address) => {
return !!address.telephone && address.telephone.trim() !== '';
},
errorMessage: 'Phone number is required'
});

In Bootstrap File

// bootstrap.js
import { addAddressValidationRule } from "@evershop/evershop/customer/services";

// Add custom rules during app initialization
addAddressValidationRule({
id: 'phoneFormat',
func: (address) => {
if (!address.telephone) return true;
return /^\+?[\d\s-()]+$/.test(address.telephone);
},
errorMessage: 'Invalid phone number format'
});

Notes

  • Rules are global (affect all address validation)
  • Add rules during app initialization using bootstrap file

See Also