schema-tools icon indicating copy to clipboard operation
schema-tools copied to clipboard

Can the schema created by combining versioned schemas have proper type?

Open bahmutov opened this issue 6 years ago • 1 comments

If we create a new schema by adding objects, we should have a good type of the result, which will allow us to know the available schemas and their versions statically. This would provide a great IntelliSense for commands and assertions.

For example, in https://github.com/bahmutov/todo-api-with-json-schema/blob/master/schemas/index.ts we know both schemas

export const schemas: SchemaCollection = combineSchemas(
  PostTodoRequest,
  PostTodoResponse
)

So when we access schemas, we should statically know that only PostTodoRequest and PostTodoResponse are valid properties

Related look ups https://mariusschulz.com/blog/typescript-2-1-keyof-and-lookup-types

bahmutov avatar Mar 25 '19 02:03 bahmutov

Probably in utils.ts

export const combineSchemas = (...versioned: VersionedSchema[]) => {
  const result: SchemaCollection = {}
  versioned.forEach(v => {
    const title = v[Object.keys(v)[0]].schema.title
    const name = normalizeName(title)
    result[name] = v
  })
  return result
}

bahmutov avatar Mar 25 '19 02:03 bahmutov