Skip to content

Commit

Permalink
Merge pull request #829 from Ceres6/fix/max-length-regex
Browse files Browse the repository at this point in the history
feat: implement best effort approach to keep patter valid when stripping
  • Loading branch information
pateketrueke authored Nov 6, 2024
2 parents 5967227 + 4daa4f5 commit ccd955f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ jsf.locate('faker');
- `omitNulls` — Remove any generated `null` value from the resulting value (default: `false`)
- `minDateTime` — When generating a string with format `date-time`, set the minimum date that will get selected. If it's a number it will be the offset from `now` in milliseconds (default: `-2524608000000`)
- `maxDateTime` — When generating a string with format `date-time`, set the maximum date that will get selected. If it's a number it will be the offset from `now` in milliseconds (default: `1000`)
- `maxRegexRetry` — When generating a string with a `pattern` and a `maxLength`, if the `maxLength` is smaller than the generated value, the value will be cropped. If the new value is not matching the pattern, it will try to keep cropping the value until it's valid or it reaches either the minimum allowed length or the maximum number of iterations set by this option (default: `100`)

## Building

Expand Down
1 change: 1 addition & 0 deletions index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface JSONSchemaFakerOptions {
renderComment?: boolean;
refDepthMax?: number;
refDepthMin?: number;
maxRegexRetry?: number;
}

export type JSONSchemaFakerRefs = Schema[] | { [k: string]: Schema };
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface JSONSchemaFakerOptions {
renderComment?: boolean;
refDepthMax?: number;
refDepthMin?: number;
maxRegexRetry?: number;
}

export type JSONSchemaFakerRefs = Schema[] | { [k: string]: Schema };
Expand Down
1 change: 1 addition & 0 deletions src/lib/api/defaults.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default defaults;

defaults.defaultInvalidTypeProduct = undefined;
defaults.defaultRandExpMax = 10;
defaults.maxRegexRetry = 100;

defaults.pruneProperties = [];
defaults.ignoreProperties = [];
Expand Down
12 changes: 12 additions & 0 deletions src/lib/core/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ function typecast(type, schema, callback) {

if (value.length > max) {
value = value.substr(0, max);
const pattern = schema.pattern ? new RegExp(schema.pattern) : null;
if (pattern && !pattern.test(value)) {
let temp = value;
const maxRetries = optionAPI('maxRegexRetry');
const minLength = Math.max(value.length - maxRetries, min);
while (temp.length > minLength && !pattern.test(temp)) {
temp = temp.slice(0, -1);
if (pattern.test(temp)) {
value = temp;
}
}
}
}

switch (schema.format) {
Expand Down
14 changes: 14 additions & 0 deletions tests/schema/core/types/string.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
},
"valid": true
},
{
"description": "string generator",
"tests": [
{
"description": "should handle pattern with maxLength",
"schema": {
"type": "string",
"pattern": "^[a-z]{2}( [a-z])?$",
"maxLength": 3
},
"valid": true
}
]
},
{
"description": "should handle format (core)",
"schema": {
Expand Down

0 comments on commit ccd955f

Please sign in to comment.