A small node package that exports all of my available schemas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Go to file
4lch4 235a2f6aac
continuous-integration/drone/push Build is passing Details
continuous-integration/drone Build is passing Details
fix(package.json): added run to test script
5 months ago
examples chore: started the examples directory 5 months ago
src feat: updated AllSchemas object with new keys 5 months ago
.drone.yml feat(.drone.yml): fixed drone manifest signature 5 months ago
.gitignore chore(.gitignore): added dist directory 5 months ago
.prettierignore chore(.gitignore): added pnpm-lock 5 months ago
.prettierrc feat: executed prettier 5 months ago
LICENSE Initial commit 5 years ago
README.md chore: executed prettier 5 months ago
package.json fix(package.json): added run to test script 5 months ago
pnpm-lock.yaml chore: removed mocha/chai, I'll add tests later 5 months ago
tsconfig.json chore(tsconfig.json): removed unnecessary exclude property 5 months ago

README.md

@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.