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
48439d28
Unverified
Commit
48439d28
authored
1 year ago
by
Sebastian Poxhofer
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
chore(util/yaml): allow to provide zod schemas to YAML parser (#26647)
parent
717ba628
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/util/yaml.spec.ts
+119
-0
119 additions, 0 deletions
lib/util/yaml.spec.ts
lib/util/yaml.ts
+34
-10
34 additions, 10 deletions
lib/util/yaml.ts
with
153 additions
and
10 deletions
lib/util/yaml.spec.ts
+
119
−
0
View file @
48439d28
import
{
codeBlock
}
from
'
common-tags
'
;
import
{
codeBlock
}
from
'
common-tags
'
;
import
{
z
}
from
'
zod
'
;
import
{
parseSingleYaml
,
parseYaml
}
from
'
./yaml
'
;
import
{
parseSingleYaml
,
parseYaml
}
from
'
./yaml
'
;
describe
(
'
util/yaml
'
,
()
=>
{
describe
(
'
util/yaml
'
,
()
=>
{
...
@@ -22,6 +23,31 @@ describe('util/yaml', () => {
...
@@ -22,6 +23,31 @@ describe('util/yaml', () => {
]);
]);
});
});
it
(
'
should parse content with single document with schema
'
,
()
=>
{
expect
(
parseYaml
(
codeBlock
`
myObject:
aString: value
`
,
null
,
{
customSchema
:
z
.
object
({
myObject
:
z
.
object
({
aString
:
z
.
string
(),
}),
}),
},
),
).
toEqual
([
{
myObject
:
{
aString
:
'
value
'
,
},
},
]);
});
it
(
'
should parse content with multiple documents
'
,
()
=>
{
it
(
'
should parse content with multiple documents
'
,
()
=>
{
expect
(
expect
(
parseYaml
(
codeBlock
`
parseYaml
(
codeBlock
`
...
@@ -42,6 +68,60 @@ describe('util/yaml', () => {
...
@@ -42,6 +68,60 @@ describe('util/yaml', () => {
]);
]);
});
});
it
(
'
should parse content with multiple documents with schema
'
,
()
=>
{
expect
(
parseYaml
(
codeBlock
`
myObject:
aString: foo
---
myObject:
aString: bar
`
,
null
,
{
customSchema
:
z
.
object
({
myObject
:
z
.
object
({
aString
:
z
.
string
(),
}),
}),
},
),
).
toEqual
([
{
myObject
:
{
aString
:
'
foo
'
,
},
},
{
myObject
:
{
aString
:
'
bar
'
,
},
},
]);
});
it
(
'
should throw if schema does not match
'
,
()
=>
{
expect
(()
=>
parseYaml
(
codeBlock
`
myObject:
aString: foo
---
aString: bar
`
,
null
,
{
customSchema
:
z
.
object
({
myObject
:
z
.
object
({
aString
:
z
.
string
(),
}),
}),
},
),
).
toThrow
();
});
it
(
'
should parse content with templates
'
,
()
=>
{
it
(
'
should parse content with templates
'
,
()
=>
{
expect
(
expect
(
parseYaml
(
parseYaml
(
...
@@ -85,6 +165,45 @@ describe('util/yaml', () => {
...
@@ -85,6 +165,45 @@ describe('util/yaml', () => {
});
});
});
});
it
(
'
should parse content with single document with schema
'
,
()
=>
{
expect
(
parseSingleYaml
(
codeBlock
`
myObject:
aString: value
`
,
{
customSchema
:
z
.
object
({
myObject
:
z
.
object
({
aString
:
z
.
string
(),
}),
}),
},
),
).
toEqual
({
myObject
:
{
aString
:
'
value
'
,
},
});
});
it
(
'
should throw with single document with schema if parsing fails
'
,
()
=>
{
expect
(()
=>
parseSingleYaml
(
codeBlock
`
myObject: foo
`
,
{
customSchema
:
z
.
object
({
myObject
:
z
.
object
({
aString
:
z
.
string
(),
}),
}),
},
),
).
toThrow
();
});
it
(
'
should parse content with multiple documents
'
,
()
=>
{
it
(
'
should parse content with multiple documents
'
,
()
=>
{
expect
(()
=>
expect
(()
=>
parseSingleYaml
(
codeBlock
`
parseSingleYaml
(
codeBlock
`
...
...
This diff is collapsed.
Click to expand it.
lib/util/yaml.ts
+
34
−
10
View file @
48439d28
...
@@ -5,28 +5,52 @@ import {
...
@@ -5,28 +5,52 @@ import {
load
as
single
,
load
as
single
,
dump
as
upstreamDump
,
dump
as
upstreamDump
,
}
from
'
js-yaml
'
;
}
from
'
js-yaml
'
;
import
type
{
ZodType
}
from
'
zod
'
;
import
{
regEx
}
from
'
./regex
'
;
import
{
regEx
}
from
'
./regex
'
;
interface
YamlOptions
extends
LoadOptions
{
type
YamlOptions
<
ResT
=
unknown
,
Schema
extends
ZodType
<
ResT
>
=
ZodType
<
ResT
>
,
>
=
{
customSchema
?:
Schema
;
removeTemplates
?:
boolean
;
removeTemplates
?:
boolean
;
}
}
&
LoadOptions
;
export
function
parseYaml
(
export
function
parseYaml
<
ResT
=
unknown
>
(
content
:
string
,
content
:
string
,
iterator
?:
null
|
undefined
,
iterator
?:
null
|
undefined
,
options
?:
YamlOptions
,
options
?:
YamlOptions
<
ResT
>
,
):
unknown
[]
{
):
ResT
[]
{
const
massagedContent
=
massageContent
(
content
,
options
);
const
massagedContent
=
massageContent
(
content
,
options
);
return
multiple
(
massagedContent
,
iterator
,
options
);
const
rawDocuments
=
multiple
(
massagedContent
,
iterator
,
options
);
const
schema
=
options
?.
customSchema
;
if
(
!
schema
)
{
return
rawDocuments
as
ResT
[];
}
const
parsed
:
ResT
[]
=
[];
for
(
const
element
of
rawDocuments
)
{
const
singleParsed
=
schema
.
parse
(
element
);
parsed
.
push
(
singleParsed
);
}
return
parsed
;
}
}
export
function
parseSingleYaml
(
export
function
parseSingleYaml
<
ResT
=
unknown
>
(
content
:
string
,
content
:
string
,
options
?:
YamlOptions
,
options
?:
YamlOptions
<
ResT
>
,
):
unknown
{
):
ResT
{
const
massagedContent
=
massageContent
(
content
,
options
);
const
massagedContent
=
massageContent
(
content
,
options
);
return
single
(
massagedContent
,
options
);
const
rawDocument
=
single
(
massagedContent
,
options
);
const
schema
=
options
?.
customSchema
;
if
(
!
schema
)
{
return
rawDocument
as
ResT
;
}
return
schema
.
parse
(
rawDocument
);
}
}
export
function
dump
(
obj
:
any
,
opts
?:
DumpOptions
|
undefined
):
string
{
export
function
dump
(
obj
:
any
,
opts
?:
DumpOptions
|
undefined
):
string
{
...
...
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