Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exact record support #162

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Exact record support #162

wants to merge 3 commits into from

Conversation

reinismu
Copy link

@reinismu reinismu commented Aug 17, 2020

Added .exact() for Record. It allows to check that incoming object doesn't have additional fields.

Close #41

First I tried to create { strict: true }, but validate, check etc. are shared between multiple Runtypes and they don't really need that setting.

@coveralls
Copy link
Collaborator

coveralls commented Aug 17, 2020

Coverage Status

Coverage increased (+0.02%) to 99.12% when pulling b3587a2 on reinismu:master into 69b8724 on pelotom:master.

@jraoult
Copy link

jraoult commented Oct 16, 2020

@reinismu I'm excited about this. Have you heard from @pelotom regarding this PR?

@reinismu
Copy link
Author

Nop :/

I made my own build so I could use this change + Omit

"dependencies": {
    "runtypes": "github:reinismu/runtypes#with-pick-omit"
  },

If you are interested

@jraoult
Copy link

jraoult commented Nov 18, 2020

@reinismu cheers, I ended up moving to zod anyway. It wasn't too hard after all. I feel this project is sadly losing momentum and zod provide strict check by default out of the box and some other nice goodies API wise.

@bayareacoder
Copy link

bayareacoder commented Nov 18, 2020

@reinismu You closed this issue but does it work for an object with both required and optional keys, to make sure there are either no required keys missing, or no additional keys, beyond the optional ones, specified? I don't see that case in your tests.
I believe this is a very common use case to eg test an API response.
We use this library and found a way to check for this that works, but is not elegant:

const allowedKeys = ['requiredKey1', ..., 'requiredKeyM', 'optionalKey1', ..., 'optionalKeyN'];
const RTtype = Record({
requiredKey1: ...
...
requiredKeyM: ...
})
.And(Partial({
optionalKey1: ...
...
optionalKeyN: ...
})
.And(Dictionary(Unknown, allowedKeys));

This will check that:

  • all required keys are present and of correct type
  • optional keys, if present, are of the correct type
  • no additional keys are present.

Basically I don't think an exact spec on any intersection type works, since it's not the same as the intersection of the exact types...

@reinismu
Copy link
Author

@jraoult Thanks! It seems promising

@bayareacoder From use of it I have noticed few edge cases where it doesn't work. One of them is when trying to use Or

@yuhr yuhr mentioned this pull request Mar 16, 2021
@yuhr
Copy link
Collaborator

yuhr commented Mar 23, 2021

This would come in handy once #113 get merged, as there will be no need to use a tricky syntax like Record({ x: Number }).And(Partial({ y: Number })) in order to define partially-optional records. We will be able to write it as Record({ x: Number, y: Optional(Number) }), so it's compatible with the .exact() API.

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.

Add a flag to check/validate/guard which allows enforcing exact match (not just structurally assignable)
5 participants