Skip to content

Commit

Permalink
Seeded mock and assert with return (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrconley committed Aug 23, 2024
1 parent 29815c5 commit 076fec7
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-ants-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nrfcloud/ts-json-schema-transformer": minor
---

seeded mock
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,20 @@ Generates an AJV validator function for the given type.
The generic type parameter is the type you want to generate a validator for, and the single input to the function.
This function call is replaced by the generated validator at compile time.

#### `getMockObject<T>(): T`
#### `getMockObject<T, Seed>(): T`

Generate a mock object for the given type.
Should support all formats as well as other constraints.
You can optionally specify a seed for the random number generator as the second parameter.

#### `assertValid<T>(obj: unknown): asserts obj is T`

Validates that a given object satisfies the constraints defined in the given generic type parameter's schema. The method will throw an error if validation fails. This function call is replaced a wrapped validator method at compile time.

#### `assert<T>(obj: unknown): T`

Very similar to `assertValid` but returns the passed object instead of narrowing the type.

### JSDoc Tags

You can add JSDoc tags to your interfaces and types to add additional schema information.
Expand Down Expand Up @@ -394,6 +399,10 @@ Note that not all options are exposed, though more could be added in the future.

// Whether properties should be sorted (stable)
"sortProps": true,

// Explicitly set the seed used for mock data generation.
// You can disable seeding by setting this false
"seed": "this is a seed"
}
]
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@types/jest": "^29.5.11",
"@types/json-schema": "^7.0.15",
"@types/node": "^18.19.3",
"@types/seedrandom": "^3.0.8",
"dprint": "^0.35.4",
"husky": "^8.0.3",
"jest": "^29.7.0",
Expand All @@ -84,7 +85,8 @@
"@apidevtools/json-schema-ref-parser": "^11.7.0",
"ajv": "^8.12.0",
"esbuild": "^0.23.0",
"json-schema-faker": "npm:@jfconley/json-schema-faker@0.5.0-rcv.48",
"json-schema-faker": "^0.5.6",
"seedrandom": "^3.0.5",
"ts-json-schema-generator": "^2.4.0-next.2"
}
}
102 changes: 58 additions & 44 deletions pnpm-lock.yaml

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

15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function getValidator<T>(): ValidateFunction<T> {
* Get a mock object for the provided type
* @transformer ts-json-schema-transformer
*/
export function getMockObject<T>(): T {
export function getMockObject<T, const S extends string = "none">(): IfEquals<S, string, never, T> {
throw new Error("Not implemented. Did you forget to run the transformer?");
}

Expand All @@ -32,6 +32,14 @@ export function assertValid<T = never, U extends T = T>(_obj: unknown): asserts
throw new Error("Not implemented. Did you forget to run the transformer?");
}

/**
* Assert that an object is valid and returns the object back
* @transformer ts-json-schema-transformer
*/
export function assert<T = never, U extends T = T>(_obj: unknown): U {
throw new Error("Not implemented. Did you forget to run the transformer?");
}

export class ValidationError extends Error {
}

Expand All @@ -46,3 +54,8 @@ export function validationAssertion(validator: ValidateFunction<unknown>, obj: u
}
return obj;
}

type Exact<T, U> = IfEquals<T, U, T, never>;
type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T ? 1 : 2) extends (<G>() => G extends U ? 1 : 2) ? Y
: N;
type NotAny<T> = 0 extends (1 & T) ? never : T;
9 changes: 8 additions & 1 deletion src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export interface IProject {
options: {
schema: SchemaConfig;
validation: AJVOptions;
mock: {
seed: string | false;
};
};
checker: ts.TypeChecker;
schemaGenerator: SchemaGenerator;
Expand All @@ -23,6 +26,8 @@ export type SchemaConfig = Pick<
"sortProps" | "expose" | "jsDoc" | "strictTuples" | "encodeRefs" | "additionalProperties"
>;

export const DEFAULT_SEED = "emerson";

export const AJV_DEFAULTS = {
useDefaults: true,
coerceTypes: false,
Expand All @@ -48,4 +53,6 @@ export const SCHEMA_DEFAULTS = {
functions: "fail",
} as const satisfies Config;

export type IOptions = AJVOptions & SchemaConfig;
export type IOptions = AJVOptions & SchemaConfig & {
seed?: string | false;
};
Loading

0 comments on commit 076fec7

Please sign in to comment.