From c529c9e0c3a7cd1fa541524ad9e423d71b377d14 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Wed, 7 Jun 2023 12:13:24 +0200 Subject: [PATCH] refactor(samples): simplify string related constraints Refs #8577 --- .../plugins/json-schema-2020-12/samples-extensions/fn.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/plugins/json-schema-2020-12/samples-extensions/fn.js b/src/core/plugins/json-schema-2020-12/samples-extensions/fn.js index 03c4d5b0..76c9af41 100644 --- a/src/core/plugins/json-schema-2020-12/samples-extensions/fn.js +++ b/src/core/plugins/json-schema-2020-12/samples-extensions/fn.js @@ -87,7 +87,7 @@ const numberConstraints = [ "exclusiveMaximum", "multipleOf", ] -const stringContracts = ["minLength", "maxLength"] +const stringConstraints = ["minLength", "maxLength", "pattern"] const liftSampleHelper = (oldSchema, target, config = {}) => { const setIfNotDefinedInTarget = (key) => { @@ -106,7 +106,7 @@ const liftSampleHelper = (oldSchema, target, config = {}) => { ...objectContracts, ...arrayContracts, ...numberConstraints, - ...stringContracts, + ...stringConstraints, ].forEach((key) => setIfNotDefinedInTarget(key)) if (oldSchema.required !== undefined && Array.isArray(oldSchema.required)) { @@ -721,10 +721,10 @@ export const sampleFromSchemaGeneric = ( } } if (typeof value === "string") { - if (schema.maxLength !== null && schema.maxLength !== undefined) { + if (typeof schema.maxLength === "number") { value = value.slice(0, schema.maxLength) } - if (schema.minLength !== null && schema.minLength !== undefined) { + if (typeof schema.minLength === "number") { let i = 0 while (value.length < schema.minLength) { value += value[i++ % value.length]