vitalics / ajv-ts

First-class ajv typescript JSON-schema builder inspired from Zod
https://www.npmjs.com/package/ajv-ts
MIT License
39 stars 1 forks source link

[question] 100% compatibility with `zod` API? #32

Closed miguelrk closed 7 months ago

miguelrk commented 7 months ago

I understand that the ajv-ts API is a subset of the larger zod API by design, since ajv-ts should only support what is defined in JSON schema, and not the extra stuff zod adds. However, it would be great for this library to achieve 100% compatibility with the zod API in what they do have in common. This would allow using ajv-ts as a drop-in replacement for zod for the vast ecosystem of zod packages. For example, auto-form should ideally simply work:

import AutoForm, { AutoFormSubmit } from "./components/ui/auto-form";
import s from "ajv-ts";

const formSchema = s.object({ name: s.string(), age: s.number().optional() });

function App() {
  return <AutoForm formSchema={formSchema} />;
}

Expected Result:

image

However, this is throwing errors since the ajv-ts schema does not match the same zod schema (the s.object() has another form than z.object()).

Would this compatibility desired? This could be a good design decision (to align 100% with zod) in order to benefit from its vast ecosystem.

vitalics commented 7 months ago

HI, @miguelrk I'm working on the zod compatibility API. Unfortunately, not everything can be 1-1 matched. I suppose the 1.0.0 version will satisfy Zod portability API

miguelrk commented 7 months ago

First of all thanks for the quick answer! That's great to hear! Looking forward to 1.0.0.. I understand not everything can be 1-1 matched (by design) since this library should adhere to the JSON schema spec first and foremost, but aligning to the zod API I think would indeed allow integrating with the zod ecosystem, right? Not sure if it will work in practice, but at least conceptually I wouldn't see why it wouldn't...

vitalics commented 7 months ago

conceptually it can work, will see in practice, right? I'll close the issue now since #35 has already been created to track the progress of API compatibility.