refactor(samples): simplify string related constraints

Refs #8577
This commit is contained in:
Vladimir Gorej
2023-06-07 12:13:24 +02:00
parent 68cfe46490
commit c529c9e0c3

View File

@@ -87,7 +87,7 @@ const numberConstraints = [
"exclusiveMaximum", "exclusiveMaximum",
"multipleOf", "multipleOf",
] ]
const stringContracts = ["minLength", "maxLength"] const stringConstraints = ["minLength", "maxLength", "pattern"]
const liftSampleHelper = (oldSchema, target, config = {}) => { const liftSampleHelper = (oldSchema, target, config = {}) => {
const setIfNotDefinedInTarget = (key) => { const setIfNotDefinedInTarget = (key) => {
@@ -106,7 +106,7 @@ const liftSampleHelper = (oldSchema, target, config = {}) => {
...objectContracts, ...objectContracts,
...arrayContracts, ...arrayContracts,
...numberConstraints, ...numberConstraints,
...stringContracts, ...stringConstraints,
].forEach((key) => setIfNotDefinedInTarget(key)) ].forEach((key) => setIfNotDefinedInTarget(key))
if (oldSchema.required !== undefined && Array.isArray(oldSchema.required)) { if (oldSchema.required !== undefined && Array.isArray(oldSchema.required)) {
@@ -721,10 +721,10 @@ export const sampleFromSchemaGeneric = (
} }
} }
if (typeof value === "string") { if (typeof value === "string") {
if (schema.maxLength !== null && schema.maxLength !== undefined) { if (typeof schema.maxLength === "number") {
value = value.slice(0, schema.maxLength) value = value.slice(0, schema.maxLength)
} }
if (schema.minLength !== null && schema.minLength !== undefined) { if (typeof schema.minLength === "number") {
let i = 0 let i = 0
while (value.length < schema.minLength) { while (value.length < schema.minLength) {
value += value[i++ % value.length] value += value[i++ % value.length]