Skip to content

Add Arktype validation (via drizzle-arktype package)#4314

Merged
AndriiSherman merged 19 commits intodrizzle-team:mainfrom
L-Mario564:arktype-validation
Mar 28, 2025
Merged

Add Arktype validation (via drizzle-arktype package)#4314
AndriiSherman merged 19 commits intodrizzle-team:mainfrom
L-Mario564:arktype-validation

Conversation

@L-Mario564
Copy link
Copy Markdown
Contributor

@L-Mario564 L-Mario564 commented Mar 25, 2025

Addresses #2473.
Depends on #4097.

This PR adds a new package: drizzle-arktype. It's exactly like the other validator packages, but of course, this one is made for Arktype.

Documentation: drizzle-team/drizzle-orm-docs#530.

Example

import { pgEnum, pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-arktype';
import { type } from 'arktype';

const users = pgTable('users', {
	id: serial('id').primaryKey(),
	name: text('name').notNull(),
	email: text('email').notNull(),
	role: text('role', { enum: ['admin', 'user'] }).notNull(),
	createdAt: timestamp('created_at').notNull().defaultNow(),
});

// Schema for inserting a user - can be used to validate API requests
const insertUserSchema = createInsertSchema(users);

// Schema for updating a user - can be used to validate API requests
const updateUserSchema = createUpdateSchema(users);

// Schema for selecting a user - can be used to validate API responses
const selectUserSchema = createSelectSchema(users);

// Overriding the fields
const insertUserSchema = createInsertSchema(users, {
	role: type('string'),
});

// Refining the fields - useful if you want to change the fields before they become nullable/optional in the final schema
const insertUserSchema = createInsertSchema(users, {
	id: (schema) => schema.atLeast(1),
	role: type('string'),
});

// Usage

const isUserValid = parse(insertUserSchema, {
	name: 'John Doe',
	email: 'johndoe@test.com',
	role: 'admin',
});

Thanks to @ssalbdivad for answering some questions I had related to Arktype.

Copy link
Copy Markdown
Contributor

@ssalbdivad ssalbdivad left a comment

Choose a reason for hiding this comment

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

Awesome work, looks great overall!

There's definitely some room for type optimization, but I'd rather ship an initial version and start getting feedback ASAP if that's a concern affecting many users first.

If so, happy to help get @ark/attest set up so we can start benchmarking and optimizing inference.

Once the few concerns I mentioned are addressed, we should get this in the hands of users right away 🚢

Comment thread drizzle-arktype/src/column.ts Outdated
Comment thread drizzle-arktype/src/column.ts Outdated
Comment thread drizzle-arktype/src/column.ts Outdated
Comment thread drizzle-arktype/src/column.ts Outdated
Comment thread drizzle-arktype/src/column.ts
Comment thread drizzle-arktype/src/schema.types.internal.ts
Comment thread drizzle-arktype/src/schema.types.internal.ts
Comment thread drizzle-arktype/src/utils.ts Outdated
Comment thread drizzle-arktype/src/utils.ts Outdated
Comment thread drizzle-arktype/tests/mysql.test.ts
@L-Mario564
Copy link
Copy Markdown
Contributor Author

Awesome work, looks great overall!

There's definitely some room for type optimization, but I'd rather ship an initial version and start getting feedback ASAP if that's a concern affecting many users first.

If so, happy to help get @ark/attest set up so we can start benchmarking and optimizing inference.

Once the few concerns I mentioned are addressed, we should get this in the hands of users right away 🚢

Thanks for the feedback! Will go through most if not all of it soon.

Copy link
Copy Markdown
Contributor

@ssalbdivad ssalbdivad left a comment

Choose a reason for hiding this comment

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

Looks perfect! I mentioned a tiny change to the assertion logic but approving now as it's good to ship!

: TData extends boolean ? Type<boolean, {}>
: TData extends string ? Type<string, {}>
: Type<any, {}>;
> = IsEnumDefined<TEnumValues> extends true ? Type<Assume<TEnumValues, any[]>[number]>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!🧹

from(actual: T) {
expect(actual.equals(expected as any)).toStrictEqual(true);
expect(actual.json).toStrictEqual(expected.json);
expect(actual.expression).toStrictEqual(expected.expression);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would switch the order of these assertions so that you get the simpler one first and stricter one last.

@AndriiSherman
Copy link
Copy Markdown
Member

thanks! I'll fix tests, for some reason they are failing, and we are ready for the initial release

@ssalbdivad
Copy link
Copy Markdown
Contributor

@AndriiSherman HYPE 🙌🙌🙌

also the number of things that can fail in CI when you think nothing should is so humbling...

@AndriiSherman
Copy link
Copy Markdown
Member

@AndriiSherman HYPE 🙌🙌🙌

also the number of things that can fail in CI when you think nothing should is so humbling...

just found, should be fixed today

@Christopher96u
Copy link
Copy Markdown

I'm ready to hit npm i drizzle-arktype. Arktype is DOPE

@AndriiSherman AndriiSherman merged commit 6ab1bbe into drizzle-team:main Mar 28, 2025
1 of 25 checks passed
@AndriiSherman AndriiSherman mentioned this pull request Mar 28, 2025
AndriiSherman added a commit that referenced this pull request Apr 23, 2025
* DPRINT!!!

* Updates to neon-http for `@neondatabase/serverless@1.0.0`, when released (#4237)

* Updates for @neondatabase/serverless@1.0.0 compatibility

* Clearer comments

* Linting

* Add release notes

* Shard integration tests, parallelise attw

* Thank you pnpm 10

* Split int tests by provider

* Update Neon config, fix bash

* Restore webSocketConstructor

* Use Docker for Neon

* Use docker-compose for Neon

* Fix env var

* Run Neon HTTP tests on real DB

* Downgrade attw

* Use Bun for attw

* Split unit tests

* Fix command

* Update latest pipeline

* Fix test

* Remove await

* Split relational tests

* Disable singlestore-relational

* Remove gel-relational

* Various fixes, features bundled for v0.41.0 (#4293)

* Various fixes, features bundled for v0.41.0

* Fixed broken test case

* D1 Buffer mapping fix, tests fix

* Disabled type parsers for `neon-http` driver

* [drizzle-kit] push to d1-http failed (#4268)

* drizzle-kit and push to d1-http failed
There are two reasons:
- d1-http don't support transactions with db.run("begin/commit/rollback")
- introspections run against some CF-D1 internal tables and fails later.
  that could mitigated with a right tablesFilter: ['!_cf_KV'].
  There was a incomplete mitigation in place which now includes _cf_KV
  tables.

* chore: there are more _cf_ prefixed tables

* fix: Add escaping to sqlite pull queries

* should we pass-with-no-tests ??

* D P R I N T

* Add ci vitest config

* Bump kit, added release notes

---------

Co-authored-by: Roman <nabukhotnyiroman@gmail.com>
Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Add Arktype validation (via `drizzle-arktype` package) (#4314)

* Export (almost) everything from validator packages

* Support infinitely recursive types in JSON columns

* Fix import

* Format

* Init drizzle-arktype

* Finish drizzle-arktype

* Sync fixes from other branch

* Update READMEs

* Update README

* Type optimizations and better debugging for tests

* Add CI/CD updates for arktype package

* Bump arktype

* bump to 0.1.2

* Update turbo config

* Bump all validator packages versions

---------

Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>
Co-authored-by: David Blass <david@arktype.io>

* feat: add lua scripts for get tag and onMutate

* Added hexp + peerDeps

* fix: use hexpire option in hexpire

* fix: add hset back

* dprint

* Update pnpm lock

* fix: getByTag script

* fix: exit getByTag script if compositeTable doesn't exist

* Add all test cases for pg

* Add mysql cache functions

* 0.41 (#4416)

* fix: incorrect types for inArray (#1774)

Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>

* Pass row type parameter to @planetscale/database's execute  (#1852)

* Update session.ts

No need to cast, you can just pass a type parameter

* Update package.json

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Don't enforce type restrictions on mysqlEnum and pgEnum to be non-empty arrays (#2429)

* Removed type restriction on non-empty arrays for mysqEnum

* Removed type restriction on non-empty arrays for pgEnum

* check values argument is not an empty array for pgEnum

* fix: typings

* Add type tests

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Export mapColumnToSchema function (#2495)


Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>

* [Pg-kit] Fix malformed array literal error on indexes (#2884)

* Fix malformed array literal error on indexes

The main issue is the expression text to array conversion happening in the edited line.
Commas in an expression become delimiters and split the expression up prematurely.
Some special characters like double quotes can cause the malformed array literal errors.

The postgres function pg_get_indexdef does what the snippet above is trying to do, but safely.

* Add index introspect test

* Update pg.test.ts

Remove .only in basic index test

---------

Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>

* add infer enum type (#2552)

* Update how enums work in pg and mysql

* Remove duplicated exports, add related test (#4413)

* Remove duplicated exports, add related test

Fixes #4079

* Fix test

* chore: updating esbuild version in drizzle-kit (#4046)

* chore: updating esbuild version in drizzle-kit

* Fix build errors

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Drizzle-kit: fix recreate enums + altering data type to enums, from enums in pg (#4330)

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Skip test and try latest gel

* Add release notes

---------

Co-authored-by: James <5511220+Zamiell@users.noreply.github.com>
Co-authored-by: Ayrton <git@ayrton.be>
Co-authored-by: April Mintac Pineda <21032419+aprilmintacpineda@users.noreply.github.com>
Co-authored-by: Matthew Ary <157217+MatthewAry@users.noreply.github.com>
Co-authored-by: Kratious <Kratious@users.noreply.github.com>
Co-authored-by: Toti Muñoz <64804554+totigm@users.noreply.github.com>
Co-authored-by: Dan Kochetov <danil.kochetov@gmail.com>
Co-authored-by: Paul Marsicovetere <71470776+paulmarsicloud@users.noreply.github.com>
Co-authored-by: Aleksandr Sherman <102579553+AleksandrSherman@users.noreply.github.com>

* Update CI/CD to 22.04

* add planetscale cache tests

* Add sqlite cache

* Add singlestore db

* Add gel cache

* Fix build errors

* Fix imports

* fix pg default schema

---------

Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>
Co-authored-by: George MacKerron <george@mackerron.co.uk>
Co-authored-by: Dan Kochetov <danil.kochetov@gmail.com>
Co-authored-by: Sergey Reka <71607800+Sukairo-02@users.noreply.github.com>
Co-authored-by: Meno Abels <meno.abels@adviser.com>
Co-authored-by: Roman <nabukhotnyiroman@gmail.com>
Co-authored-by: L-Mario564 <ka.mario564@gmail.com>
Co-authored-by: David Blass <david@arktype.io>
Co-authored-by: James <5511220+Zamiell@users.noreply.github.com>
Co-authored-by: Ayrton <git@ayrton.be>
Co-authored-by: April Mintac Pineda <21032419+aprilmintacpineda@users.noreply.github.com>
Co-authored-by: Matthew Ary <157217+MatthewAry@users.noreply.github.com>
Co-authored-by: Kratious <Kratious@users.noreply.github.com>
Co-authored-by: Toti Muñoz <64804554+totigm@users.noreply.github.com>
Co-authored-by: Paul Marsicovetere <71470776+paulmarsicloud@users.noreply.github.com>
Co-authored-by: Aleksandr Sherman <102579553+AleksandrSherman@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

4 participants