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

Updated zod to schema comparison: added mention of Schema.URL #4346

Closed
wants to merge 1 commit into from

Conversation

nikelborm
Copy link
Contributor

@nikelborm nikelborm commented Jan 27, 2025

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

schema/comparisons.md said that there is no alternative in Effect to zod's z.string().url(), while @KhraksMamtsov added support in #3889

Copy link

changeset-bot bot commented Jan 27, 2025

⚠️ No Changeset found

Latest commit: 999a5f6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@@ -221,7 +221,7 @@ S.String.pipe(S.maxLength(5))
S.String.pipe(S.minLength(5))
S.String.pipe(S.length(5))
// S.string().email() // No equivalent
// S.string().url() // No equivalent
S.URL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK S.URL is not equivalent, z.string().url() just validates a string:

import z from "zod"

console.log(typeof z.string().url().parse("https://example.com"))
// string

Copy link
Contributor Author

@nikelborm nikelborm Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schema.Date is also not an exact equivalent, but this fact didn't prevent it from being mentioned as an equivalent a few lines later:

schema/comparisons.md L286-L307
### Dates

Zod

```ts
const date = z.string().date()

date.parse("2020-01-01") // pass
date.parse("2020-1-1") // fail
date.parse("2020-01-32") // fail
```

Schema

```ts
import { Schema as S } from "effect"

S.decodeUnknownSync(S.Date)("2020-01-01") // pass
S.decodeUnknownSync(S.Date)("2020-1-1") // pass
S.decodeUnknownSync(S.Date)("2020-01-32") // fail
```
import z from 'zod';

console.log(typeof z.string().date().parse('2020-01-01'));
// string

console.log(typeof Schema.decodeUnknownSync(Schema.Date)('2020-01-01'));
// object (Date)

And I think that's fine that they're not exactly equivalent and still mentioned because there's a value in it. When I first saw that there's no equivalent of URL validation, I was really confused, spent time reimplementing Schema.URL, and only after I was done, I noticed it actually existed and I created absolutely the same thing.

I don't want this to happen to anybody else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't claim that they are equivalent, it shows what can be used in Schema when the input is a string. However, without proper description, that section is confusing and should be reorganized. Furthermore, since these docs were written, some things have changed (e.g., Schema.URL didn’t exist at the time). IMO, the entire document needs to be refactored

Copy link
Contributor Author

@nikelborm nikelborm Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed it claimed they were equivalent because it didn't claim otherwise. In cases where Effect had no equivalent, it was stated as:

### Times

No equivalent.

### IP addresses

No equivalent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's not your fault, the document needs to be restructured, it's currently confusing

@nikelborm
Copy link
Contributor Author

Thank you @gcanti ☺️❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants