-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
365 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@criteria/synthetic-data | ||
|
||
Synthesize data that conforms to schema. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
roots: ['<rootDir>/src'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './synthesizeDataWithJSONSchema' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface Options { | ||
random: () => number | ||
minimum?: number | ||
maximum?: number | ||
minItems?: number | ||
maxItems?: number | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/criteria-synthetic-data/src/synthesize/synthesizeArray.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/criteria-synthetic-data/src/synthesize/synthesizeBoolean.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/criteria-synthetic-data/src/synthesize/synthesizeNumber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/criteria-synthetic-data/src/synthesize/synthesizeObject.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/criteria-synthetic-data/src/synthesize/synthesizeString.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
} |
66 changes: 66 additions & 0 deletions
66
packages/criteria-synthetic-data/src/synthesize/synthesizeValue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 {} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/criteria-synthetic-data/src/synthesizeDataWithJSONSchema.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'] | ||
}) | ||
}) | ||
}) | ||
}) |
12 changes: 12 additions & 0 deletions
12
packages/criteria-synthetic-data/src/synthesizeDataWithJSONSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]"}}') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.