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鈥檒l 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 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 requested review from raymondfeng, hacksparrow, emonddr, jannyHou, b-admike, nabdelgadir and strongloop/loopback-maintainers 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)[];

This comment has been minimized.

Copy link
@bajtos

bajtos Mar 26, 2019

Author Member

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'>>,

This comment has been minimized.

Copy link
@raymondfeng

raymondfeng Mar 26, 2019

Member

Maybe define a type alias:

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

This comment has been minimized.

Copy link
@bajtos

bajtos Mar 28, 2019

Author Member

+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>
@jannyHou
Copy link
Contributor

jannyHou left a comment

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 Mar 28, 2019

@bajtos

This comment has been minimized.

Copy link
Member Author

bajtos commented Mar 28, 2019

Created the following stories:

  • Emit schema with all model properties optional #2652
  • Emit schema excluding certain model properties #2653
  • Spike: simplify requestBody annotation with schema options #2654

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
You can鈥檛 perform that action at this time.