From 93b1e92d147f59081e66653642c78118386607b8 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 15 Feb 2024 15:26:05 +1100 Subject: [PATCH 1/2] Add @criteria/synthetic-data --- package-lock.json | 40 +++++++++++ packages/criteria-synthetic-data/LICENSE | 9 +++ packages/criteria-synthetic-data/README.md | 3 + .../criteria-synthetic-data/jest.config.js | 6 ++ packages/criteria-synthetic-data/package.json | 48 ++++++++++++++ packages/criteria-synthetic-data/src/index.ts | 1 + .../src/synthesize/Options.ts | 7 ++ .../src/synthesize/synthesizeArray.ts | 20 ++++++ .../src/synthesize/synthesizeBoolean.ts | 10 +++ .../src/synthesize/synthesizeNumber.ts | 21 ++++++ .../src/synthesize/synthesizeObject.ts | 20 ++++++ .../src/synthesize/synthesizeString.ts | 8 +++ .../src/synthesize/synthesizeValue.ts | 66 +++++++++++++++++++ .../src/synthesizeDataWithJSONSchema.test.ts | 35 ++++++++++ .../src/synthesizeDataWithJSONSchema.ts | 12 ++++ .../src/util/hash.test.ts | 22 +++++++ .../criteria-synthetic-data/src/util/hash.ts | 23 +++++++ .../criteria-synthetic-data/tsconfig.json | 14 ++++ 18 files changed, 365 insertions(+) create mode 100644 packages/criteria-synthetic-data/LICENSE create mode 100644 packages/criteria-synthetic-data/README.md create mode 100644 packages/criteria-synthetic-data/jest.config.js create mode 100644 packages/criteria-synthetic-data/package.json create mode 100644 packages/criteria-synthetic-data/src/index.ts create mode 100644 packages/criteria-synthetic-data/src/synthesize/Options.ts create mode 100644 packages/criteria-synthetic-data/src/synthesize/synthesizeArray.ts create mode 100644 packages/criteria-synthetic-data/src/synthesize/synthesizeBoolean.ts create mode 100644 packages/criteria-synthetic-data/src/synthesize/synthesizeNumber.ts create mode 100644 packages/criteria-synthetic-data/src/synthesize/synthesizeObject.ts create mode 100644 packages/criteria-synthetic-data/src/synthesize/synthesizeString.ts create mode 100644 packages/criteria-synthetic-data/src/synthesize/synthesizeValue.ts create mode 100644 packages/criteria-synthetic-data/src/synthesizeDataWithJSONSchema.test.ts create mode 100644 packages/criteria-synthetic-data/src/synthesizeDataWithJSONSchema.ts create mode 100644 packages/criteria-synthetic-data/src/util/hash.test.ts create mode 100644 packages/criteria-synthetic-data/src/util/hash.ts create mode 100644 packages/criteria-synthetic-data/tsconfig.json diff --git a/package-lock.json b/package-lock.json index f109c08..bad7eb7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -609,6 +609,10 @@ "resolved": "packages/criteria-openapi", "link": true }, + "node_modules/@criteria/synthetic-data": { + "resolved": "packages/criteria-synthetic-data", + "link": true + }, "node_modules/@eslint/eslintrc": { "version": "1.4.0", "dev": true, @@ -4130,6 +4134,11 @@ "queue-microtask": "^1.2.2" } }, + "node_modules/seedrandom": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-3.0.5.tgz", + "integrity": "sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg==" + }, "node_modules/semver": { "version": "7.3.8", "dev": true, @@ -4806,6 +4815,21 @@ "ts-jest": "^29.0.3", "typescript": "^4.9.4" } + }, + "packages/criteria-synthetic-data": { + "name": "@criteria/synthetic-data", + "version": "0.1.0", + "license": "MIT", + "dependencies": { + "@criteria/json-schema": "^0.9.0", + "seedrandom": "^3.0.5" + }, + "devDependencies": { + "@types/jest": "^29.2.4", + "jest": "^29.3.1", + "ts-jest": "^29.0.3", + "typescript": "^4.9.4" + } } }, "dependencies": { @@ -5275,6 +5299,17 @@ "typescript": "^4.9.4" } }, + "@criteria/synthetic-data": { + "version": "file:packages/criteria-synthetic-data", + "requires": { + "@criteria/json-schema": "^0.9.0", + "@types/jest": "^29.2.4", + "jest": "^29.3.1", + "seedrandom": "^3.0.5", + "ts-jest": "^29.0.3", + "typescript": "^4.9.4" + } + }, "@eslint/eslintrc": { "version": "1.4.0", "dev": true, @@ -7525,6 +7560,11 @@ "queue-microtask": "^1.2.2" } }, + "seedrandom": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-3.0.5.tgz", + "integrity": "sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg==" + }, "semver": { "version": "7.3.8", "dev": true, diff --git a/packages/criteria-synthetic-data/LICENSE b/packages/criteria-synthetic-data/LICENSE new file mode 100644 index 0000000..f4f2334 --- /dev/null +++ b/packages/criteria-synthetic-data/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2023 Criteria Labs Pty Ltd + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/criteria-synthetic-data/README.md b/packages/criteria-synthetic-data/README.md new file mode 100644 index 0000000..62ce808 --- /dev/null +++ b/packages/criteria-synthetic-data/README.md @@ -0,0 +1,3 @@ +@criteria/synthetic-data + +Synthesize data that conforms to schema. diff --git a/packages/criteria-synthetic-data/jest.config.js b/packages/criteria-synthetic-data/jest.config.js new file mode 100644 index 0000000..5225ccd --- /dev/null +++ b/packages/criteria-synthetic-data/jest.config.js @@ -0,0 +1,6 @@ +/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', + roots: ['/src'] +} diff --git a/packages/criteria-synthetic-data/package.json b/packages/criteria-synthetic-data/package.json new file mode 100644 index 0000000..67dd23a --- /dev/null +++ b/packages/criteria-synthetic-data/package.json @@ -0,0 +1,48 @@ +{ + "name": "@criteria/synthetic-data", + "version": "0.1.0", + "description": "Synthesize data that conforms to a schema.", + "keywords": [ + "synthetic data", + "example data", + "sample data" + ], + "homepage": "https://github.com/criteria-labs/criteria-api-tools", + "bugs": { + "url": "https://github.com/criteria-labs/criteria-api-tools/issues", + "email": "support@criteria.sh" + }, + "license": "MIT", + "author": "Criteria Labs Pty Ltd", + "files": [ + "dist/*", + "!dist/**/*.test.{js,d.ts}" + ], + "exports": { + ".": "./dist/index.js", + "./v3.0": "./dist/v3.0.js", + "./v3.1": "./dist/v3.1.js" + }, + "main": "dist/index.js", + "types": "dist/index.d.ts", + "repository": { + "type": "git", + "url": "https://github.com/criteria-labs/criteria-api-tools.git", + "directory": "packages/synthetic-data" + }, + "scripts": { + "build": "tsc --build", + "test": "jest .", + "clean": "rimraf tsconfig.build.tsbuildinfo ./dist" + }, + "dependencies": { + "@criteria/json-schema": "^0.9.0", + "seedrandom": "^3.0.5" + }, + "devDependencies": { + "@types/jest": "^29.2.4", + "jest": "^29.3.1", + "ts-jest": "^29.0.3", + "typescript": "^4.9.4" + } +} diff --git a/packages/criteria-synthetic-data/src/index.ts b/packages/criteria-synthetic-data/src/index.ts new file mode 100644 index 0000000..1acc6e4 --- /dev/null +++ b/packages/criteria-synthetic-data/src/index.ts @@ -0,0 +1 @@ +export * from './synthesizeDataWithJSONSchema' diff --git a/packages/criteria-synthetic-data/src/synthesize/Options.ts b/packages/criteria-synthetic-data/src/synthesize/Options.ts new file mode 100644 index 0000000..aa4bc36 --- /dev/null +++ b/packages/criteria-synthetic-data/src/synthesize/Options.ts @@ -0,0 +1,7 @@ +export interface Options { + random: () => number + minimum?: number + maximum?: number + minItems?: number + maxItems?: number +} diff --git a/packages/criteria-synthetic-data/src/synthesize/synthesizeArray.ts b/packages/criteria-synthetic-data/src/synthesize/synthesizeArray.ts new file mode 100644 index 0000000..4f38f52 --- /dev/null +++ b/packages/criteria-synthetic-data/src/synthesize/synthesizeArray.ts @@ -0,0 +1,20 @@ +import { JSONSchemaDraft04, JSONSchemaDraft06, JSONSchemaDraft07, JSONSchemaDraft2020_12 } from '@criteria/json-schema' +import { synthesizeValue } from './synthesizeValue' +import { Options } from './Options' + +type Schema = JSONSchemaDraft04 | JSONSchemaDraft06 | JSONSchemaDraft07 | JSONSchemaDraft2020_12 + +export function synthesizeArray(schema: Schema & object, options: Options): any[] { + const { random } = options + + const minItems = schema.minItems ?? options.minItems ?? 1 + const maxItems = schema.maxItems ?? options.maxItems ?? 3 + const span = Math.max(maxItems - minItems, 0) + + const array: any = [] + const length = minItems + span * random() + for (let i = 0; i < length; i++) { + array.push(synthesizeValue(schema.items ?? {}, options)) + } + return array +} diff --git a/packages/criteria-synthetic-data/src/synthesize/synthesizeBoolean.ts b/packages/criteria-synthetic-data/src/synthesize/synthesizeBoolean.ts new file mode 100644 index 0000000..f520baa --- /dev/null +++ b/packages/criteria-synthetic-data/src/synthesize/synthesizeBoolean.ts @@ -0,0 +1,10 @@ +import { JSONSchemaDraft04, JSONSchemaDraft06, JSONSchemaDraft07, JSONSchemaDraft2020_12 } from '@criteria/json-schema' +import { Options } from './Options' + +type Schema = JSONSchemaDraft04 | JSONSchemaDraft06 | JSONSchemaDraft07 | JSONSchemaDraft2020_12 + +export function synthesizeBoolean(schema: Schema & object, options: Options): boolean { + const { random } = options + + return random() < 0.5 ? true : false +} diff --git a/packages/criteria-synthetic-data/src/synthesize/synthesizeNumber.ts b/packages/criteria-synthetic-data/src/synthesize/synthesizeNumber.ts new file mode 100644 index 0000000..5275bdf --- /dev/null +++ b/packages/criteria-synthetic-data/src/synthesize/synthesizeNumber.ts @@ -0,0 +1,21 @@ +import { JSONSchemaDraft04, JSONSchemaDraft06, JSONSchemaDraft07, JSONSchemaDraft2020_12 } from '@criteria/json-schema' +import { Options } from './Options' + +type Schema = JSONSchemaDraft04 | JSONSchemaDraft06 | JSONSchemaDraft07 | JSONSchemaDraft2020_12 + +export function synthesizeNumber(schema: Schema & object, options: Options): number { + const { random } = options + + const minimum = schema.minimum ?? options.minimum ?? 0 + const maximum = schema.maximum ?? options.maximum ?? minimum + 10 + const span = Math.max(maximum - minimum, 0) + + if (schema.type === 'integer') { + return minimum + Math.floor(span * random()) + } + if (schema.type === 'number') { + return minimum + Math.floor(span * 10 * random()) / 1 + } + + return 0 +} diff --git a/packages/criteria-synthetic-data/src/synthesize/synthesizeObject.ts b/packages/criteria-synthetic-data/src/synthesize/synthesizeObject.ts new file mode 100644 index 0000000..6e991b8 --- /dev/null +++ b/packages/criteria-synthetic-data/src/synthesize/synthesizeObject.ts @@ -0,0 +1,20 @@ +import { JSONSchemaDraft04, JSONSchemaDraft06, JSONSchemaDraft07, JSONSchemaDraft2020_12 } from '@criteria/json-schema' +import { synthesizeValue } from './synthesizeValue' +import { Options } from './Options' + +type Schema = JSONSchemaDraft04 | JSONSchemaDraft06 | JSONSchemaDraft07 | JSONSchemaDraft2020_12 + +export function synthesizeObject(schema: Schema & object, options: Options): object { + const { random } = options + + const object: any = {} + Object.entries(schema.properties ?? {}).forEach(([propertyName, propertySchema]) => { + const required = schema.required ? schema.required.includes(propertyName) : false + if (required) { + object[propertyName] = synthesizeValue(propertySchema, options) + } else if (random() < 0.5) { + object[propertyName] = synthesizeValue(propertySchema, options) + } + }) + return object +} diff --git a/packages/criteria-synthetic-data/src/synthesize/synthesizeString.ts b/packages/criteria-synthetic-data/src/synthesize/synthesizeString.ts new file mode 100644 index 0000000..d0ad472 --- /dev/null +++ b/packages/criteria-synthetic-data/src/synthesize/synthesizeString.ts @@ -0,0 +1,8 @@ +import { JSONSchemaDraft04, JSONSchemaDraft06, JSONSchemaDraft07, JSONSchemaDraft2020_12 } from '@criteria/json-schema' +import { Options } from './Options' + +type Schema = JSONSchemaDraft04 | JSONSchemaDraft06 | JSONSchemaDraft07 | JSONSchemaDraft2020_12 + +export function synthesizeString(schema: Schema & object, options: Options): string { + return 'string' +} diff --git a/packages/criteria-synthetic-data/src/synthesize/synthesizeValue.ts b/packages/criteria-synthetic-data/src/synthesize/synthesizeValue.ts new file mode 100644 index 0000000..f2627db --- /dev/null +++ b/packages/criteria-synthetic-data/src/synthesize/synthesizeValue.ts @@ -0,0 +1,66 @@ +import { JSONSchemaDraft04, JSONSchemaDraft06, JSONSchemaDraft07, JSONSchemaDraft2020_12 } from '@criteria/json-schema' +import { Options } from './Options' +import { synthesizeArray } from './synthesizeArray' +import { synthesizeBoolean } from './synthesizeBoolean' +import { synthesizeNumber } from './synthesizeNumber' +import { synthesizeObject } from './synthesizeObject' +import { synthesizeString } from './synthesizeString' + +type Schema = JSONSchemaDraft04 | JSONSchemaDraft06 | JSONSchemaDraft07 | JSONSchemaDraft2020_12 + +export function synthesizeValue(schema: Schema, options: Options): any { + const { random } = options + + try { + if (typeof schema === 'boolean') { + return {} + } + + if ('example' in schema) { + return schema.example + } + + if ('examples' in schema && (schema as any).examples.length > 0) { + return (schema as any).examples[0] + } + + if ('default' in schema) { + return schema.default + } + + if ('enum' in schema && Array.isArray(schema.enum)) { + return schema.enum[Math.floor(schema.enum.length * random())] + } + + if (!('type' in schema)) { + return {} + } + + if (Array.isArray(schema.type)) { + const type = schema.type[Math.floor(schema.type.length * random())] + return synthesizeValue({ ...schema, type }, options) + } + + if (schema.type === 'object') { + return synthesizeObject(schema, options) + } + if (schema.type === 'array') { + return synthesizeArray(schema, options) + } + if (schema.type === 'integer' || schema.type === 'number') { + return synthesizeNumber(schema, options) + } + if (schema.type === 'string') { + return synthesizeString(schema, options) + } + if (schema.type === 'boolean') { + return synthesizeBoolean(schema, options) + } + if (schema.type === 'null') { + return null + } + return {} + } catch { + return {} + } +} diff --git a/packages/criteria-synthetic-data/src/synthesizeDataWithJSONSchema.test.ts b/packages/criteria-synthetic-data/src/synthesizeDataWithJSONSchema.test.ts new file mode 100644 index 0000000..ced4357 --- /dev/null +++ b/packages/criteria-synthetic-data/src/synthesizeDataWithJSONSchema.test.ts @@ -0,0 +1,35 @@ +import { synthesizeDataWithJSONSchema } from '.' + +describe('synthesizeDataWithJSONSchema()', () => { + describe('with an object schema', () => { + test('synthesizes an object', () => { + const data = synthesizeDataWithJSONSchema({ + type: 'object', + required: ['name', 'age', 'verified', 'labels'], + properties: { + name: { + type: 'string' + }, + age: { + type: 'integer' + }, + verified: { + type: 'boolean' + }, + labels: { + type: 'array', + items: { + type: 'string' + } + } + } + }) + expect(data).toEqual({ + name: 'string', + age: 3, + verified: false, + labels: ['string', 'string'] + }) + }) + }) +}) diff --git a/packages/criteria-synthetic-data/src/synthesizeDataWithJSONSchema.ts b/packages/criteria-synthetic-data/src/synthesizeDataWithJSONSchema.ts new file mode 100644 index 0000000..8b6b0df --- /dev/null +++ b/packages/criteria-synthetic-data/src/synthesizeDataWithJSONSchema.ts @@ -0,0 +1,12 @@ +import { JSONSchemaDraft04, JSONSchemaDraft06, JSONSchemaDraft07, JSONSchemaDraft2020_12 } from '@criteria/json-schema' +import seedrandom from 'seedrandom' +import { synthesizeValue } from './synthesize/synthesizeValue' +import { hash } from './util/hash' + +type Schema = JSONSchemaDraft04 | JSONSchemaDraft06 | JSONSchemaDraft07 | JSONSchemaDraft2020_12 + +export function synthesizeDataWithJSONSchema(schema: Schema): any { + const seed = typeof schema === 'object' ? hash(schema) : `${schema}` + const random = seedrandom(seed) + return synthesizeValue(schema, { random }) +} diff --git a/packages/criteria-synthetic-data/src/util/hash.test.ts b/packages/criteria-synthetic-data/src/util/hash.test.ts new file mode 100644 index 0000000..22a1880 --- /dev/null +++ b/packages/criteria-synthetic-data/src/util/hash.test.ts @@ -0,0 +1,22 @@ +import { hash } from './hash' + +describe('hash()', () => { + describe('direct circular object', () => { + test('does not throw', () => { + const root: any = {} + root.self = root + + const value = hash(root) + expect(value).toBe('{"self":"[Circular]"}') + }) + }) + describe('indirect circular object', () => { + test('does not throw', () => { + const root: any = {} + root.child = { root } + + const value = hash(root) + expect(value).toBe('{"child":{"root":"[Circular]"}}') + }) + }) +}) diff --git a/packages/criteria-synthetic-data/src/util/hash.ts b/packages/criteria-synthetic-data/src/util/hash.ts new file mode 100644 index 0000000..771b2a1 --- /dev/null +++ b/packages/criteria-synthetic-data/src/util/hash.ts @@ -0,0 +1,23 @@ +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value +function getCircularReplacer() { + const ancestors = [] + return function (key, value) { + if (typeof value !== 'object' || value === null) { + return value + } + // `this` is the object that value is contained in, + // i.e., its direct parent. + while (ancestors.length > 0 && ancestors.at(-1) !== this) { + ancestors.pop() + } + if (ancestors.includes(value)) { + return '[Circular]' + } + ancestors.push(value) + return value + } +} + +export function hash(object: object): string { + return JSON.stringify(object, getCircularReplacer()) +} diff --git a/packages/criteria-synthetic-data/tsconfig.json b/packages/criteria-synthetic-data/tsconfig.json new file mode 100644 index 0000000..87f9eab --- /dev/null +++ b/packages/criteria-synthetic-data/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "lib": ["es2021"], + "esModuleInterop": true, + "moduleResolution": "node16", + "noEmit": false, + "declaration": true, + "resolveJsonModule": true, + "downlevelIteration": true, + "outDir": "dist", + "rootDir": "src" + }, + "include": ["src/**/*.ts", "test/util/jest.d.ts"] +} From 1afbb48a479414a92b95b70a4fb6926ca3a55fc9 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 15 Feb 2024 15:26:20 +1100 Subject: [PATCH 2/2] Add @criteria/synthetic-data to GitHub workflow --- .github/workflows/node.js.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 8225dae..7b7825c 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -28,5 +28,5 @@ jobs: node-version: ${{ matrix.node-version }} cache: 'npm' - run: npm ci - - run: npm run build -w packages/criteria-json-pointer -w packages/criteria-json-schema -w packages/criteria-json-schema-validation -w packages/criteria-openapi + - run: npm run build --workspaces - run: npm run test --workspaces