-
Notifications
You must be signed in to change notification settings - Fork 2
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
Refactor: Schema Definition in TypeScript with Zod #181
Conversation
Not mentioned in this PR description - We can generate a JSON Schema formatted Meta-Schema for the Lectern Dictionary structure so that a language agnostic schema is available to validate against. We should add a piece of automation to the build step that generates this Meta-Schema file from the Zod Schemas. It would also be valuable to add a version of the create dictionary, add schema, and update schema methods that work as a validator without updating the stored dictionaries. These endpoints would output the dictionary as it would look after updates were made, but nothing would be committed to the DB. It would be best to put these on a URI path separate from the endpoints that update the DB. |
Note: Changes route `/diff/` to `/dictionaries/diff`
6db0f40
to
66867fc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heaps of trolling and nitpicking. don't take anything too seriously...
but maybe, the primitive spelling mistakes... those, do correct ;)
"build": "npm run build-ts", | ||
"serve": "node dist/server.js", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these steps can be avoided altogether using ts-node
.
it's as close as it gets right now to treating ts like an actual language 😆
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
import { Dictionary } from '../../../../src/types/dictionaryTypes'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
....lol.....
visited: VisitedSet, | ||
): string | string[] => { | ||
const cachedValue = discovered.get(tag); | ||
if (cachedValue !== undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
le !isUndefined
is le blergh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future reference: The intention here is to permit empty strings through, and certainly TS tells us it will only be a string, string array, or undefined... but assuming some strange value gets through this will also permit NaN and null through.
The absolute best check would be to check if it is truthy or an empty string: if (cachedValue || cachedValue === '')
Co-authored-by: Anders Richardsson <2107110+justincorrigible@users.noreply.github.com>
Co-authored-by: Anders Richardsson <2107110+justincorrigible@users.noreply.github.com>
Purpose of this Refactor: Improve Types
This work started as an attempt to improve the type system usage throughout the repository, which revealed an opportunity to improve the method used to define the Dictionary Meta-Schema.
The original planned type system improvements were to:
.tsconfig.json
to usestrict: true
(and fix the errors this causes).any
types where the internal types were known.Working on item 2 - writing TS types for the Dictionary Meta-Schema - revealed a couple things about the previous setup:
Along with this information it is important to highlight that there are tools available to TypeScript in 2023 that were not available when Lectern was first written. Specifically, we now have zod, a TypeScript first schema definition library that is becoming an industry standard. Zod can resolve all of the issues highlighted above, including the two most important changes (in my opinion):
This is important for code maintenance (no inconsistency between JSON schema and TS). It lets developers take advantage of the TS type system in their coding, for the benefits in reliability and code feedback that it provides. This will also standardize how Dictionary validation is performed, putting the content rules adjacent to the structure definition and ensuring both are run in the same way.
Changes in This PR
There are a lot of files touched in this PR but the important changes are isolated to a few. The long list of files changed is the result of related changes to imports, small type fixes, and some moving of files around.
The important changes are:
/src/types/dictionaryTypes.ts
- Zod Schema definitions, and exports of types for Dictionary, Schema, fields, restrictions, and all of the composite types.dictionaryTypes.ts
./src/db/dictionaryModel.ts
file./src/routers
./src/external
- This includes ego and vault integration code./src/controllers/dictionaryController.ts
.Some other updates performed:
package.json
has been updated to be version 2+, using the pre-release version tagnext
./src
filesBugs Fixed