Skip to content

Validation Failure Hooks #8

@ghost

Description

Feature Title: Validation Failure Hooks

Introduce hooks that trigger custom logic when validation fails, such as logging errors, sending notifications, or executing fallback logic.

Detailed Explanation:

1. Overview:

  • Purpose: The "Validation Failure Hooks" feature allows developers to define custom actions that should occur whenever a validation check fails. This could be useful for scenarios where additional steps need to be taken beyond simply returning an error message, such as logging, alerting, or triggering alternative workflows.
  • Use Cases:
    • Logging validation failures for debugging or audit purposes.
    • Sending notifications (e.g., emails, Slack messages) to alert a team when specific validation rules fail.
    • Executing fallback logic, such as attempting an alternative data processing method when validation fails.

2. How It Works:

  • Hook Definition: Developers can define hooks within their validation schema or rule set. These hooks are functions or callbacks that execute when a particular validation fails.
  • Hook Parameters: When validation fails, the hook is triggered with relevant context parameters, such as the field name, the invalid value, the validation rule that failed, and any custom messages or metadata.
  • Global and Rule-Specific Hooks: Hooks can be defined globally (affecting all validation failures) or on a per-rule basis (only triggering for specific validation failures).
  • Synchronous and Asynchronous Support: The system supports both synchronous and asynchronous hooks, allowing for complex logic such as API calls or database writes.

3. Example Use Cases:

  • on field failed:

    v.validator<Type>({
        id: "id",
        items: {
            field: v.string().required().onFail((field, value, rule) => {/*some fallback*/} )
        }
    })
  • on Validator failed:

    v.validator<Type>({
        id: "id",
        items: {/* ... */}
        nested: {/* ... */}
    })
    .onFail((/* some params? */) => {/*some fallback*/} )
  • Global fallback:

    v.configure({ // global configuration
        onFail: {
            validator: (/* some params? */) => {/*some fallback*/}, // this will be executed for all validator
            field: (field, value, rule) => {/*some fallback*/} // this will executed when any field failed
        }
    })

4. Benefits:

  • Centralized Error Handling: Developers can handle all validation failures in a consistent and centralized manner.
  • Improved Debugging: Logging hooks provide detailed information that can help with diagnosing and fixing validation issues.
  • Enhanced User Experience: By executing fallback logic or notifying support teams, the system can handle validation failures more gracefully, potentially improving the end-user experience.

5. Considerations:

  • Performance: Running complex logic within hooks (especially asynchronous operations) may impact performance. Developers should ensure that any logic within hooks is optimized and necessary.
  • Security: Care must be taken to avoid exposing sensitive information in logs or notifications triggered by validation failures.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions