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

Validations error and wrong generation when referencing a namespace #208

Open
uribrod opened this issue Feb 21, 2024 · 5 comments
Open

Validations error and wrong generation when referencing a namespace #208

uribrod opened this issue Feb 21, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@uribrod
Copy link

uribrod commented Feb 21, 2024

Getting validation error when trying to generate from type with namespace

Input

/**
 * DO NOT EDIT!
 * THIS IS AN AUTO-GENERATED FILE CREATED FROM AN AVRO SCHEMA.
 **/
/* eslint-disable @typescript-eslint/no-namespace */

export type AuditingProcessed = ComRiskifiedAvroAuditingprocessedV3.AuditingProcessed;

export namespace ComRiskifiedAvroAuditingprocessedV3 {
    export const ChangeTypeName = 'com.riskified.avro.auditingprocessed.v3.ChangeType';
    export type ChangeType = 'Create' | 'Delete' | 'Update';
    export const AuditingProcessedName = 'com.riskified.avro.auditingprocessed.v3.AuditingProcessed';
    export interface AuditingProcessed {
        entityType: string;
        entityId: string;
        adminUserEmail: string;
        createdAt: number;
        changeType: ComRiskifiedAvroAuditingprocessedV3.ChangeType;
        shopUrl: null | string;
        triggeringSource: string;
        beforeState: string;
        afterState: string;
        description: string;
        diff: string;
        auditingProcessedHash: string;
    }
}

Expected output

// Expected Zod schemas

Actual output

// Generated by ts-to-zod
import { z } from 'zod';

export const auditingProcessedSchema = z.literal(ComRiskifiedAvroAuditingprocessedV3.AuditingProcessed);

export const comRiskifiedAvroAuditingprocessedV3ChangeTypeSchema = z.union([
    z.literal('Create'),
    z.literal('Delete'),
    z.literal('Update'),
]);

export const comRiskifiedAvroAuditingprocessedV3AuditingProcessedSchema = z.object({
    entityType: z.string(),
    entityId: z.string(),
    adminUserEmail: z.string(),
    createdAt: z.number(),
    changeType: z.literal(ComRiskifiedAvroAuditingprocessedV3.ChangeType),
    shopUrl: z.string().nullable(),
    triggeringSource: z.string(),
    beforeState: z.string(),
    afterState: z.string(),
    description: z.string(),
    diff: z.string(),
    auditingProcessedHash: z.string(),
});
validating generated types
    Error: 'comRiskifiedAvroAuditingprocessedV3AuditingProcessedSchema' is not compatible with 'ComRiskifiedAvroAuditingprocessedV3AuditingProcessed':
    Argument of type '{ [x: string]: any; entityType?: string | undefined; entityId?: string | undefined; adminUserEmail?: string | undefined; createdAt?: number | undefined; changeType?: any; shopUrl?: string | null | undefined; ... 5 more ...; 
 ›   auditingProcessedHash?: string | undefined; }' is not assignable to parameter of type 'ComRiskifiedAvroAuditingprocessedV3AuditingProcessed'.
      Types of property 'entityType' are incompatible.
        Type 'string | undefined' is not assignable to type 'string'.
          Type 'undefined' is not assignable to type 'string'.

Versions

  • Typescript: v3.7.1
  • Zod: v3.22.4
@schiller-manuel
Copy link
Collaborator

Please fill in the "Expected output". Is the problem "just" the validation error? or something else?

@schiller-manuel
Copy link
Collaborator

schiller-manuel commented Feb 28, 2024

does this still occur in v.3.7.3?

@uribrod
Copy link
Author

uribrod commented Mar 3, 2024

@schiller-manuel yes still happens also in v 3.7.3, the issue is just the validation error

@schiller-manuel
Copy link
Collaborator

and did this work with previous versions?

@tvillaren
Copy link
Collaborator

Looking at the actual output, it seems to me it should rather be:

// Generated by ts-to-zod
import { z } from "zod";

export const comRiskifiedAvroAuditingprocessedV3ChangeTypeSchema = z.union([
  z.literal("Create"),
  z.literal("Delete"),
  z.literal("Update"),
]);

export const comRiskifiedAvroAuditingprocessedV3AuditingProcessedSchema =
  z.object({
    entityType: z.string(),
    entityId: z.string(),
    adminUserEmail: z.string(),
    createdAt: z.number(),
    changeType: comRiskifiedAvroAuditingprocessedV3ChangeTypeSchema,
    shopUrl: z.string().nullable(),
    triggeringSource: z.string(),
    beforeState: z.string(),
    afterState: z.string(),
    description: z.string(),
    diff: z.string(),
    auditingProcessedHash: z.string(),
  });

export const auditingProcessedSchema =
  comRiskifiedAvroAuditingprocessedV3AuditingProcessedSchema;

in order to be valid.

The export const auditingProcessedSchema = z.literal(ComRiskifiedAvroAuditingprocessedV3.AuditingProcessed); would not work as their is no ComRiskifiedAvroAuditingprocessedV3 in the output file (nor import).
Was it imported in previous version?

@tvillaren tvillaren added the bug Something isn't working label Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants