Open mikesol opened 5 years ago
To kickstart this, I've created two supplemental repos that IMO will be necessary to pull this off:
fake-me-some-json
for basic JSON schema fakingtjs
for the JSON schema definitionBoth of these have analogs in the JS world:
The one thing that'll be really hard to get right is typing. Because TypeScript is so mature, we've been able to implement Haskellian typing for all of the JSON Schema manipulations, which leads to really precise messages from the IDE if any parameters are off. Because python is missing several key features from TS-land (ie intersection types are impossible, even when using the new TypedDict
), this sort of thing is not possible. That doesn't mean we should forego typing, but it does mean that the extension type system I describe here is not possible.
One solution is to use strict typing without extension types, which means that we are locked to the JSON schema syntax and cannot extend it without mypy
failing our build. The other is to specialize JSON schema only for our use case, but that defeats the purpose of building a generic library.
We should use the same fluent API as the JS library for defining specs. This is This is similar to HTTPretty and pook with the notable difference that we are generating schemas instead of hardcoded responses. As a result, this requires several additional sub-projects.
json-schema-strictly-typed
. Type safety will probably be a lot harder in Python, especially intersection types. I thinkmypy
currently cannot handle intersections. So we may need to give up on some of the sweet type safety that we can get with languages like TypeScript and Haskell.json-schema-poet
. This will probably be easier, but again the typing will be a challenge if we decide to go down that route. FYI, this is used in Unmock JS here, so there is a clean separation between the JSON library and its usage in unmock.Currently, unmock-py just takes a request and serves a user-defined response without using a schema, so it is not clear where these JSON schemas should live. In
unmock-js
, they live in a global schema store, which is kinda hacky but works. There are several ways to do it in python - global store, injecting them into tests via decorators, etc. Because python has better inversion of control and test runners, we can probably have a much cleaner interface in python for schema storage and activation than in JS.