question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

This feature causes yup to throw an error on validation if there’s a property on the object that doesn’t match the schema. Similar to yup.object().noUnknown()

Split off from #14.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
colinhackscommented, Mar 30, 2020

@christophemarois I’ve been thinking about this a fair bit.

I think I’ll introduce a .mask() method on object schemas that lets you pass in a properly typed instance and get back a new object with some subset of the properties. Like so:

const Dog = z.object({
  name: z.string(),
  age: z.number()
})

const justName = Dog.mask({ name: true }, { name: "Fido", age: 20 });
// => { name: string }

In your situation I’d recommend creating a proper recursive type for the sake of representational accuracy:

interface A {
  val: number;
  b: B;
}

interface B {
  val: number;
  a: A;
}

const A: z.ZodType<A> = z.lazy(() =>
  z.object({
    val: z.number(),
    b: B,
  }),
);

const B: z.ZodType<B> = z.lazy(() =>
  z.object({
    val: z.number(),
    a: A,
  }),
);

const a: any = { val: 1 };
const b: any = { val: 2 };
a.b = b;
b.a = a;

const parsedA = A.parse(a);
const maskedA = A.mask({ val: true, b: { val: true, a: { val: true } } }, parsedA);

.mask isn’t actually implemented yet, I was just toying with the idea. Would this work for your use cases? I’ll leave this issue open in the meantime.

2reactions
christophemaroiscommented, Mar 25, 2020

I don’t know if you’d consider also adding yup’s stripUnknown option, which is related to the noUnknown() behavior, but is not treated as an error.

This would be a really useful feature for my particular use-case: I have React components that take as a prop an object with circular dependencies (think ActiveRecord). I currently use yup with to do both prob validation and “picking” properties, so I end up with an object that is serializable, that I can send over HTTP, and that only contains the property I need.

I have a working workflow with yup, but the lack of optional types and correct type inference is problematic.

Extremely dumb yup example to illustrate my point:

import { object, number } from 'yup'

let a = { val: 1 }
let b = { val: 2 }
a.b = b
b.a = a

const schema = object({
  val: number(),
  b: object({
    val: number(),
    a: object({
      val: number()
    })
  }),
})

schema.validateSync(schema, { stripUnknown: true })
// => { val: 1, b: { val: 2, a: { val: 1 } } }
Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript type for object with unknown keys, but only numeric ...
I want the typescript compiler to know that for all present keys, the value is numeric. I tried things like type numObj =...
Read more >
TypeScript | Known and Unknown Keys - John Darryl Pelingo
There are cases where we have known and unknown keys in an object. Having at least the known keys will help with TypeScript...
Read more >
TypeScript | Objects with Unknown Keys and Known Values
Learn how to define object types with unknown keys and known values using TypeScript by explaining with easy code examples.
Read more >
Unknown Keys in Configuration profiles after 10.31 update
Solved: After our server was updated to 10.31 the unknown keys feature is presenting itself in a number of profiles. At first all...
Read more >
Managing objects with unknown structures in TypeScript using ...
No index signature with a parameter of type 'string' was found on type '{}'. ... we define the type of keys and the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found