Skip to content

Commit 7edc591

Browse files
committed
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
1 parent 06afeb5 commit 7edc591

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

lib/types/number.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ module.exports = Any.extend({
269269
}
270270

271271
return helpers.error('number.port');
272+
},
273+
jsonSchema(rule, res) {
274+
275+
res.type = 'integer';
276+
res.minimum = 0;
277+
res.maximum = 65535;
278+
return res;
272279
}
273280
},
274281

@@ -318,7 +325,13 @@ module.exports = Any.extend({
318325
},
319326
jsonSchema(rule, res) {
320327

321-
res['x-constraint'] = { ...res['x-constraint'], sign: rule.args.sign };
328+
if (rule.args.sign === 'positive') {
329+
res.exclusiveMinimum = 0;
330+
}
331+
else {
332+
res.exclusiveMaximum = 0;
333+
}
334+
322335
return res;
323336
}
324337
},

test/json-schema.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,39 +1070,38 @@ describe('jsonSchema', () => {
10701070

10711071
Helper.validateJsonSchema(Joi.number().positive(), {
10721072
type: 'number',
1073-
'x-constraint': {
1074-
sign: 'positive'
1075-
}
1073+
exclusiveMinimum: 0
10761074
});
10771075

10781076
Helper.validateJsonSchema(Joi.number().positive().only(), {
10791077
type: 'number',
1080-
'x-constraint': {
1081-
sign: 'positive'
1082-
}
1078+
exclusiveMinimum: 0
10831079
});
10841080

10851081
Helper.validateJsonSchema(Joi.number().negative(), {
10861082
type: 'number',
1087-
'x-constraint': {
1088-
sign: 'negative'
1089-
}
1083+
exclusiveMaximum: 0
10901084
});
10911085

10921086
Helper.validateJsonSchema(Joi.number().positive().multiple(3), {
10931087
type: 'number',
10941088
multipleOf: 3,
1095-
'x-constraint': {
1096-
sign: 'positive'
1097-
}
1089+
exclusiveMinimum: 0
10981090
});
10991091

11001092
Helper.validateJsonSchema(Joi.number().multiple(3).positive(), {
11011093
type: 'number',
11021094
multipleOf: 3,
1103-
'x-constraint': {
1104-
sign: 'positive'
1105-
}
1095+
exclusiveMinimum: 0
1096+
});
1097+
});
1098+
1099+
it('represents number.port() constraints', () => {
1100+
1101+
Helper.validateJsonSchema(Joi.number().port(), {
1102+
type: 'integer',
1103+
minimum: 0,
1104+
maximum: 65535
11061105
});
11071106
});
11081107

0 commit comments

Comments
 (0)