@4lch4/schemas
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

@4lch4/Schemas

This repository is home to my Schemas package, which was inspired by the Vercel/Schemas package. Basically it's an NPM package that exports schemas for a variety of use-cases and utility classes such as a Schema Validator that uses Ajv/Joi behind the scenes to validate a given object matches a given Schema object.

Schemas

The following table provides information regarding the available schemas that this package exports. The first two columns are self-explanatory but the third column (Parent Project) is a bit special. If the schema was developed for use with a particular open-source project then this column should contain the name of said project and it should also be a hyperlink to the homepage of said project. For example, if the parent project was the Sindri API, then it should link to its git repository hosted on my instance of Gitea.

Name Description Parent Project
QuestionnaireSchema Defines the structure of a questionnaire.json file that is present at the root of a template repository for Sindri. Sindri-API
TurboRepoSchema Defines the structure of a turbo.json file that is present in mono-repos managed by Turbo. Turbo Repo
VercelSchemas Defines the structure of the Vercel Config file present in individual projects and at the global level. Vercel

Validator Utility

This library also exports a ValidatorUtil class that uses Ajv under the hood to validate objects match a given Schema, like so:

import { ValidatorUtil, QuestionnaireSchema } from '@4lch4/schemas'

const testObj = {
  alpha: 'string',
  beta: {
    subAlpha: 'Another String.'
  }
}

const validator = new ValidatorUtil()

// Validate the testObj against the QuestionnaireSchema by name.
const namedIsValid = await validator.validate(testObj, 'QuestionnaireSchema')

// Validate the testObj using the QuestionnaireSchema by passing in the QuestionnaireSchema object itself.
const specialIsValid = await validator.validate(testObj, QuestionnaireSchema)

Why?

At first I just wanted a small API that would return the JSON Schema for a particular schema I needed. Once I built that I stumbled upon Vercel's Schema package and realized how useful it would be to have something like that in my tool belt. That aside, the other reasons why I'm making this are the same as what Vercel listed in their README:

  • Keep schemas used across 4lch4/4lch4 Industries projects in sync.
  • We use .js instead of .json because parsing JSON takes longer.

NOTE: A lot of the following text is a copy/paste from the Vercel/Schemas README since what I'm doing with this repository/package is the same as what their package does.

Usage

To get started, pick one of the schemas in this repository and load it:

const { TurboRepoSchema, Validator } = require('@4lch4/schemas')

const isValid = ajv.validate(TurboRepoSchema, <object-to-validate>);

if (!isValid) {
  console.error(`The following entries are wrong: ${JSON.stringify(ajv.errors)}`);
}

Next, set up AJV (the validator) and run the schema through it:

const AJV = require('ajv')

const ajv = new AJV({ allErrors: true })

That is all! 🎉

Contributing

At the moment I am not looking for any external contributions to this repository due to the very nature of the library.

Readme

Keywords

none

Package Sidebar

Install

npm i @4lch4/schemas

Weekly Downloads

1

Version

0.3.0

License

MIT

Unpacked Size

154 kB

Total Files

70

Last publish

Collaborators

  • alcha
  • 4lch4-ci