Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: json-schema-faker/json-schema-faker
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: datawire/json-schema-faker
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 3 commits
  • 21 files changed
  • 1 contributor

Commits on Mar 14, 2025

  1. Remove JSONPath support

    P0lip committed Mar 14, 2025
    Copy the full SHA
    65e776d View commit details
  2. Copy the full SHA
    2624025 View commit details
  3. Impose traversal limit

    P0lip committed Mar 14, 2025
    Copy the full SHA
    de88037 View commit details
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@

The source code is written in modern ES6-modules through `src` (for testing), but bundled versions are provided to ensure portabilty:

- `vendor.js` required dependencies for browser usage: `json-schema-ref-parser` and `jsonpath-plus`
- `vendor.js` required dependencies for browser usage: `json-schema-ref-parser`
- `bundle.js` is generated containing both dependencies from above, ready for the browser!
- `main.{iife,esm,cjs}.js` are the bundled versions for general usage on NodeJS

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@
"watch": "npm run mortero -- --platform browser -dw",
"build": "NODE_ENV=production npm run mortero -- -fS --no-minify --platform browser",
"pretest": "npm run lint && npm run build -- -ymain -yshared",
"copy:vendor": "concat -o dist/vendor.js node_modules/json-schema-ref-parser/dist/ref-parser.min.js node_modules/jsonpath-plus/dist/index-browser-umd.min.cjs",
"copy:vendor": "cp node_modules/json-schema-ref-parser/dist/ref-parser.min.js dist/vendor.js",
"copy:bundle": "concat -o dist/bundle.js dist/vendor.js dist/main.iife.js",
"copy:wargs": "concat -o dist/wargs.cjs node_modules/wargs/dist/wargs.cjs",
"entry:cjs": "node patch.js",
@@ -66,8 +66,7 @@
"**/app.js"
],
"external": [
"json-schema-ref-parser",
"jsonpath-plus"
"json-schema-ref-parser"
],
"options": {
"less": {
@@ -99,8 +98,7 @@
"index.d.ts"
],
"dependencies": {
"json-schema-ref-parser": "^6.1.0",
"jsonpath-plus": "^10.1.0"
"json-schema-ref-parser": "^6.1.0"
},
"devDependencies": {
"@faker-js/faker": "^7.6.0",
5 changes: 0 additions & 5 deletions patch.js
Original file line number Diff line number Diff line change
@@ -8,8 +8,3 @@ writeFileSync('dist/index.cjs', 'module.exports = require("./main.cjs").default;

// prefix was setting `location` and that caused redirections under browser-like environments
// if you run this module on a browser-like environment that does not have `location` set it'll fail

let vendor = readFileSync('dist/vendor.js').toString();
vendor = `!(()=>{${vendor}})()`;

writeFileSync('dist/vendor.js', vendor);
1 change: 0 additions & 1 deletion src/lib/api/defaults.mjs
Original file line number Diff line number Diff line change
@@ -25,7 +25,6 @@ defaults.maxItems = null;
defaults.minLength = 0;
defaults.maxLength = null;

defaults.resolveJsonPath = false;
defaults.reuseProperties = false;
defaults.fillProperties = true;
defaults.sortProperties = false;
Empty file added src/lib/core/cleanSchema.mjs
Empty file.
94 changes: 4 additions & 90 deletions src/lib/core/run.mjs
Original file line number Diff line number Diff line change
@@ -1,87 +1,8 @@
import { getDependencies } from '../vendor.mjs';
import optionAPI from '../api/option.mjs';
import traverse from './traverse.mjs';
import random from './random.mjs';
import createTraverse from './traverse.mjs';
import utils from './utils.mjs';
import buildResolveSchema from './buildResolveSchema.mjs';

function pick(data) {
return Array.isArray(data)
? random.pick(data)
: data;
}

function cycle(data, reverse) {
if (!Array.isArray(data)) {
return data;
}

const value = reverse
? data.pop()
: data.shift();

if (reverse) {
data.unshift(value);
} else {
data.push(value);
}

return value;
}

function resolve(obj, data, values, property) {
if (!obj || typeof obj !== 'object') {
return obj;
}

if (!values) {
values = {};
}

if (!data) {
data = obj;
}

if (Array.isArray(obj)) {
return obj.map(x => resolve(x, data, values, property));
}

if (obj.jsonPath) {
const { JSONPath } = getDependencies();

const params = typeof obj.jsonPath !== 'object'
? { path: obj.jsonPath }
: obj.jsonPath;

params.group = obj.group || params.group || property;
params.cycle = obj.cycle || params.cycle || false;
params.reverse = obj.reverse || params.reverse || false;
params.count = obj.count || params.count || 1;

const key = `${params.group}__${params.path}`;

if (!values[key]) {
if (params.count > 1) {
values[key] = JSONPath(params.path, data).slice(0, params.count);
} else {
values[key] = JSONPath(params.path, data);
}
}

if (params.cycle || params.reverse) {
return cycle(values[key], params.reverse);
}

return pick(values[key]);
}

Object.keys(obj).forEach(k => {
obj[k] = resolve(obj[k], data, values, k);
});

return obj;
}

// TODO provide types?
function run(refs, schema, container, synchronous) {
if (Object.prototype.toString.call(schema) !== '[object Object]') {
@@ -90,6 +11,8 @@ function run(refs, schema, container, synchronous) {

const refDepthMin = optionAPI('refDepthMin') || 0;
const refDepthMax = optionAPI('refDepthMax') || 3;
const ticks = optionAPI('ticks') ?? -1;
const traverse = createTraverse(ticks);

try {
const { resolveSchema } = buildResolveSchema({
@@ -100,16 +23,7 @@ function run(refs, schema, container, synchronous) {
refDepthMin,
refDepthMax,
});
const result = traverse(utils.clone(schema), [], resolveSchema);

if (optionAPI('resolveJsonPath')) {
return {
value: resolve(result.value),
context: result.context,
};
}

return result;
return traverse(utils.clone(schema), [], resolveSchema);
} catch (e) {
if (e.path) {
throw new Error(`${e.message} in /${e.path.join('/')}`);
50 changes: 50 additions & 0 deletions src/lib/core/toMaybeDraft4.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const KNOWN_VOCABULARY = new Set([
'$ref',
'id',
'$schema',
'title',
'description',
'default',
'multipleOf',
'maximum',
'exclusiveMaximum',
'minimum',
'exclusiveMinimum',
'maxLength',
'minLength',
'pattern',
'additionalItems',
'items',
'maxItems',
'minItems',
'uniqueItems',
'maxProperties',
'minProperties',
'required',
'additionalProperties',
'definitions',
'properties',
'patternProperties',
'dependencies',
'enum',
'type',
'format',
'allOf',
'anyOf',
'oneOf',
'not',
]);

function toMaybeDraft4(maybeSchema) {
const keys = Object.keys(maybeSchema);
const maybeDraft4 = {};
for (const key of keys) {
if (KNOWN_VOCABULARY.has(key)) {
toMaybeDraft4[key] = maybeSchema[key];
}
}

return maybeDraft4;
}

export default toMaybeDraft4;
Loading