Skip to content

Commit

Permalink
Merge branch 'main' into joco/improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrconley committed Aug 24, 2024
2 parents 3c02565 + b2f3d65 commit 6219a35
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @nrfcloud/ts-json-schema-transformer

## 2.1.0

### Minor Changes

- 076fec7: seeded mock

## 2.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nrfcloud/ts-json-schema-transformer",
"version": "2.0.1",
"version": "2.1.0",
"description": "A TypeScript transformer that generates JSON schemas and validators from TypeScript interfaces",
"type": "commonjs",
"main": "dist/index.js",
Expand Down
41 changes: 41 additions & 0 deletions src/transformers/get-mock-object-transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { JSONSchema7 } from "json-schema";

import seedrandom from "seedrandom";
import { ts as schemaGeneratorTs } from "ts-json-schema-generator";
import * as ts from "typescript";
import { IProject } from "../project.js";
import { addFormatsJsf, convertValueToExpression, derefJSONSchemaRoot, getGenericArg } from "./utils.js";

// @ts-expect-error Using an esm type import
type jsf = import("json-schema-faker");
const jsf = require("json-schema-faker");

addFormatsJsf();

export abstract class GetMockObjectTransformer {
public static transform(project: IProject, expression: ts.CallExpression): ts.Node {
const [type, node] = getGenericArg(project, expression);
const [, seedNode, seedProvided] = getGenericArg(project, expression, 1);

if (type.isTypeParameter()) {
throw new Error(
`Error on getSchema: non-specified generic argument.`,
);
}

let specifiedSeed: string | undefined = undefined;
if (
seedProvided && seedNode != undefined && ts.isLiteralTypeNode(seedNode) && ts.isStringLiteral(seedNode.literal)
) {
specifiedSeed = seedNode.literal.text;
}
if (project.options.mock.seed !== false || specifiedSeed) {
jsf.option("random", seedrandom(project.options.mock.seed || specifiedSeed));
} else {
jsf.option("random", Math.random);
}

const schema = derefJSONSchemaRoot(project.schemaGenerator.createSchemaFromNodes([node as schemaGeneratorTs.Node]));
return convertValueToExpression(jsf.generate(schema as JSONSchema7));
}
}
6 changes: 6 additions & 0 deletions tests/src/assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,10 @@ describe("Simple Assert Test", () => {
expect(result).toMatchObject({ foo: "bar" });
});
});

it("Should assert and return object", () => {
const test = { foo: "bar" };
const result = assert<SimpleType>(test);
expect(test).toMatchObject(result);
});
});

0 comments on commit 6219a35

Please sign in to comment.