Repository: glennjones/hapi-swagger
Pull request #742 (2022-04-07 08:05:58)

feat: add support for format field in integer properties
I have prepared this pull request to address issue #740 that I opened some days ago.

I have added 2 new tests, and the second one tests the new functionality of this PR. 
The first one isn't directly related to the new functionality, but it is a case that it seems to me that it was missing. If you feel that is redundant, let me know to remove it.

Please have a look and send my any comments you may have

robmcguinness: LGTM. Missing docs and we should be good to go.
robmcguinness: @codingstill apologies but we have merge conflict now. Once that is resolved I can release.

Patch:
diff --git a/lib/properties.js b/lib/properties.js
index 739bc7f..a0e2b9b 100644
--- a/lib/properties.js
+++ b/lib/properties.js
@@ -373,6 +373,14 @@ internals.properties.prototype.parseNumber = function (property, joiObj) {
   property.maximum = internals.getArgByName(describe.rules, 'max');
   if (internals.hasPropertyByName(describe.rules, 'integer')) {
     property.type = 'integer';
+    if (Array.isArray(describe.metas)) {
+      const meta = describe.metas.find((meta) => {
+        return typeof meta.format === 'string';
+      });
+      if (meta) {
+        property.format = meta.format;
+      }
+    }
   }
 
   // add extended properties not part of openAPI spec
diff --git a/usageguide.md b/usageguide.md
index f9c4ec8..f1eb75c 100644
--- a/usageguide.md
+++ b/usageguide.md
@@ -16,6 +16,7 @@
 -   [File upload](#file-upload)
 -   [Prevent JOI properties from being included in the Swagger schema](#prevent-joi-properties-from-being-included-in-the-swagger-schema)
 -   [Name JOI properties for XML formats](#name-joi-properties-for-xml-formats)
+-   [Set format field to JOI integer properties](#set-format-field-to-joi-integer-properties)
 -   [Headers and .unknown()](#headers-and-unknown)
 -   [Additional Hapi data using x-\*](#additional-hapi-data-using-x-)
 -   [JSON without UI](#json-without-ui)
@@ -514,6 +515,12 @@ Joi.array()
 
 See this link for more info: [OpenAPI 2.0 XML object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#xmlObject)
 
+## Set format field to JOI integer properties
+
+You can pass the format property with value `int32` or `int64` on JOI integer properties, as defined in the [guide of OpenAPI 3.0](https://swagger.io/docs/specification/data-models/data-types/#numbers).
+
+`.number().integer().meta({ format: 'int64' })`
+
 ## Default values and examples
 
 You can add both default values and examples to your JOI objects which are displayed within the Swagger interface.


Test patch:
diff --git a/test/unit/property-test.js b/test/unit/property-test.js
index 164ec0f..f0bf77d 100644
--- a/test/unit/property-test.js
+++ b/test/unit/property-test.js
@@ -53,6 +53,13 @@ lab.experiment('property - ', () => {
     expect(propertiesNoAlt.parseProperty('x', Joi.number(), null, 'body', true, false)).to.equal({
       type: 'number'
     });
+    expect(propertiesNoAlt.parseProperty('x', Joi.number().integer(), null, 'body', true, false)).to.equal({
+      type: 'integer'
+    });
+    expect(propertiesNoAlt.parseProperty('x', Joi.number().integer().meta({ format: 'int64' }), null, 'body', true, false)).to.equal({
+      type: 'integer',
+      format: 'int64'
+    });
     expect(propertiesNoAlt.parseProperty('x', Joi.boolean(), null, 'body', true, false)).to.equal({
       type: 'boolean'
     });