Validation Examples

Examples of handling FortiGate-side validation errors and doing optional pre-flight checks. HFortix does not validate payloads automatically — the FortiGate enforces constraints and the client raises exceptions from hfortix_core.exceptions.

Handling Server-Side Validation Errors

from hfortix_fortios import FortiOS
from hfortix_core.exceptions import APIError, BadRequestError

fgt = FortiOS(host='192.168.1.99', token='your-token')

try:
    fgt.api.cmdb.firewall.address.post(
        name='a' * 100,  # Exceeds FortiOS name length limit
        subnet='192.168.1.1 255.255.255.255'
    )
except BadRequestError as e:
    print(f"FortiGate rejected the request: {e}")
    # Retry with valid data
    fgt.api.cmdb.firewall.address.post(
        name='valid-name',
        subnet='192.168.1.1 255.255.255.255'
    )
except APIError as e:
    print(f"Other API error: {e}")

Invalid Enum Values

Invalid option values are rejected by the device and raise InvalidValueError (FortiOS error -651) or BadRequestError:

from hfortix_core.exceptions import BadRequestError, InvalidValueError

try:
    fgt.api.cmdb.firewall.policy.post(
        name='test',
        action='invalid-action',  # Not a valid action
        srcintf=[{"name": "port1"}],
        dstintf=[{"name": "port2"}],
        srcaddr=[{"name": "all"}],
        dstaddr=[{"name": "all"}],
        service=[{"name": "ALL"}],
        schedule='always',
    )
except (InvalidValueError, BadRequestError) as e:
    print(f"Invalid value: {e}")

Tip: the shipped .pyi type stubs declare Literal[...] enums for these parameters, so your IDE or type checker flags invalid enum values before the code ever runs.

Optional Pre-Flight Checks

Use the endpoint metadata helpers to validate explicitly before sending:

addr = fgt.api.cmdb.firewall.address

is_valid, error = addr.validate_field('type', 'not-a-real-type')
if not is_valid:
    print(error)
    # Invalid value for 'type': 'not-a-real-type'
    #   → Valid options: 'ipmask', 'iprange', 'fqdn', ...

See Also