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

Spike: Constrain the OpenAPI schema emitted for a model [DO NOT MERGE] #2646

Closed
wants to merge 2 commits into from

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Mar 26, 2019

This pull request offers a PoC implementation demonstrating how to modify the schema generated from a LB4 model depending on configurable options. In particular, the following two use cases are covered:

  • PATCH - all properties are optional
  • CREATE - id and possibly _rev must not be present

The first commit imports code from #2592 and should be ignored.

The second commit contains PoC and SPIKE notes with a high-level overview.

Related stories:

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

@bajtos bajtos added this to the March 2019 milestone milestone Mar 26, 2019
@bajtos bajtos self-assigned this Mar 26, 2019
@bajtos bajtos changed the title Spike: spec for model with constraints Spike: OpenAPI schema for model with constraints Mar 26, 2019
@bajtos bajtos changed the title Spike: OpenAPI schema for model with constraints Spike: constrain the OpenAPI schema emitted for a model Mar 26, 2019
@bajtos bajtos changed the title Spike: constrain the OpenAPI schema emitted for a model Spike: Constrain the OpenAPI schema emitted for a model Mar 26, 2019
includeRelations?: boolean;

/// List of properties to exclude from the schema
exclude?: (keyof T)[];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type construct allows TypeScript to verify that exclude is using only existing model properties.

Consider the following incorrect usage:

{
  schema: getModelSchemaRef(TodoList, {exclude: ['id', 'foobar']}),
}

The compiler prints the following error:

Argument of type '{ exclude: ("id" | "foobar")[]; }' is not assignable to parameter of type 'JsonSchemaOptions<TodoList>'.
  Types of property 'exclude' are incompatible.
    Type '("id" | "foobar")[]' is not assignable to type '("id" | "title" | "color" | "todos" | "image" | "getId" | "getIdObject" | "toJSON" | "toObject")[]'.
      Type '"id" | "foobar"' is not assignable to type '"id" | "title" | "color" | "todos" | "image" | "getId" | "getIdObject" | "toJSON" | "toObject"'.
        Type '"foobar"' is not assignable to type '"id" | "title" | "color" | "todos" | "image" | "getId" | "getIdObject" | "toJSON" | "toObject"'.

@bajtos bajtos changed the title Spike: Constrain the OpenAPI schema emitted for a model Spike: Constrain the OpenAPI schema emitted for a model [DO NOT MERGE] Mar 26, 2019
},
},
})
obj: Pick<TodoList, Exclude<keyof TodoList, 'id'>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe define a type alias:

export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to define a type alias.

We must carefully choose a name that will be different enough from Exclude. Few options that come to my mind:

  • ExcludeProperties<T, K>
  • ExcludeKeys<T, K>
  • OmitProperties<T, K>
  • OmitKeys<T, K>

Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The design that customizes the schema by options LGTM 👍

@bajtos bajtos force-pushed the spike/spec-for-model-with-constraints branch from 887ae84 to f7db169 Compare March 28, 2019 08:45
@bajtos
Copy link
Member Author

bajtos commented Mar 28, 2019

Created the following stories:

Closing this spike as done.

@bajtos bajtos closed this Mar 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants