Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple byte tests #31

Merged
merged 5 commits into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const fullFormats: DefinedFormats = {
"relative-json-pointer": /^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)$/,
// the following formats are used by the openapi specification: https://spec.openapis.org/oas/v3.0.0#data-types
// byte: https://github.com/miguelmota/is-base64
byte: /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm,
byte,
// signed 32 bit integer
int32: {type: "number", validate: validateInt32},
// signed 64 bit integer
Expand Down Expand Up @@ -197,6 +197,13 @@ function uri(str: string): boolean {
return NOT_URI_FRAGMENT.test(str) && URI.test(str)
}

const BYTE = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm

function byte(str: string): boolean {
BYTE.lastIndex = 0
return BYTE.test(str)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think rather than defining regex locally it would be better to reset the index on regex after the call and have regex itself defined once in the scope of this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep the code concise I propose to do it like this:

const BYTE = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm

function byte(str: string): boolean {
  BYTE.lastIndex = 0
  return BYTE.test(str)
}

Ok ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - thank you


const MIN_INT32 = -(2 ** 31)
const MAX_INT32 = 2 ** 31 - 1

Expand Down
5 changes: 5 additions & 0 deletions tests/extras/format.json
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,11 @@
"data": "VGhpcyBpcyBhIGJhc2U2NCBtdWx0aWxpbmUgc3RyaW5nIHRoYXQgc3BhbnMgbW9yZSB0aGFuIDc2\nIGNoYXJhY3RlcnMgdG8gdGVzdCBtdWx0aWxpbmUgY2FwYWJpbGl0aWVzCg==",
"valid": true
},
{
"description": "second round multiline base64",
"data": "VGhpcyBpcyBhIGJhc2U2NCBtdWx0aWxpbmUgc3RyaW5nIHRoYXQgc3BhbnMgbW9yZSB0aGFuIDc2\nIGNoYXJhY3RlcnMgdG8gdGVzdCBtdWx0aWxpbmUgY2FwYWJpbGl0aWVzCg==",
"valid": true
},
{
"description": "Invalid base64",
"data": "aGVsbG8gd29ybG=",
Expand Down
2 changes: 1 addition & 1 deletion tests/extras/formatMinimum.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
"valid": false
},
{
"description": "same date, time before the minimum time is stillinvalid",
"description": "same date, time before the minimum time is still invalid",
"data": "2015-08-17T10:33:55.000Z",
"valid": false
},
Expand Down
12 changes: 6 additions & 6 deletions tests/extras/issues/1061_alternative_time_offsets.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"schema": {"format": "date-time"},
"tests": [
{
"description": "valid positiive two digit",
"description": "valid positive two digit",
"data": "2016-01-31T02:31:17+01",
"valid": true
},
Expand All @@ -14,7 +14,7 @@
"valid": true
},
{
"description": "valid positiive four digit no colon",
"description": "valid positive four digit no colon",
"data": "2016-01-31T02:31:17+0130",
"valid": true
},
Expand All @@ -24,7 +24,7 @@
"valid": true
},
{
"description": "invalid positiive three digit no colon",
"description": "invalid positive three digit no colon",
"data": "2016-01-31T02:31:17+013",
"valid": false
},
Expand All @@ -40,7 +40,7 @@
"schema": {"format": "time"},
"tests": [
{
"description": "valid positiive two digit",
"description": "valid positive two digit",
"data": "02:31:17+01",
"valid": true
},
Expand All @@ -50,7 +50,7 @@
"valid": true
},
{
"description": "valid positiive four digit no colon",
"description": "valid positive four digit no colon",
"data": "02:31:17+0130",
"valid": true
},
Expand All @@ -60,7 +60,7 @@
"valid": true
},
{
"description": "invalid positiive three digit no colon",
"description": "invalid positive three digit no colon",
"data": "02:31:17+013",
"valid": false
},
Expand Down