Skip to content

Commit

Permalink
Add @criteria/synthetic-data
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmosc committed Feb 15, 2024
1 parent 0045e6b commit 267ab0e
Show file tree
Hide file tree
Showing 18 changed files with 365 additions and 0 deletions.
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/criteria-synthetic-data/LICENSE
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.
3 changes: 3 additions & 0 deletions packages/criteria-synthetic-data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@criteria/synthetic-data

Synthesize data that conforms to schema.
6 changes: 6 additions & 0 deletions packages/criteria-synthetic-data/jest.config.js
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']
}
48 changes: 48 additions & 0 deletions packages/criteria-synthetic-data/package.json
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"
}
}
1 change: 1 addition & 0 deletions packages/criteria-synthetic-data/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './synthesizeDataWithJSONSchema'
7 changes: 7 additions & 0 deletions packages/criteria-synthetic-data/src/synthesize/Options.ts
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 packages/criteria-synthetic-data/src/synthesize/synthesizeArray.ts
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
}
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
}
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
}
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
}
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 packages/criteria-synthetic-data/src/synthesize/synthesizeValue.ts
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 {}
}
}
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']
})
})
})
})
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 })
}
22 changes: 22 additions & 0 deletions packages/criteria-synthetic-data/src/util/hash.test.ts
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]"}}')
})
})
})
Loading

0 comments on commit 267ab0e

Please sign in to comment.