schema icon indicating copy to clipboard operation
schema copied to clipboard

Expose object's schema shape

Open darkowic opened this issue 4 years ago • 1 comments

Consider following schema

const mySchema = object({
  id: string(),
  name: pipe(string, minStringLength(10))
})

Now, I would like to create another schema to validate only part of mySchema. For that reason I can create another schema and pick only a few validators from mySchema, potentially like this:

const anotherSchema = object({
  name: mySchema.name
})

But the object's validator shape is not available after the validator is built. I took a look at the code and this is potentially possible but the types in refine function are too complex for me at the moment :)

darkowic avatar May 06 '21 19:05 darkowic

Hi @darkowic @mmiszy!

Since the mySchema from darkowic's example is function, which is an object in JS, we can add a new namespace property like schema that will store all the validators from the original one. It will look like this:

const anotherSchema = object({
  name: mySchema.schema.name
})

I don't think we can just store the validators as first lever properties, because it might cause some naming collisions. For example:

const schema = object({
  length: number(),
  name: string()
})

In this case the resulting schema function would have overwritten length and name properties.

Let me know what you think! :)

TomekStaszkiewicz avatar Jun 05 '21 22:06 TomekStaszkiewicz