Skip to content

Commit

Permalink
Add "string", "number" and "date" types for JzodSchemas (JzodAttribut…
Browse files Browse the repository at this point in the history
…e), deprecate "simpleTypes" #7
  • Loading branch information
miroir-framework committed Jun 1, 2024
1 parent db0c4e0 commit b200e78
Show file tree
Hide file tree
Showing 6 changed files with 538 additions and 262 deletions.
48 changes: 36 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ npm install @miroir-framework/jzod-ts

Package `jzod-ts` provides Typescript types for Jzod schemas, and conversion functions from Jzod Schemas to Typescript types.

## Release / Migration notes

From version `0.7.0` onwards, `"simpleType"` specification is `deprecated`. It means using, instead of:

```js
{
type: "simpleType",
definition: "string"
}
```

the new, simpler definition:

```js
{
type: "string",
}
```

The `"simpleType"` specification format is de-released from version `0.8.0` onwards. The `"subDiscriminator"` for discriminated unions, which was used internally by Jzod but had no impact on the generated Zod schemas, is also de-released from version `0.8.0`.

## Principle

Instead of writing [Zod](https://github.com/colinhacks/zod) schemas:
Expand All @@ -36,8 +57,8 @@ import { jzodToZod } from "@miroir-framework/jzod";
const myJzodSchema = {
type: "object",
definition: {
a: { type: "simpleType", definition: "number", optional: true },
b: { type:"array", definition: { type:"simpleType", definition:"boolean" } }
a: { type: "number", optional: true },
b: { type:"array", definition: { type: "boolean" } }
}
}
const myZodSchema = jzodToZod(myJzodSchema);
Expand Down Expand Up @@ -68,12 +89,12 @@ const union: JzodElement = {
definition: [
{
type: "object",
definition: { a: { type: "simpleType", definition: "string" } },
, validations:[{ type: "min", parameter: 5 }, { type:"includes", parameter:"#"}] // string must be at least 5 characters long and contain '#'
definition: { a: { type: "string" } },
validations:[{ type: "min", parameter: 5 }, { type:"includes", parameter:"#"}] // string must be at least 5 characters long and contain '#'
},
{
type: "object",
definition: { b: { type: "simpleType", definition: "number", validations:[ { type: "gte", parameter: 0 }, { type:"lte", parameter:"100"} ] } }, // comprised between 0 and 100
definition: { b: { type: "number", validations:[ { type: "gte", parameter: 0 }, { type:"lte", parameter:"100"} ] } } // comprised between 0 and 100
},
],
};
Expand All @@ -94,12 +115,13 @@ For details about the (yet) unsupported Zod features, see [Limitations and Drawb
### References and Recursive types

Jzod enables references

```ts
const referencedType: JzodElement = {
type: "schemaReference",
context: {
myString: {
type: "simpleType", definition: "string"
type: "string"
},
myObject: {
type: "object",
Expand All @@ -126,8 +148,7 @@ const recursiveType: JzodElement = {
type: "union",
definition: [
{
type: "simpleType",
definition: "string",
type: "string",
},
{
type: "schemaReference",
Expand All @@ -149,8 +170,8 @@ Jzod provides Typescript Types for Jzod Schemas. One thus benefits from completi

![Completion](https://github.com/miroir-framework/jzod/blob/main/doc/ts-completion.png)


### Conversion to Zod

The function

```ts
Expand All @@ -161,27 +182,31 @@ jzodToZod(schema: JzodElement)
returns a Zod Schema corresponding to a Jzod Schema.

### Conversion From Zod

The function

```ts
import { zodToJzod } from "jzod"
zodToJzod(schema: ZodTypeAny, identifier:string)
```

returns a Jzod Schema corresponding to a Zod Schema. The `identifier` parameter gives the name of the reference to be used in case a `z.lazy()` occurs in the type.

For example:

```ts
const JzodSchema = zodToJzod(z.lazy(()=>z.any()),"test")); // equivalent to JzodSchema = {"type":"schemaReference","definition":{"relativePath":"test"}}
```

One thus has to ensure that any lazy-referenced Zod schema is available as a context. The most simple solution is to make any internal definition of a `lazy` call available as a js `const`, and use that name as context reference.

### Conversion to Typescript

In the separate [Jzod-ts](https://www.npmjs.com/package/jzod-ts) package, the function

```ts
import { jzodToZod } from "jzod-ts"
jzodToTsCode({ type: "simpleType", definition: "string" }, true/*export declaration*/, "testJzodSchema1")
jzodToTsCode({ type: "string" }, true/*export declaration*/, "testJzodSchema1")

// /* returns: */
// import { ZodType, ZodTypeAny, z } from "zod";
Expand Down Expand Up @@ -239,5 +264,4 @@ TBC

Jzod does not currently check for adequate use of validation contraint parameters with the employed Zod schema type; for example, it is allowed to pass a parameter to the number `int` constraint, which does not make sense, since this contraint only checks that the given number is an integer. The type of the parameter is not checked, either. Finally, Jzod does not allow yet to pass a custom error message (second parameter) to validators (TBD).

_Are not supported yet_: Native enums, effects, most object methods (`pick`, `omit`, `deepPartial`, and `merge`, but `extend` and `partial` are supported), other methods (`readonly`, `brand`, `pipe`) and transforms.

_Are not supported yet_: Native enums, effects, most object methods (`pick`, `omit`, `deepPartial`, and `merge`, but `extend` and `partial` are supported), other methods (`readonly`, `brand`, `pipe`) and transforms.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@miroir-framework/jzod",
"private": false,
"version": "0.6.3",
"version": "0.7.0",
"description": "The Json bootstrapper for Zod.",
"main": "dist/bundle.js",
"types": "dist/bundle.d.ts",
Expand Down
101 changes: 76 additions & 25 deletions src/JzodInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export const jzodBootstrapElementSchema: JzodReference = {
jzodBaseObject: {
type: "object",
definition: {
optional: { type: "simpleType", definition: "boolean", optional: true },
nullable: { type: "simpleType", definition: "boolean", optional: true },
extra: { type: "record", definition: { type: "simpleType", definition: "any" }, optional: true },
optional: { type: "boolean", optional: true },
nullable: { type: "boolean", optional: true },
extra: { type: "record", definition: { type: "any" }, optional: true },
},
},
jzodArray: {
Expand All @@ -42,19 +42,27 @@ export const jzodBootstrapElementSchema: JzodReference = {
extend: { type: "schemaReference", definition: { eager: true, relativePath: "jzodBaseObject" } },
definition: {
type: { type: "literal", definition: "simpleType" },
coerce: { type: "simpleType", definition: "boolean", optional: true },
coerce: { type: "boolean", optional: true },
definition: { type: "schemaReference", definition: { relativePath: "jzodEnumAttributeTypes" } },
},
},
jzodPlainAttribute: {
type: "object",
extend: { type: "schemaReference", definition: { eager: true, relativePath: "jzodBaseObject" } },
definition: {
type: { type: "schemaReference", definition: { relativePath: "jzodEnumAttributeTypes" } },
coerce: { type: "boolean", optional: true },
},
},
jzodAttributeDateValidations: {
type: "object",
definition: {
extra: { type: "record", definition: { type: "simpleType", definition: "any" }, optional: true },
extra: { type: "record", definition: { type: "any" }, optional: true },
type: {
type: "enum",
definition: ["min", "max"],
},
parameter: { type: "simpleType", definition: "any" },
parameter: { type: "any" },
},
},
jzodAttributeDateWithValidations: {
Expand All @@ -63,7 +71,19 @@ export const jzodBootstrapElementSchema: JzodReference = {
definition: {
type: { type: "literal", definition: "simpleType" },
definition: { type: "literal", definition: "date" },
coerce: { type: "simpleType", definition: "boolean", optional: true },
coerce: { type: "boolean", optional: true },
validations: {
type: "array",
definition: { type: "schemaReference", definition: { relativePath: "jzodAttributeDateValidations" } },
},
},
},
jzodAttributePlainDateWithValidations: {
type: "object",
extend: { type: "schemaReference", definition: { eager: true, relativePath: "jzodBaseObject" } },
definition: {
type: { type: "literal", definition: "date" },
coerce: { type: "boolean", optional: true },
validations: {
type: "array",
definition: { type: "schemaReference", definition: { relativePath: "jzodAttributeDateValidations" } },
Expand All @@ -73,7 +93,7 @@ export const jzodBootstrapElementSchema: JzodReference = {
jzodAttributeNumberValidations: {
type: "object",
definition: {
extra: { type: "record", definition: { type: "simpleType", definition: "any" }, optional: true },
extra: { type: "record", definition: { type: "any" }, optional: true },
type: {
type: "enum",
definition: [
Expand All @@ -91,7 +111,7 @@ export const jzodBootstrapElementSchema: JzodReference = {
"safe",
],
},
parameter: { type: "simpleType", definition: "any" },
parameter: { type: "any" },
},
},
jzodAttributeNumberWithValidations: {
Expand All @@ -100,7 +120,19 @@ export const jzodBootstrapElementSchema: JzodReference = {
definition: {
type: { type: "literal", definition: "simpleType" },
definition: { type: "literal", definition: "number" },
coerce: { type: "simpleType", definition: "boolean", optional: true },
coerce: { type: "boolean", optional: true },
validations: {
type: "array",
definition: { type: "schemaReference", definition: { relativePath: "jzodAttributeNumberValidations" } },
},
},
},
jzodAttributePlainNumberWithValidations: {
type: "object",
extend: { type: "schemaReference", definition: { eager: true, relativePath: "jzodBaseObject" } },
definition: {
type: { type: "literal", definition: "number" },
coerce: { type: "boolean", optional: true },
validations: {
type: "array",
definition: { type: "schemaReference", definition: { relativePath: "jzodAttributeNumberValidations" } },
Expand All @@ -110,7 +142,7 @@ export const jzodBootstrapElementSchema: JzodReference = {
jzodAttributeStringValidations: {
type: "object",
definition: {
extra: { type: "record", definition: { type: "simpleType", definition: "any" }, optional: true },
extra: { type: "record", definition: { type: "any" }, optional: true },
type: {
type: "enum",
definition: [
Expand All @@ -132,7 +164,7 @@ export const jzodBootstrapElementSchema: JzodReference = {
"ip",
],
},
parameter: { type: "simpleType", definition: "any" },
parameter: { type: "any" },
},
},
jzodAttributeStringWithValidations: {
Expand All @@ -141,7 +173,19 @@ export const jzodBootstrapElementSchema: JzodReference = {
definition: {
type: { type: "literal", definition: "simpleType" },
definition: { type: "literal", definition: "string" },
coerce: { type: "simpleType", definition: "boolean", optional: true },
coerce: { type: "boolean", optional: true },
validations: {
type: "array",
definition: { type: "schemaReference", definition: { relativePath: "jzodAttributeStringValidations" } },
},
},
},
jzodAttributePlainStringWithValidations: {
type: "object",
extend: { type: "schemaReference", definition: { eager: true, relativePath: "jzodBaseObject" } },
definition: {
type: { type: "literal", definition: "string" },
coerce: { type: "boolean", optional: true },
validations: {
type: "array",
definition: { type: "schemaReference", definition: { relativePath: "jzodAttributeStringValidations" } },
Expand All @@ -154,9 +198,13 @@ export const jzodBootstrapElementSchema: JzodReference = {
definition: [
{ type: "schemaReference", definition: { relativePath: "jzodArray" } },
{ type: "schemaReference", definition: { relativePath: "jzodAttribute" } },
{ type: "schemaReference", definition: { relativePath: "jzodPlainAttribute" } },
{ type: "schemaReference", definition: { relativePath: "jzodAttributeDateWithValidations" } },
{ type: "schemaReference", definition: { relativePath: "jzodAttributePlainDateWithValidations" } },
{ type: "schemaReference", definition: { relativePath: "jzodAttributeNumberWithValidations" } },
{ type: "schemaReference", definition: { relativePath: "jzodAttributePlainNumberWithValidations" } },
{ type: "schemaReference", definition: { relativePath: "jzodAttributeStringWithValidations" } },
{ type: "schemaReference", definition: { relativePath: "jzodAttributePlainStringWithValidations" } },
{ type: "schemaReference", definition: { relativePath: "jzodEnum" } },
{ type: "schemaReference", definition: { relativePath: "jzodFunction" } },
{ type: "schemaReference", definition: { relativePath: "jzodLazy" } },
Expand All @@ -177,7 +225,7 @@ export const jzodBootstrapElementSchema: JzodReference = {
extend: { type: "schemaReference", definition: { eager: true, relativePath: "jzodBaseObject" } },
definition: {
type: { type: "literal", definition: "enum" },
definition: { type: "array", definition: { type: "simpleType", definition: "string" } },
definition: { type: "array", definition: { type: "string" } },
},
},
jzodEnumAttributeTypes: {
Expand All @@ -201,18 +249,21 @@ export const jzodBootstrapElementSchema: JzodReference = {
type: "enum",
definition: [
"array",
"date",
"enum",
"function",
"lazy",
"literal",
"intersection",
"map",
"number",
"object",
"promise",
"record",
"schemaReference",
"set",
"simpleType",
"string",
"tuple",
"union",
],
Expand Down Expand Up @@ -250,10 +301,10 @@ export const jzodBootstrapElementSchema: JzodReference = {
definition: {
"type": "union",
"definition": [
{ type: "simpleType", definition: "string" },
{ type: "simpleType", definition: "number" },
{ type: "simpleType", definition: "bigint" },
{ type: "simpleType", definition: "boolean" },
{ type: "string" },
{ type: "number" },
{ type: "bigint" },
{ type: "boolean" },
]
}
},
Expand Down Expand Up @@ -299,8 +350,8 @@ export const jzodBootstrapElementSchema: JzodReference = {
],
},
type: { type: "literal", definition: "object" },
nonStrict: { type: "simpleType", definition: "boolean", optional: true },
partial: { type: "simpleType", definition: "boolean", optional: true },
nonStrict: { type: "boolean", optional: true },
partial: { type: "boolean", optional: true },
definition: {
type: "record",
definition: { type: "schemaReference", definition: { relativePath: "jzodElement" } },
Expand Down Expand Up @@ -336,10 +387,10 @@ export const jzodBootstrapElementSchema: JzodReference = {
definition: {
type: "object",
definition: {
eager: { type: "simpleType", definition: "boolean", optional: true },
partial: { type: "simpleType", definition: "boolean", optional: true },
relativePath: { type: "simpleType", definition: "string", optional: true },
absolutePath: { type: "simpleType", definition: "string", optional: true }, // absolutePath => lazy evaluation
eager: { type: "boolean", optional: true },
partial: { type: "boolean", optional: true },
relativePath: { type: "string", optional: true },
absolutePath: { type: "string", optional: true }, // absolutePath => lazy evaluation
},
},
},
Expand Down Expand Up @@ -368,7 +419,7 @@ export const jzodBootstrapElementSchema: JzodReference = {
extend: { type: "schemaReference", definition: { eager: true, relativePath: "jzodBaseObject" } },
definition: {
type: { type: "literal", definition: "union" },
discriminator: { type: "simpleType", definition: "string", optional: true },
discriminator: { type: "string", optional: true },
definition: {
type: "array",
definition: { type: "schemaReference", definition: { relativePath: "jzodElement" } },
Expand Down
Loading

0 comments on commit b200e78

Please sign in to comment.