Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
renovate
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
Renovate Bot
renovate
Commits
eb5181e2
Unverified
Commit
eb5181e2
authored
2 years ago
by
Sergei Zharinov
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
refactor: Make invalid schema reporting optional (#18024)
parent
d0027cf2
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/modules/platform/github/common.ts
+2
-2
2 additions, 2 deletions
lib/modules/platform/github/common.ts
lib/util/schema.spec.ts
+24
-16
24 additions, 16 deletions
lib/util/schema.spec.ts
lib/util/schema.ts
+7
-3
7 additions, 3 deletions
lib/util/schema.ts
with
33 additions
and
21 deletions
lib/modules/platform/github/common.ts
+
2
−
2
View file @
eb5181e2
import
is
from
'
@sindresorhus/is
'
;
import
{
PrState
}
from
'
../../../types
'
;
import
{
checkS
chema
}
from
'
../../../util/schema
'
;
import
*
as
s
chema
from
'
../../../util/schema
'
;
import
{
getPrBodyStruct
}
from
'
../pr-body
'
;
import
*
as
platformSchemas
from
'
../schemas
'
;
import
type
{
GhPr
,
GhRestPr
}
from
'
./types
'
;
...
...
@@ -52,6 +52,6 @@ export function coerceRestPr(pr: GhRestPr): GhPr {
result
.
closedAt
=
pr
.
closed_at
;
}
che
ckSchema
(
platformSchemas
.
Pr
,
result
);
s
che
ma
.
match
(
platformSchemas
.
Pr
,
result
,
true
);
return
result
;
}
This diff is collapsed.
Click to expand it.
lib/util/schema.spec.ts
+
24
−
16
View file @
eb5181e2
import
{
z
}
from
'
zod
'
;
import
{
logger
}
from
'
../../test/util
'
;
import
*
as
memCache
from
'
./cache/memory
'
;
import
{
checkSchema
,
reportErrors
}
from
'
./schema
'
;
import
*
as
schema
from
'
./schema
'
;
describe
(
'
util/schema
'
,
()
=>
{
beforeEach
(()
=>
{
...
...
@@ -10,33 +10,41 @@ describe('util/schema', () => {
});
it
(
'
validates data
'
,
()
=>
{
const
s
chema
=
z
.
object
({
foo
:
z
.
string
()
});
const
testS
chema
=
z
.
object
({
foo
:
z
.
string
()
});
const
validData
=
{
foo
:
'
bar
'
};
const
res
=
che
ckSchema
(
s
chema
,
validData
);
const
res
=
s
che
ma
.
match
(
testS
chema
,
validData
);
expect
(
res
).
toBeTrue
();
});
it
(
'
returns false for invalid data
'
,
()
=>
{
const
testSchema
=
z
.
object
({
foo
:
z
.
string
()
});
const
invalidData
=
{
foo
:
123
};
reportErrors
();
expect
(
logger
.
logger
.
warn
).
not
.
toHaveBeenCalledOnce
();
const
res
=
schema
.
match
(
testSchema
,
invalidData
);
expect
(
res
).
toBeFalse
();
schema
.
reportErrors
();
expect
(
logger
.
logger
.
warn
).
not
.
toHaveBeenCalled
();
});
it
(
'
reports nothing if there are no any reports
'
,
()
=>
{
reportErrors
();
schema
.
reportErrors
();
expect
(
logger
.
logger
.
warn
).
not
.
toHaveBeenCalled
();
});
it
(
'
reports same warning on
c
e
'
,
()
=>
{
const
s
chema
=
z
.
object
(
it
(
'
reports same warning on
e tim
e
'
,
()
=>
{
const
testS
chema
=
z
.
object
(
{
foo
:
z
.
string
()
},
{
description
:
'
Some test schema
'
}
);
const
invalidData
=
{
foo
:
42
};
che
ckSchema
(
s
chema
,
invalidData
);
che
ckSchema
(
s
chema
,
invalidData
);
che
ckSchema
(
s
chema
,
invalidData
);
che
ckSchema
(
s
chema
,
invalidData
);
reportErrors
();
s
che
ma
.
match
(
testS
chema
,
invalidData
,
true
);
s
che
ma
.
match
(
testS
chema
,
invalidData
,
true
);
s
che
ma
.
match
(
testS
chema
,
invalidData
,
true
);
s
che
ma
.
match
(
testS
chema
,
invalidData
,
true
);
schema
.
reportErrors
();
expect
(
logger
.
logger
.
warn
).
toHaveBeenCalledOnce
();
expect
(
logger
.
logger
.
warn
.
mock
.
calls
[
0
]).
toMatchObject
([
...
...
@@ -46,11 +54,11 @@ describe('util/schema', () => {
});
it
(
'
reports unspecified schema
'
,
()
=>
{
const
s
chema
=
z
.
object
({
foo
:
z
.
string
()
});
const
testS
chema
=
z
.
object
({
foo
:
z
.
string
()
});
const
invalidData
=
{
foo
:
42
};
che
ckSchema
(
s
chema
,
invalidData
);
reportErrors
();
s
che
ma
.
match
(
testS
chema
,
invalidData
,
true
);
schema
.
reportErrors
();
expect
(
logger
.
logger
.
warn
).
toHaveBeenCalledOnce
();
expect
(
logger
.
logger
.
warn
.
mock
.
calls
[
0
]).
toMatchObject
([
...
...
This diff is collapsed.
Click to expand it.
lib/util/schema.ts
+
7
−
3
View file @
eb5181e2
...
...
@@ -44,14 +44,18 @@ export function reportErrors(): void {
memCache
.
set
(
'
schema-errors
'
,
null
);
}
export
function
checkSchema
<
T
extends
z
.
ZodSchema
>
(
export
function
match
<
T
extends
z
.
ZodSchema
>
(
schema
:
T
,
input
:
unknown
input
:
unknown
,
report
=
false
):
input
is
z
.
infer
<
T
>
{
const
res
=
schema
.
safeParse
(
input
);
const
{
success
}
=
res
;
if
(
!
success
)
{
collectError
(
schema
,
res
.
error
);
if
(
report
)
{
collectError
(
schema
,
res
.
error
);
}
return
false
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment