Skip to content

Commit ec288c4

Browse files
authored
Merge pull request #22 from seriousme/master
Added openApi spec formats
2 parents 0fd9f3f + c2399d0 commit ec288c4

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ The package defines these formats:
4141
- _uuid_: Universally Unique IDentifier according to [RFC4122](http://tools.ietf.org/html/rfc4122).
4242
- _json-pointer_: JSON-pointer according to [RFC6901](https://tools.ietf.org/html/rfc6901).
4343
- _relative-json-pointer_: relative JSON-pointer according to [this draft](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00).
44+
- _byte_: base64 encoded data according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
45+
- _int32_: signed 32 bits integer according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
46+
- _int64_: signed 64 bits according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
47+
- _float_: float according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
48+
- _double_: double according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
49+
- _password_: password string according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
50+
- _binary_: binary string according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
4451

4552
See regular expressions used for format validation and the sources that were used in [formats.ts](https://github.com/ajv-validator/ajv-formats/blob/master/src/formats.ts).
4653

src/formats.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ export type FormatName =
2121
| "json-pointer"
2222
| "json-pointer-uri-fragment"
2323
| "relative-json-pointer"
24+
| "byte"
25+
| "int32"
26+
| "int64"
27+
| "float"
28+
| "double"
29+
| "password"
30+
| "binary"
2431

2532
export type DefinedFormats = {
2633
[key in FormatName]: Format
@@ -62,6 +69,21 @@ export const fullFormats: DefinedFormats = {
6269
"json-pointer-uri-fragment": /^#(?:\/(?:[a-z0-9_\-.!$&'()*+,;:=@]|%[0-9a-f]{2}|~0|~1)*)*$/i,
6370
// relative JSON-pointer: http://tools.ietf.org/html/draft-luff-relative-json-pointer-00
6471
"relative-json-pointer": /^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)$/,
72+
// the following formats are used by the openapi specification: https://spec.openapis.org/oas/v3.0.0#data-types
73+
// byte: https://github.com/miguelmota/is-base64
74+
byte: /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm,
75+
// signed 32 bit integer
76+
int32: {type: "number", validate: validateInt32},
77+
// signed 64 bit integer
78+
int64: {type: "number", validate: validateInt64},
79+
// C-type float
80+
float: {type: "number", validate: validateNumber},
81+
// C-type double
82+
double: {type: "number", validate: validateNumber},
83+
// hint to the UI to hide input strings
84+
password: true,
85+
// unchecked string payload
86+
binary: true,
6587
}
6688

6789
export const fastFormats: DefinedFormats = {
@@ -169,6 +191,22 @@ function uri(str: string): boolean {
169191
return NOT_URI_FRAGMENT.test(str) && URI.test(str)
170192
}
171193

194+
const MIN_INT32 = -(2 ** 31)
195+
const MAX_INT32 = 2 ** 31 - 1
196+
197+
function validateInt32(value: number): boolean {
198+
return Number.isInteger(value) && value <= MAX_INT32 && value >= MIN_INT32
199+
}
200+
201+
function validateInt64(value: number): boolean {
202+
// JSON and javascript max Int is 2**53, so any int that passes isInteger is valid for Int64
203+
return Number.isInteger(value)
204+
}
205+
206+
function validateNumber(): boolean {
207+
return true
208+
}
209+
172210
const Z_ANCHOR = /[^\\]\\Z/
173211
function regex(str: string): boolean {
174212
if (Z_ANCHOR.test(str)) return false

tests/extras/format.json

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,5 +728,142 @@
728728
"valid": true
729729
}
730730
]
731+
},
732+
{
733+
"description": "validation of Byte",
734+
"schema": {"format": "byte"},
735+
"tests": [
736+
{
737+
"description": "'hello world' encoded in base64",
738+
"data": "aGVsbG8gd29ybGQ=",
739+
"valid": true
740+
},
741+
{
742+
"description": "multiline base64",
743+
"data": "VGhpcyBpcyBhIGJhc2U2NCBtdWx0aWxpbmUgc3RyaW5nIHRoYXQgc3BhbnMgbW9yZSB0aGFuIDc2\nIGNoYXJhY3RlcnMgdG8gdGVzdCBtdWx0aWxpbmUgY2FwYWJpbGl0aWVzCg==",
744+
"valid": true
745+
},
746+
{
747+
"description": "Invalid base64",
748+
"data": "aGVsbG8gd29ybG=",
749+
"valid": false
750+
}
751+
]
752+
},
753+
{
754+
"description": "validation of int32",
755+
"schema": {"type": "number", "format": "int32"},
756+
"tests": [
757+
{
758+
"description": "256 is ok",
759+
"data": 256,
760+
"valid": true
761+
},
762+
{
763+
"description": "256.1 fails",
764+
"data": 256.1,
765+
"valid": false
766+
},
767+
{
768+
"description": "-(2**31) is ok",
769+
"data": -2147483648,
770+
"valid": true
771+
},
772+
{
773+
"description": "-(2**31) - 1 fails",
774+
"data": -2147483649,
775+
"valid": false
776+
},
777+
{
778+
"description": "(2**31)-1 is ok",
779+
"data": 2147483647,
780+
"valid": true
781+
},
782+
{
783+
"description": "2**31 fails",
784+
"data": 2147483648,
785+
"valid": false
786+
},
787+
{
788+
"description": "non-numeric fails",
789+
"data": "x",
790+
"valid": false
791+
}
792+
]
793+
},
794+
{
795+
"description": "validation of int64",
796+
"schema": {"type": "number", "format": "int64"},
797+
"tests": [
798+
{
799+
"description": "256 is ok",
800+
"data": 256,
801+
"valid": true
802+
},
803+
{
804+
"description": "float fails",
805+
"data": 256.1,
806+
"valid": false
807+
},
808+
{
809+
"description": "non-numeric fails",
810+
"data": "x",
811+
"valid": false
812+
}
813+
]
814+
},
815+
{
816+
"description": "validation of float",
817+
"schema": {"type": "number", "format": "float"},
818+
"tests": [
819+
{
820+
"description": "256.1 is ok",
821+
"data": 256.1,
822+
"valid": true
823+
},
824+
{
825+
"description": "non-numeric fails",
826+
"data": "x",
827+
"valid": false
828+
}
829+
]
830+
},
831+
{
832+
"description": "validation of double",
833+
"schema": {"type": "number", "format": "double"},
834+
"tests": [
835+
{
836+
"description": "256.1 is ok",
837+
"data": 256.1,
838+
"valid": true
839+
},
840+
{
841+
"description": "non-numeric fails",
842+
"data": "x",
843+
"valid": false
844+
}
845+
]
846+
},
847+
{
848+
"description": "validation of password",
849+
"schema": {"format": "password"},
850+
"tests": [
851+
{
852+
"description": "'password string' is ok",
853+
"data": "password string",
854+
"valid": true
855+
}
856+
]
857+
},
858+
{
859+
"description": "validation of binary",
860+
"schema": {"format": "binary"},
861+
"tests": [
862+
{
863+
"description": "'binary string' is ok",
864+
"data": "binary string",
865+
"valid": true
866+
}
867+
]
731868
}
732869
]

0 commit comments

Comments
 (0)