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

object decoder should have an option to throw error if extra properties are present #57

Open
mrjones2014 opened this issue Feb 18, 2021 · 0 comments

Comments

@mrjones2014
Copy link

If an object is run through the decoder at runtime, there should be a way to configure the decoder to throw an error if the decoded object has extra properties not defined on the original decoder.

For example, given the following decoder:

interface MyType {
  myProperty?: string;
}

const MyTypeDecoder: Decoder<MyType> = object({
  myProperty: optional(string());
});

There should be an additional combinator to make the decoder throw an error given the following input:

const myTypeRuntimeValue: MyType = {
  myProperty: "someValue",
  myOtherProperty: "This one isn't defined on the interface"
};

// I want this to throw an error because myTypeRuntimeValue has
// an additional property not defined in the interface
const decoded = MyTypeDecoder.runWithException(myTypeRuntimeValue);

The combinator could be something like exact(value), e.g.

interface MyType {
  myProperty?: string;
}

const MyTypeDecoder: Decoder<MyType> = exact(object({
  myProperty: optional(string());
}));
@mrjones2014 mrjones2014 changed the title object decoder should have an option to throw error if extra fields are present object decoder should have an option to throw error if extra properties are present Feb 18, 2021
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 a pull request may close this issue.

1 participant