Skip to content

fix: improve JSON Schema conversion for number.port() and number.sign()#3107

Merged
Marsup merged 1 commit into
hapijs:masterfrom
mahmoodhamdi:fix/json-schema-number-rules
Mar 30, 2026
Merged

fix: improve JSON Schema conversion for number.port() and number.sign()#3107
Marsup merged 1 commit into
hapijs:masterfrom
mahmoodhamdi:fix/json-schema-number-rules

Conversation

@mahmoodhamdi
Copy link
Copy Markdown
Contributor

While using Joi schemas with the Standard Schema JSON Schema output for API documentation, I noticed two gaps in the number type's JSON Schema conversion:

1. number.port() — missing JSON Schema output

Joi.number().port() validates that a value is an integer between 0 and 65535, but it produced no JSON Schema constraints — just {"type": "number"}.

Before: {"type": "number"}
After: {"type": "integer", "minimum": 0, "maximum": 65535}

2. number.sign() — non-standard x-constraint

Joi.number().positive() / .negative() used a non-standard x-constraint property instead of the standard JSON Schema keywords.

Before: {"type": "number", "x-constraint": {"sign": "positive"}}
After: {"type": "number", "exclusiveMinimum": 0}

Before: {"type": "number", "x-constraint": {"sign": "negative"}}
After: {"type": "number", "exclusiveMaximum": 0}

exclusiveMinimum and exclusiveMaximum are standard JSON Schema Draft 2020-12 keywords that correctly express the same constraint.

Changes

  • lib/types/number.js: Added jsonSchema handler for port rule, updated sign rule to use standard keywords
  • test/json-schema.js: Added test for port() conversion, updated sign test expectations

All tests pass: 1797 tests, 100% coverage, lint clean, types clean.

- Add jsonSchema conversion for number.port() rule, producing
  type: integer with minimum: 0 and maximum: 65535
- Replace non-standard x-constraint for number.sign() with proper
  JSON Schema keywords: exclusiveMinimum: 0 for positive,
  exclusiveMaximum: 0 for negative
@Marsup Marsup self-assigned this Mar 30, 2026
@Marsup Marsup added the bug Bug or defect label Mar 30, 2026
@Marsup Marsup added this to the 18.1.2 milestone Mar 30, 2026
@Marsup
Copy link
Copy Markdown
Collaborator

Marsup commented Mar 30, 2026

Good catch, thanks!

@Marsup Marsup merged commit d98c802 into hapijs:master Mar 30, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Bug or defect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants