From 7edc59186379f62c0f4c6336dc5f559647340cc2 Mon Sep 17 00:00:00 2001 From: Mahmoud Hamdi Date: Mon, 30 Mar 2026 02:11:32 +0200 Subject: [PATCH] fix: improve JSON Schema conversion for number.port() and number.sign() - 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 --- lib/types/number.js | 15 ++++++++++++++- test/json-schema.js | 29 ++++++++++++++--------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/lib/types/number.js b/lib/types/number.js index b79c1299..ae07eae8 100755 --- a/lib/types/number.js +++ b/lib/types/number.js @@ -269,6 +269,13 @@ module.exports = Any.extend({ } return helpers.error('number.port'); + }, + jsonSchema(rule, res) { + + res.type = 'integer'; + res.minimum = 0; + res.maximum = 65535; + return res; } }, @@ -318,7 +325,13 @@ module.exports = Any.extend({ }, jsonSchema(rule, res) { - res['x-constraint'] = { ...res['x-constraint'], sign: rule.args.sign }; + if (rule.args.sign === 'positive') { + res.exclusiveMinimum = 0; + } + else { + res.exclusiveMaximum = 0; + } + return res; } }, diff --git a/test/json-schema.js b/test/json-schema.js index d84ffab9..b09db475 100644 --- a/test/json-schema.js +++ b/test/json-schema.js @@ -1070,39 +1070,38 @@ describe('jsonSchema', () => { Helper.validateJsonSchema(Joi.number().positive(), { type: 'number', - 'x-constraint': { - sign: 'positive' - } + exclusiveMinimum: 0 }); Helper.validateJsonSchema(Joi.number().positive().only(), { type: 'number', - 'x-constraint': { - sign: 'positive' - } + exclusiveMinimum: 0 }); Helper.validateJsonSchema(Joi.number().negative(), { type: 'number', - 'x-constraint': { - sign: 'negative' - } + exclusiveMaximum: 0 }); Helper.validateJsonSchema(Joi.number().positive().multiple(3), { type: 'number', multipleOf: 3, - 'x-constraint': { - sign: 'positive' - } + exclusiveMinimum: 0 }); Helper.validateJsonSchema(Joi.number().multiple(3).positive(), { type: 'number', multipleOf: 3, - 'x-constraint': { - sign: 'positive' - } + exclusiveMinimum: 0 + }); + }); + + it('represents number.port() constraints', () => { + + Helper.validateJsonSchema(Joi.number().port(), { + type: 'integer', + minimum: 0, + maximum: 65535 }); });