Skip to content

Commit 3d79c24

Browse files
dewijones92tim-lai
andauthoredMar 4, 2022
fix(examples): allow string created by regex pattern (#7829)
Co-authored-by: Tim Lai <timothy.lai@gmail.com>
1 parent 7d4086b commit 3d79c24

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"js-yaml": "=4.1.0",
7575
"lodash": "^4.17.21",
7676
"prop-types": "^15.8.1",
77+
"randexp": "^0.5.3",
Has conversations. Original line has conversations.
7778
"randombytes": "^2.1.0",
7879
"react": "=17.0.2",
7980
"react-copy-to-clipboard": "5.0.4",

‎src/core/plugins/samples/fn.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import XML from "xml"
2+
import RandExp from "randexp"
23
import isEmpty from "lodash/isEmpty"
34
import { objectify, isFunc, normalizeArray, deeplyStripKey } from "core/utils"
45

56
import memoizeN from "../../../helpers/memoizeN"
67

8+
const generateStringFromRegex = (pattern) => {
9+
const randexp = new RandExp(pattern)
10+
return randexp.gen()
11+
}
12+
713
const primitives = {
8-
"string": () => "string",
14+
"string": (schema) => schema.pattern ? generateStringFromRegex(schema.pattern) : "string",
915
"string_email": () => "user@example.com",
1016
"string_date-time": () => new Date().toISOString(),
1117
"string_date": () => new Date().toISOString().substring(0, 10),

‎test/unit/core/plugins/samples/fn.js

+17
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,23 @@ describe("sampleFromSchema", () => {
153153
expect(sampleFromSchema(definition, { includeReadOnly: true })).toEqual(expected)
154154
})
155155

156+
157+
158+
it("regex pattern test", function () {
159+
let definition = {
160+
type: "object",
161+
properties: {
162+
macAddress: {
163+
type: "string",
164+
pattern: "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"
165+
}
166+
}
167+
}
168+
const resp = sampleFromSchema(definition)
169+
170+
expect(new RegExp("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", "g").test(resp.macAddress)).toBe(true)
171+
})
172+
156173
it("returns object without deprecated fields for parameter", function () {
157174
let definition = {
158175
type: "object",

0 commit comments

Comments
 (0)
Please sign in to comment.