grantila / suretype

Typesafe JSON (Schema) validator
502 stars 9 forks source link

How to convert z.enum to use sure type? #8

Closed Kishimotovn closed 3 years ago

Kishimotovn commented 3 years ago

Hi, I want to try and convert our code which is currently use zod to a more performance parsing framework. However I'm having problem try to replace z.enum(['a', 'b', 'c']) to any equivalent validators on suretype. I would appreciate it if you can add some guidance on how to do this. Thanks

grantila commented 3 years ago

I think that would be equivalent to v.string().enum('a', 'b', 'c'), try that.

Kishimotovn commented 3 years ago

@grantila thanks, what about z.record(type)? Do you have equivalent for that?

jaymoid commented 3 years ago

If it helps, an example of what we would be trying to validate might look like:

{
    id48359: {
        name: "Simon",
        age: 42
    },
    id34598: {
        name: "Garfunkel",
        age: 43
    }
}

many thanks in advance!

grantila commented 3 years ago

Are the keys known? If so, something like:

v.object({
    id48359: v.object({
        name: v.string().required(),
        age: v.number().required(),
    }),
    // id34598 etc...
})

otherwise, if the keys are "any", make an empty object, and allow additional properties:

v.object({})
.additional(v.object({
    name: v.string().required(),
    age: v.number().required(),
}))

The nomenclature tries to resemble JSON Schema to some degree, hence "object" and "additional".

This project is still very new, and I will write an API documentation for it, I'm sorry for not having one yet!

jaymoid commented 3 years ago

Thank you Gustaf, it's the latter case where we don't know the keys, so the second example is perfect. When you put it like that the nomenclature makes total sense, I hadn't thought about it in terms of schema side of things. Thanks again.

Kishimotovn commented 3 years ago

Thanks @grantila :) solved!

Kishimotovn commented 3 years ago

Ace lib By the way ^^