From e587ae0f90bb244b62c0c4f6e23f0214c9ebda47 Mon Sep 17 00:00:00 2001 From: Oprysk Viacheslav Date: Thu, 2 May 2024 16:26:49 +0300 Subject: [PATCH] feat: add enablePatterns config option (#163) --- src/samplers/string.js | 5 +++-- src/types.d.ts | 1 + test/unit/string.spec.js | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/samplers/string.js b/src/samplers/string.js index f332e50..ef30dba 100644 --- a/src/samplers/string.js +++ b/src/samplers/string.js @@ -43,8 +43,8 @@ function timeSample(min, max) { return commonDateTimeSample({ min, max, omitTime: false, omitDate: true }).slice(1); } -function defaultSample(min, max, _propertyName, pattern) { - if (pattern) { +function defaultSample(min, max, _propertyName, pattern, enablePatterns = false) { + if (pattern && enablePatterns) { return faker.regexSample(pattern); } let res = ensureMinLength('string', min); @@ -136,5 +136,6 @@ export function sampleString(schema, options, spec, context) { schema.maxLength, propertyName, schema.pattern, + options?.enablePatterns ); } diff --git a/src/types.d.ts b/src/types.d.ts index 55713b2..6da5a4b 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -5,6 +5,7 @@ export interface Options { readonly skipReadOnly?: boolean; readonly skipWriteOnly?: boolean; readonly quiet?: boolean; + readonly enablePatterns?: boolean } export function sample(schema: JSONSchema7, options?: Options, document?: object): unknown; diff --git a/test/unit/string.spec.js b/test/unit/string.spec.js index 1b9c255..7b8cdc5 100644 --- a/test/unit/string.spec.js +++ b/test/unit/string.spec.js @@ -135,7 +135,7 @@ describe('sampleString', () => { .forEach((regexp) => { res = sampleString( {pattern: regexp.source}, - null, + {enablePatterns: true}, null, {propertyName: 'fooId'}, );