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

prohibit extra fields in request payload #182

Open
Geksanit opened this issue Feb 4, 2024 · 1 comment
Open

prohibit extra fields in request payload #182

Geksanit opened this issue Feb 4, 2024 · 1 comment

Comments

@Geksanit
Copy link
Contributor

Geksanit commented Feb 4, 2024

problem: If I send the payload with extra fields, then I get an error in the rest. rest does not expect extra fields.

interface ProductModify {
  name: string;
}
interface Product extends ProductModify {
  id: string;
  foo: number;
}
const product: Product = { name: 'TEST', id: '1', foo: 1 };
const modifyProduct = (product: ProductModify) => {};
modifyProduct(product); // no type errors, because Product compatible with ProductModify

I propose to prohibit extra fields, i.e. disable polymorphism.

type Impossible<K extends keyof any> = {
  [P in K]: never;
};
export type NoExtraProperties<T, U extends T = T> = U & Impossible<Exclude<keyof U, keyof T>>;

const modifyProduct = <T extends ProductModify>(product: NoExtraProperties<ProductModify, T>) => {};
modifyProduct(product); // type error: Argument of type 'Product' is not assignable to parameter of type 'NoExtraProperties<ProductModify, Product>'
@Geksanit Geksanit changed the title prohibit extra field in request payload prohibit extra fields in request payload Feb 4, 2024
@kokovtsev
Copy link
Contributor

kokovtsev commented Feb 4, 2024

This can be easily done by wrapping each generated type into io-ts' exact.

On the other hand, it is desirable sometimes to pass extra fields to the server, for example for features like "custom data" where the system objects can be extended with customer-specific fields.

Looks like this should be controlled by the additionalProperties parameter in schemas, WDYT?
https://www.apimatic.io/openapi/additionalproperties

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

No branches or pull requests

2 participants