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

Unsupported schema Error while applying Partial to Schema class #3113

Closed
bhvngt opened this issue Jun 28, 2024 · 4 comments
Closed

Unsupported schema Error while applying Partial to Schema class #3113

bhvngt opened this issue Jun 28, 2024 · 4 comments

Comments

@bhvngt
Copy link

bhvngt commented Jun 28, 2024

What version of Effect is running?

3.4.3

What steps can reproduce the bug?

import { Schema } from "@effect/schema";

class Todo extends Schema.Class<Todo>("Todo")({
  id: Schema.Number,
  title: Schema.String,
  completed: Schema.Boolean
}) {}

const CreateTodoParams = Todo.pipe(Schema.omit("id"));
type CreateTodoParams = Schema.Schema.Type<typeof CreateTodoParams>;

const UpdateTodoParams = Schema.partial(Todo, { exact: true }); // throws Unsupported schema schema (Transformation): (Todo (Encoded side) <-> Todo)
type UpdateTodoParams = Schema.Schema.Type<typeof UpdateTodoParams>;

What is the expected behavior?

It should not throw any error. Also it is unionising properties with undefined for UpdateTodoParams to

const UpdateTodoParams: Schema.SchemaClass<{
readonly id?: number | undefined;
readonly title?: string | undefined;
readonly completed?: boolean | undefined;
}, {
readonly id?: number | undefined;
readonly title?: string | undefined;
readonly completed?: boolean | undefined;
}, never>

even though {exact: true} is passed. My understanding is that it should opt-out of that.

What do you see instead?

See following error in the terminal

Unsupported schema
schema (Transformation): (Todo (Encoded side) <-> Todo)
	at Module.partial ./node_modules/.pnpm/@effect+schema@0.68.10_effect@3.4.3/node_modules/@effect/schema/src/AST.ts:2245
	at Module.apply ./node_modules/.pnpm/@effect+schema@0.68.10_effect@3.4.3/node_modules/@effect/schema/src/Schema.ts:2855
	at Module.<anonymous> ./node_modules/.pnpm/effect@3.4.3/node_modules/effect/src/Function.ts:85

Additional information

@bhvngt bhvngt added the bug Something isn't working label Jun 28, 2024
@gcanti
Copy link
Contributor

gcanti commented Jun 28, 2024

It raises an error because the Schema.Class API implicitly defines a transformation, specifically from:

Schema.Struct({
  id: Schema.Number,
  title: Schema.String,
  completed: Schema.Boolean
})

to Todo in this case, and transformations are not supported by Schema.partial.

However, this should work:

const UpdateTodoParams = Schema.partial(Schema.Struct(Todo.fields), { exact: true })

@gcanti gcanti removed the bug Something isn't working label Jun 28, 2024
@bhvngt
Copy link
Author

bhvngt commented Jun 28, 2024

Got it. Thank you for your reply.

Tried your solution and that stopped the error from occurring. However, I was expecting the non-unionised type, since exact: true is passed as an option to opt out of unionisation. However, types are still unionised. Am I missing something here?

image

@gcanti
Copy link
Contributor

gcanti commented Jun 28, 2024

Do you have the exactOptionalPropertyTypes option enabled in your tsconfig?

@bhvngt
Copy link
Author

bhvngt commented Jun 28, 2024

Yep that was the missing piece. Sorry, I overlooked the documentation here.

Thanks you very much for this really powerful and composable library.

@bhvngt bhvngt closed this as completed Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants