Skip to content

Commit

Permalink
Generate examples from pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
j3rem1e committed Aug 19, 2023
1 parent 3e4a20c commit 7606464
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"lit": "^2.7.5",
"marked": "^5.1.0",
"prismjs": "^1.29.0",
"randexp": "^0.5.3",
"xml-but-prettier": "^1.0.1"
},
"scripts": {
Expand Down
13 changes: 12 additions & 1 deletion src/utils/schema-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import RandExp from 'randexp';

// Make RandExp determinist
RandExp.prototype.randInt = (from) => from;

// Takes a value as input and provides a printable string to replresent null values, spaces, blankstring etc
export function getPrintableVal(val) {
if (val === undefined) {
Expand Down Expand Up @@ -260,7 +265,13 @@ export function getSampleValueByType(schemaObj) {
if (typeValue.match(/^string/g)) {
if (schemaObj.enum) { return schemaObj.enum[0]; }
if (schemaObj.const) { return schemaObj.const; }
if (schemaObj.pattern) { return schemaObj.pattern; }
if (schemaObj.pattern) {
try {
return new RandExp(schemaObj.pattern).gen();
} catch (error) {
return schemaObj.pattern;
}
}
if (schemaObj.format) {
const u = `${Date.now().toString(16)}${Math.random().toString(16)}0`.repeat(16);
switch (schemaObj.format.toLowerCase()) {
Expand Down

0 comments on commit 7606464

Please sign in to comment.