brandonchinn178 / aeson-schemas

Easily consume JSON data on-demand with type-safety
http://hackage.haskell.org/package/aeson-schemas
BSD 3-Clause "New" or "Revised" License
52 stars 1 forks source link

Restrict `Object schema` to only contain `SchemaObject` #3

Closed brandon-leapyear closed 4 years ago

brandon-leapyear commented 5 years ago

Currently, Object schema allows schema to be any SchemaType. It would be nice to encode the stronger version: schema is only allowed to be the SchemaObject constructor.

This could potentially be implemented by changing SchemaType to be:

data SchemaTypeDefs (onlyObject :: Bool) where
  SchemaBool :: SchemaTypeDefs 'False
  SchemaInt :: SchemaTypeDefs 'False
  SchemaDouble :: SchemaTypeDefs 'False
  SchemaText :: SchemaTypeDefs 'False
  SchemaCustom :: Type -> SchemaTypeDefs 'False
  SchemaMaybe :: SchemaTypeDefs 'False -> SchemaTypeDefs 'False
  SchemaList :: SchemaTypeDefs 'False -> SchemaTypeDefs 'False
  SchemaObject :: [(Symbol, SchemaTypeDefs 'False)] -> SchemaTypeDefs onlyObject

type SchemaType = SchemaTypeDefs 'False
type SchemaObjectType = SchemaTypeDefs 'True

and then change Object to be:

newtype Object (schema :: SchemaObjectType) = ...

This change breaks other things, so would take some work into fixing up all the other areas. Off the top of my head, the schema quasiquoter would need to have a kind signature of SchemaObjectType, IsSchemaObject would need to be changed or removed, and there would need to be some SchemaObjectType -> SchemaType casting