npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yaschva

v2.6.1

Published

Yet another schema validation

Downloads

84

Readme

License: MIT Node.js CI Code coverage

A simple validation library.

Using a concise definition, with safe defaults.

Playground

There is an interactive playground, where yaschva can be test driven

playground.yaschva.com

Examples

Defining an object with a few properties is as simple this:

{
  this helps your IDE to autocomplete the schema when writing it.
  ↓↓↓↓↓↓↓↓↓
  "$schema": "https://yaschva.com/schema.json",
  "username": "string",               <-- Username is a string
  "numberOfCats": "number",           <-- this is a number
  "favoriteCatName": ["?", "string"]  <-- this is an optional string
}

More examples

Capabilities

Overview

  • Validate data based on a schema defined either in json or in type safe typescript.
  • Returns easy to understand error messages on validation failure.
  • Generate random data based on the schema.
  • Generate typescript types from schema.

Strict and safe by default

Every field is mandatory by default, and additional properties are rejected.

Concise definition

The schema syntax is concise, and simple, with as little noise as possible.

DRY schema

Custom types can be defined and reused. This enables recursive data structures and makes sure that copy paste is not needed to fully define the structure.

Helpful failure

When a validation fails, it's output describes what went wrong with the validation, in a way that reflects the original schema, for easy troubleshooting.

Written in Typescript

It helps auto complete while using it in the IDE. The schema structure is also described in Typescript types.

100% unit test coverage

All code paths are tested, every scenario is covered

Concise, easy to read implementation

The whole validation is done in 310 lines of code. That includes all the Typescript types.

Includes helpful tools

It contains a random data generator, that can generate random data, It is useful for testing and mock APIs.

It can generate Typescript types from the schema, so after validation you can be sure to have the correct type, and Typescript can help you from there on. This saves the effort of manually typing out the type and precludes the possibility of making an error.

There is also a playground where you can test out your schemas, validate data or generate dummy data.

Can self describe the schema

Yaschva is flexible enough to describe itself. This enables the generator to generate valid schema definitions.

Limitations

Property names starting with a $ (dollar sign) are reserved. If your data structure has value names starting with $, they need to be escaped in the schema: { "\\$escapedDollarSign": "string" }

Project structure

This project is written in typescript. These can be found under ./src.

The default build will create ES6 Modules with EsNext target, these can be found under package root directly in the npm package.

Sources compiled with Commonjs modules and es6 target can be found under ./cjs in the npm package.

Why not just use JSON schema?

JSON schema is a very sharp tool, which tons of features. This project is in fact using a json schema to describe it's format, and provide validation and auto completion.

JSON schema syntax can be very noisy and hard to understand, it provides dangerous defaults (everything is optional and additional properties are always accepted).

This project tries to simplify syntax for better readability, and provide more secure defaults among others.

JSON schema is very verbose, yaschva's schema is defined with both itself and in JSON schema. The JSON schema is about twice as long.

How to use JSON schema definition when writing yaschva schema

In your yaschva schema file you need to add a property in the root object called:

"$schema": "https://yaschva.com/schema.json"

This will enable your IDE to help with yaschva's syntax and check for errors.

Note: the $schema property is not part of yaschva schema, and will be removed when using the loadJson function.

What can be improved

Generating recursive Typescript type

This is not currently possible, because the type generator is designed to generate a single type, unnamed type, but to do it for recursive types, it must be named to enable recursion. The type generation needs to be reworked a bit for it to work.

Arrays and maps with size constraint are not encoded in Typescript.

More constraints

There are a few more constraints that are currently not supports but would be helpful to implement, like:

  • Array to be unique set
  • Map to be a unique set

Faster happy path validation

Although Yaschva is very fast, it could be speed up for valid values. Right now it always builds up an object, that reflects the input data structure, to mark the problems where they occur.

An alternate validator could forgo this, and just throw an error on the first failure. After that it could send it to the error collection implementation, to generate a meaningful error message. This creates a tradeoff, the best case speed is improved, but the worst case speed is slower.