Skip to content

Conversation

@Anuj-Chhajed
Copy link

Fixes #15845

This PR adds enumValues support to SchemaBuffer so that buffer enum behavior becomes consistent with SchemaString and SchemaNumber.

Background

SchemaBuffer previously allowed enum validation but did not expose an enumValues array or fully support the .enum() API pattern used by other schema types. This created an inconsistency where buffer enums could validate values, but the allowed values could not be introspected or extended using additional .enum() calls.

What this PR changes

  • Initializes this.enumValues = [] in the SchemaBuffer constructor.
  • Implements SchemaBuffer.prototype.enum() following the same structure as SchemaNumber.prototype.enum():
    • Removes the previous enum validator if one exists.
    • Supports object syntax: { values, message }.
    • Casts enum inputs using this.cast(...) before storing.
    • Pushes casted values into this.enumValues.
    • Creates a new enum validator that compares buffers using Buffer.compare.
    • Stores the validator and associated metadata in this.validators.
    • Returns this for method chaining.
  • Ensures message fallback uses a Buffer-specific default string format.
  • Does not alter existing casting or validation logic outside the enum pathway.

Tests

A new test has been added to test/schema.validation.test.js that mirrors the structure of the existing string and number enum tests and verifies:

  • enumValues is correctly populated on schema paths.
  • Additive .enum() behavior works (enum('c') appends).
  • Object syntax ({ values, message }) is supported.
  • Valid buffer values pass validation; invalid ones fail.
  • null and undefined behave consistently with other SchemaTypes.

Why this change is needed

This is an API consistency improvement. String and Number schema types already support:

  • enumValues exposure
  • additive .enum()
  • object syntax
  • getter behavior when calling .enum() with no args

This PR brings SchemaBuffer up to the same standard without introducing breaking changes.

Notes

  • All changes are self-contained within schema/buffer.js.
  • No behavior outside enum handling is modified.
  • No breaking changes.

@Anuj-Chhajed
Copy link
Author

@vkarpov15 pls review my pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SchemaBuffer does not expose enumValues like String and Number enums

1 participant