Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using const for literals in documentation #1651

Merged
merged 12 commits into from
Apr 1, 2024
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@

## Version 17

### v17.6.0

- Using `const` property for depicting `z.literal()` in the generated documentation;
- Fixed possibly invalid values of `type` property when depicting `z.literal()`, `z.enum()` and `z.nativeEnum()`.

```yaml
# z.literal("success")
before:
type: string
enum: # replaced
- success
after:
type: string
const: success
```

```yaml
# z.literal(null)
before:
type: object # fixed
enum:
- null
after:
type: null
const: null
```

### v17.5.0

- Depicting the `.rest()` part of `z.tuple()` in the generated `Documentation`:
Expand Down
39 changes: 13 additions & 26 deletions example/example.documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ paths:
properties:
status:
type: string
enum:
- success
const: success
data:
type: object
properties:
Expand Down Expand Up @@ -69,8 +68,7 @@ paths:
properties:
status:
type: string
enum:
- error
const: error
error:
type: object
properties:
Expand Down Expand Up @@ -147,8 +145,7 @@ paths:
properties:
status:
type: string
enum:
- success
const: success
data:
type: object
properties:
Expand Down Expand Up @@ -185,8 +182,7 @@ paths:
properties:
status:
type: string
enum:
- error
const: error
error:
type: object
properties:
Expand Down Expand Up @@ -230,8 +226,7 @@ paths:
properties:
status:
type: string
enum:
- created
const: created
data:
type: object
properties:
Expand All @@ -254,8 +249,7 @@ paths:
properties:
status:
type: string
enum:
- created
const: created
data:
type: object
properties:
Expand All @@ -278,8 +272,7 @@ paths:
properties:
status:
type: string
enum:
- error
const: error
reason:
type: string
required:
Expand All @@ -294,8 +287,7 @@ paths:
properties:
status:
type: string
enum:
- exists
const: exists
id:
type: integer
format: int64
Expand All @@ -313,8 +305,7 @@ paths:
properties:
status:
type: string
enum:
- error
const: error
reason:
type: string
required:
Expand Down Expand Up @@ -440,8 +431,7 @@ paths:
properties:
status:
type: string
enum:
- success
const: success
data:
type: object
properties:
Expand Down Expand Up @@ -478,8 +468,7 @@ paths:
properties:
status:
type: string
enum:
- error
const: error
error:
type: object
properties:
Expand Down Expand Up @@ -518,8 +507,7 @@ paths:
properties:
status:
type: string
enum:
- success
const: success
data:
type: object
properties:
Expand All @@ -542,8 +530,7 @@ paths:
properties:
status:
type: string
enum:
- error
const: error
error:
type: object
properties:
Expand Down
28 changes: 23 additions & 5 deletions src/documentation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
both,
complement,
concat,
type as detectType,
filter,
fromPairs,
has,
Expand All @@ -32,6 +33,7 @@ import {
pluck,
range,
reject,
toLower,
union,
when,
xprod,
Expand Down Expand Up @@ -257,18 +259,34 @@ export const depictNullable: Depicter<z.ZodNullable<z.ZodTypeAny>> = ({
return nested;
};

const getSupportedType = (value: unknown): SchemaObjectType | undefined => {
const detected = toLower(detectType(value)); // toLower is typed well unlike .toLowerCase()
const isSupported =
detected === "number" ||
detected === "string" ||
detected === "boolean" ||
detected === "object" ||
detected === "null" ||
detected === "array";
return typeof value === "bigint"
? "integer"
: isSupported
? detected
: undefined;
};

export const depictEnum: Depicter<
z.ZodEnum<[string, ...string[]]> | z.ZodNativeEnum<any> // keeping "any" for ZodNativeEnum as compatibility fix
> = ({ schema }) => ({
type: typeof Object.values(schema.enum)[0] as "string" | "number",
type: getSupportedType(Object.values(schema.enum)[0]),
enum: Object.values(schema.enum),
});

export const depictLiteral: Depicter<z.ZodLiteral<unknown>> = ({
schema: { value },
}) => ({
type: typeof value as "string" | "number" | "boolean",
enum: [value],
type: getSupportedType(value), // constructor allows z.Primitive only, but ZodLiteral does not have that constrant
const: value,
});

export const depictObject: Depicter<z.ZodObject<z.ZodRawShape>> = ({
Expand Down Expand Up @@ -539,10 +557,10 @@ export const depictObjectProperties = ({
}: Parameters<Depicter<z.ZodObject<z.ZodRawShape>>>[0]) => map(next, shape);

const makeSample = (depicted: SchemaObject) => {
const type = (
const firstType = (
Array.isArray(depicted.type) ? depicted.type[0] : depicted.type
) as keyof typeof samples;
return samples?.[type];
return samples?.[firstType];
};

const makeNullableType = (prev: SchemaObject): SchemaObjectType[] => {
Expand Down
47 changes: 28 additions & 19 deletions tests/unit/__snapshots__/documentation-helpers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ exports[`Documentation helpers > depictDiscriminatedUnion() > should wrap next d
"format": "any",
},
"status": {
"enum": [
"success",
],
"const": "success",
"type": "string",
},
},
Expand All @@ -128,9 +126,7 @@ exports[`Documentation helpers > depictDiscriminatedUnion() > should wrap next d
"type": "object",
},
"status": {
"enum": [
"error",
],
"const": "error",
"type": "string",
},
},
Expand Down Expand Up @@ -317,9 +313,7 @@ exports[`Documentation helpers > depictIntersection() > should fall back to allO
"type": "number",
},
{
"enum": [
5,
],
"const": 5,
"type": "number",
},
],
Expand Down Expand Up @@ -392,9 +386,7 @@ exports[`Documentation helpers > depictIntersection() > should maintain uniquene
{
"properties": {
"test": {
"enum": [
5,
],
"const": 5,
"format": "double",
"maximum": 1.7976931348623157e+308,
"minimum": -1.7976931348623157e+308,
Expand Down Expand Up @@ -495,15 +487,34 @@ exports[`Documentation helpers > depictLazy > should handle circular references
}
`;

exports[`Documentation helpers > depictLiteral() > should set type and involve enum property 1`] = `
exports[`Documentation helpers > depictLiteral() > should set type and involve const property 0 1`] = `
{
"enum": [
"testing",
],
"const": "testng",
"type": "string",
}
`;

exports[`Documentation helpers > depictLiteral() > should set type and involve const property 1 1`] = `
{
"const": null,
"type": "null",
}
`;

exports[`Documentation helpers > depictLiteral() > should set type and involve const property 2 1`] = `
{
"const": 123n,
"type": "integer",
}
`;

exports[`Documentation helpers > depictLiteral() > should set type and involve const property 3 1`] = `
{
"const": Symbol(test),
"type": undefined,
}
`;

exports[`Documentation helpers > depictNull() > should give type:null 1`] = `
{
"type": "null",
Expand Down Expand Up @@ -1248,9 +1259,7 @@ exports[`Documentation helpers > depictTuple() > should utilize prefixItems and
"type": "string",
},
{
"enum": [
"test",
],
"const": "test",
"type": "string",
},
],
Expand Down
Loading
Loading