bsh-generator / ts-validation

library to simplify the data validation in ts projects
https://bshg-libs.vercel.app/ts-validation
MIT License
4 stars 1 forks source link

Simplify Access to Validation Status and Messages #2

Closed bsh-generator closed 2 months ago

bsh-generator commented 2 months ago

Feature Request

Summary:

The current method of accessing validation statuses and messages involves deep property access (e.g., validator.items.<item>.<message|valid|validate>). To enhance usability and improve code readability, a simpler and more intuitive way to access validation results should be provided.

Use Case:

In many scenarios, developers need to quickly and easily check the validation status of various fields or elements. The existing method, which requires navigating through multiple properties (validator.items.<item>.<message|valid|validate>), can be cumbersome, especially when dealing with complex forms or data structures.

A simplified API for accessing validation status and messages would streamline development, reduce errors, and improve overall developer experience.

Expected Behavior:

  1. Simplified Access:

    • Introduce a new method or property that provides direct access to validation statuses and messages.
    • Example: Instead of using validator.items.username.valid, allow access through a simpler syntax like validator.getStatus('username').valid.
  2. Flexible Retrieval:

    • Allow retrieval of validation results for multiple fields at once.
    • Example: validator.getStatuses(['username', 'email']) could return an object with validation statuses for both fields.
  3. Consistent and Intuitive API:

    • Ensure that the new access pattern is consistent with the existing API and easy to understand.
  4. Backward Compatibility:

    • Maintain backward compatibility to ensure that existing codebases are not broken by this change.

Possible Implementation:

hamza-bousalih commented 2 months ago

three functions are added to Validator to simplify the access to items status:

validator.validateItem('filed-name') // replacing : validator.items?.email?.validate()
validator.itemMessage('filed-name') // replacing : validator.items?.email?.message
validator.isItemValid('filed-name') // replacing : validator.items?.email?.valid