mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
Created types for json-schema-faker npm library (#45189)
* Created types for json-schema-faker npm library * fixed some errors. * json-schema-faker removed redundant keywords and added export = * json-schema-faker changed export format * Fixed module call arguments * removed unnecessary reference * Update types/json-schema-faker/index.d.ts Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <peterblazejewicz@users.noreply.github.com> * Update types/json-schema-faker/index.d.ts Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <peterblazejewicz@users.noreply.github.com> * Update types/json-schema-faker/tsconfig.json Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <peterblazejewicz@users.noreply.github.com> * Update types/json-schema-faker/index.d.ts Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <peterblazejewicz@users.noreply.github.com> * Fixed imports in test. * Removed unused interface from tests Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <peterblazejewicz@users.noreply.github.com>
This commit is contained in:
parent
ccb0081a24
commit
3abbf3159f
67
types/json-schema-faker/index.d.ts
vendored
Normal file
67
types/json-schema-faker/index.d.ts
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
// Type definitions for json-schema-faker 0.5
|
||||
// Project: https://github.com/json-schema-faker/json-schema-faker
|
||||
// Definitions by: Charles Desbiens <https://github.com/baremaximum>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 3.8
|
||||
|
||||
import { JSONSchema4, JSONSchema6, JSONSchema7 } from 'json-schema';
|
||||
|
||||
declare namespace jsf {
|
||||
const version: string;
|
||||
function format(nameOrFormatMap?: NameOrFormatMap, callback?: (schema?: Schema) => void): any;
|
||||
function extend(name: string, cb: () => void): any;
|
||||
function define(name: string, cb: () => void): any;
|
||||
function reset(name: string): any;
|
||||
function locate(name: string): any;
|
||||
function generate(schema: Schema, refs?: string | Schema[]): any[];
|
||||
function resolve(schema: Schema, refs?: string | Schema[], cwd?: string): Promise<any[]>;
|
||||
function option(option: string | OptionInputObject, value?: any): any;
|
||||
|
||||
// jsf.random
|
||||
namespace random {
|
||||
function randexp(value: string): string | number;
|
||||
function pick<T>(collection: T[]): T;
|
||||
function date(step: string): Date;
|
||||
function shuffle<T>(collection: T[]): T[];
|
||||
function number(min?: number, max?: number, defMin?: number, defMax?: number, hasPrecision?: boolean): number;
|
||||
}
|
||||
|
||||
type Schema = JSONSchema4 | JSONSchema6 | JSONSchema7;
|
||||
type OptionInputObject = Partial<
|
||||
{
|
||||
[option in jsfOptions]: any;
|
||||
}
|
||||
>;
|
||||
type NameOrFormatMap = string | { name: string; callback: (schema?: Schema) => void };
|
||||
|
||||
// List of all valid registerable options.
|
||||
type jsfOptions =
|
||||
| 'defaultInvalidTypeProduct'
|
||||
| 'defaultRandExpMax'
|
||||
| 'ignoreProperties'
|
||||
| 'ignoreMissingRefs'
|
||||
| 'failOnInvalidTypes'
|
||||
| 'failOnInvalidFormat'
|
||||
| 'alwaysFakeOptionals'
|
||||
| 'optionalsProbability'
|
||||
| 'fixedProbabilities'
|
||||
| 'useExamplesValue'
|
||||
| 'useDefaultValue'
|
||||
| 'requiredOnly'
|
||||
| 'minItems'
|
||||
| 'maxItems'
|
||||
| 'minLength'
|
||||
| 'maxLength'
|
||||
| 'refDepthMin'
|
||||
| 'refDepthMax'
|
||||
| 'resolveJsonPath'
|
||||
| 'reuseProperties'
|
||||
| 'fillProperties'
|
||||
| 'random'
|
||||
| 'replaceEmptyByRandomValue';
|
||||
}
|
||||
|
||||
/** @deprecated calling JsonSchemaFaker() is deprecated, call either .generate() or .resolve()' */
|
||||
declare function jsf(schema: jsf.Schema, refs?: string | jsf.Schema[]): any;
|
||||
export as namespace jsf;
|
||||
export = jsf;
|
||||
127
types/json-schema-faker/json-schema-faker-tests.ts
Normal file
127
types/json-schema-faker/json-schema-faker-tests.ts
Normal file
@ -0,0 +1,127 @@
|
||||
/// <reference types="node" />
|
||||
import jsf = require('json-schema-faker');
|
||||
import { Chance } from 'chance';
|
||||
import { Schema } from 'json-schema-faker';
|
||||
|
||||
// custom chance extension
|
||||
jsf.extend('chance', () => {
|
||||
const chance = new Chance();
|
||||
chance.mixin({
|
||||
user: () => {
|
||||
return {
|
||||
first: chance.first(),
|
||||
last: chance.last(),
|
||||
email: chance.email(),
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
return chance;
|
||||
});
|
||||
|
||||
// extend with faker
|
||||
jsf.extend('faker', () => require('faker'));
|
||||
|
||||
// custom faker extension
|
||||
jsf.extend('faker', () => {
|
||||
const faker = require('faker/locale/de');
|
||||
|
||||
faker.mixin = (namespace: string, fnObject: any) => {
|
||||
faker[namespace] = fnObject;
|
||||
};
|
||||
|
||||
faker.mixin('custom', {
|
||||
statement(length: number) {
|
||||
return `${faker.name.firstName()} has ${faker.finance.amount()} on ${faker.finance.account(length)}.`;
|
||||
},
|
||||
});
|
||||
|
||||
return faker;
|
||||
});
|
||||
|
||||
// add custom format using arg input format
|
||||
jsf.format('semver', () => jsf.random.randexp('\\d\\.\\d\\.[1-9]\\d?'));
|
||||
|
||||
// add custom format using object input format
|
||||
jsf.format({ name: 'semver2', callback: () => jsf.random.randexp('\\d\\.\\d\\.[1-9]\\d?') });
|
||||
|
||||
// register an option using arg input format
|
||||
jsf.option('failOnInvalidTypes', false);
|
||||
|
||||
// register an option using object input format
|
||||
jsf.option({ alwaysFakeOptionals: true });
|
||||
const testSchema: Schema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
user: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: {
|
||||
$ref: '#/definitions/positiveInt',
|
||||
},
|
||||
name: {
|
||||
type: 'string',
|
||||
faker: 'name.findName',
|
||||
},
|
||||
email: {
|
||||
type: 'string',
|
||||
format: 'email',
|
||||
faker: 'internet.email',
|
||||
},
|
||||
version: {
|
||||
type: 'string',
|
||||
format: 'semver',
|
||||
},
|
||||
version2: {
|
||||
type: 'string',
|
||||
format: 'semver2',
|
||||
},
|
||||
reffed: {
|
||||
$ref: '#/otherSchema',
|
||||
},
|
||||
subuser: {
|
||||
type: 'object',
|
||||
chance: 'user',
|
||||
},
|
||||
},
|
||||
required: ['id', 'name', 'email', 'version', 'version2', 'reffed', 'randomname'],
|
||||
},
|
||||
},
|
||||
required: ['user'],
|
||||
definitions: {
|
||||
positiveInt: {
|
||||
type: 'integer',
|
||||
minimum: 0,
|
||||
exclusiveMinimum: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const testRef: jsf.Schema[] = [
|
||||
{
|
||||
id: 'otherSchema',
|
||||
type: 'string',
|
||||
},
|
||||
];
|
||||
|
||||
// generate
|
||||
const generated = jsf.generate(testSchema);
|
||||
generated.forEach(testItem);
|
||||
|
||||
// resolve
|
||||
jsf.resolve(testSchema, testRef).then(res => {
|
||||
res.forEach(testItem);
|
||||
});
|
||||
|
||||
// test that item has all expected properties. If not, throw error.
|
||||
function testItem(item: any): void {
|
||||
const user = item.user;
|
||||
if (!item.hasOwnProperty('user')) throw new Error();
|
||||
if (!user.hasOwnProperty('reffed')) throw new Error();
|
||||
if (!user.hasOwnProperty('version')) throw new Error();
|
||||
if (!user.hasOwnProperty('version2')) throw new Error();
|
||||
if (!user.hasOwnProperty('name')) throw new Error();
|
||||
if (!user.hasOwnProperty('email')) throw new Error();
|
||||
if (!user.hasOwnProperty('subuser')) throw new Error();
|
||||
if (!user.hasOwnProperty('id')) throw new Error();
|
||||
}
|
||||
16
types/json-schema-faker/tsconfig.json
Normal file
16
types/json-schema-faker/tsconfig.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": ["es6"],
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"noImplicitThis": true,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": ["../"],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": ["index.d.ts", "json-schema-faker-tests.ts"]
|
||||
}
|
||||
1
types/json-schema-faker/tslint.json
Normal file
1
types/json-schema-faker/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user