Skip to content

Commit

Permalink
fix(examples): allow string created by regex pattern (#7829)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Lai <timothy.lai@gmail.com>
  • Loading branch information
dewijones92 and tim-lai committed Mar 4, 2022
1 parent 7d4086b commit 3d79c24
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -74,6 +74,7 @@
"js-yaml": "=4.1.0",
"lodash": "^4.17.21",
"prop-types": "^15.8.1",
"randexp": "^0.5.3",

This comment has been minimized.

Copy link
@char0n

char0n Mar 10, 2022

Member

Note: package-lock.json change didn't come with this PR. I'll regenerate package-lock and push to master branch.

This comment has been minimized.

Copy link
@char0n

char0n Mar 10, 2022

Member

This has been resolved with latest dependabot updates.

"randombytes": "^2.1.0",
"react": "=17.0.2",
"react-copy-to-clipboard": "5.0.4",
Expand Down
8 changes: 7 additions & 1 deletion src/core/plugins/samples/fn.js
@@ -1,11 +1,17 @@
import XML from "xml"
import RandExp from "randexp"
import isEmpty from "lodash/isEmpty"
import { objectify, isFunc, normalizeArray, deeplyStripKey } from "core/utils"

import memoizeN from "../../../helpers/memoizeN"

const generateStringFromRegex = (pattern) => {
const randexp = new RandExp(pattern)
return randexp.gen()
}

const primitives = {
"string": () => "string",
"string": (schema) => schema.pattern ? generateStringFromRegex(schema.pattern) : "string",
"string_email": () => "user@example.com",
"string_date-time": () => new Date().toISOString(),
"string_date": () => new Date().toISOString().substring(0, 10),
Expand Down
17 changes: 17 additions & 0 deletions test/unit/core/plugins/samples/fn.js
Expand Up @@ -153,6 +153,23 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition, { includeReadOnly: true })).toEqual(expected)
})



it("regex pattern test", function () {
let definition = {
type: "object",
properties: {
macAddress: {
type: "string",
pattern: "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"
}
}
}
const resp = sampleFromSchema(definition)

expect(new RegExp("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", "g").test(resp.macAddress)).toBe(true)
})

it("returns object without deprecated fields for parameter", function () {
let definition = {
type: "object",
Expand Down

0 comments on commit 3d79c24

Please sign in to comment.