Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
json-schema-faker
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fork
json-schema-faker
Commits
fd3e5a36
Commit
fd3e5a36
authored
6 years ago
by
Alvaro Cabrera Durán
Browse files
Options
Downloads
Patches
Plain Diff
Include additional formats; include OAS ones
parent
bb11eff9
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/generators/coreFormat.js
+22
-3
22 additions, 3 deletions
src/generators/coreFormat.js
src/types/string.js
+7
-0
7 additions, 0 deletions
src/types/string.js
with
29 additions
and
3 deletions
src/generators/coreFormat.js
+
22
−
3
View file @
fd3e5a36
import
random
from
'
../core/random
'
;
const
FRAGMENT
=
'
[a-zA-Z][a-zA-Z0-9+-.]*
'
;
const
URI_PATTERN
=
`https?://{hostname}(?:
${
FRAGMENT
}
)+`
;
const
PARAM_PATTERN
=
'
(?:
\\
?([a-z]{1,7}(=
\\
w{1,5})?&){0,3})?
'
;
/**
* Predefined core formats
* @type {[key: string]: string}
...
...
@@ -8,10 +12,25 @@ const regexps = {
email
:
'
[a-zA-Z
\\
d][a-zA-Z
\\
d-]{1,13}[a-zA-Z
\\
d]@{hostname}
'
,
hostname
:
'
[a-zA-Z]{1,33}
\\
.[a-z]{2,4}
'
,
ipv6
:
'
[a-f
\\
d]{4}(:[a-f
\\
d]{4}){7}
'
,
uri
:
'
https?://[a-zA-Z][a-zA-Z0-9+-.]*
'
,
'
uri-reference
'
:
'
(https?://|#|/|)[a-zA-Z][a-zA-Z0-9+-.]*
'
,
uri
:
URI_PATTERN
,
// types from draft-0[67] (?)
'
uri-reference
'
:
`
${
URI_PATTERN
}${
PARAM_PATTERN
}
`
,
'
uri-template
'
:
URI_PATTERN
.
replace
(
'
(?:
'
,
'
(?:/
\\
{[a-z][:a-zA-Z0-9-]*
\\
}|
'
),
'
json-pointer
'
:
`(/(?:
${
FRAGMENT
.
replace
(
'
]*
'
,
'
/]*
'
)}
|~[01]))+`
,
// some types from https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#data-types (?)
uuid
:
'
^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$
'
,
};
regexps
.
iri
=
regexps
[
'
uri-reference
'
];
regexps
[
'
iri-reference
'
]
=
regexps
[
'
uri-reference
'
];
regexps
[
'
idn-email
'
]
=
regexps
.
email
;
regexps
[
'
idn-hostname
'
]
=
regexps
.
hostname
;
const
ALLOWED_FORMATS
=
new
RegExp
(
`
\\
{(
${
Object
.
keys
(
regexps
).
join
(
'
|
'
)}
)
\\
}`
);
/**
* Generates randomized string basing on a built-in regex format
*
...
...
@@ -19,7 +38,7 @@ const regexps = {
* @returns {string}
*/
function
coreFormatGenerator
(
coreFormat
)
{
return
random
.
randexp
(
regexps
[
coreFormat
]).
replace
(
/
\{(\w
+
)\}
/
,
(
match
,
key
)
=>
{
return
random
.
randexp
(
regexps
[
coreFormat
]).
replace
(
ALLOWED_FORMATS
,
(
match
,
key
)
=>
{
return
random
.
randexp
(
regexps
[
key
]);
});
}
...
...
This diff is collapsed.
Click to expand it.
src/types/string.js
+
7
−
0
View file @
fd3e5a36
...
...
@@ -34,6 +34,13 @@ function generateFormat(value, invalid) {
case
'
ipv6
'
:
case
'
uri
'
:
case
'
uri-reference
'
:
case
'
iri
'
:
case
'
iri-reference
'
:
case
'
idn-email
'
:
case
'
idn-hostname
'
:
case
'
json-pointer
'
:
case
'
uri-template
'
:
case
'
uuid
'
:
return
coreFormat
(
value
.
format
);
default
:
if
(
typeof
callback
===
'
undefined
'
)
{
...
...
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