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

@wmfs/j2119

v1.26.0

Published

A general-purpose validator generator that uses RFC2119-style assertions as input.

Downloads

17,152

Readme

j2119

Tymly Package npm (scoped) CircleCI codecov Dependabot badge Commitizen friendly JavaScript Style Guide license

A general-purpose validator generator that uses RFC2119-style assertions as input.

This package is derived from Amazon Web Services Labs' awslabs/j2119.

Usage

There is only one outward-facing function, J2119Validator.

const J2119Validator = require('@wmfs/j2119')

const validator = J2119Validator(schema-filename, ...[schema-extension-filenames])
  • schema-filename is the name of a file containing text in the J2119 syntax described below.
  • schema-extension-filenames are zero or more names of files containing extensions to the schema, using the same J2119 syntax.

J2219Validator returns a validator, which can be re-used, and should be, since its construction is moderately expensive. If the schema files can not be read, then the function will throw an exception.

validate

const problems = validator.validate(json)
  • source is the JSON object to be validated.

validate returns a string array of error messages describing any problems it found. If the array is empty, then there were no problems. The error messages are human-readable and have the general form <json-path to error site> <description of error>.

root

const root = validator.root

root returns the root "role" in the J2119 schema, a string that is useful in making user-friendly error messages.

J2119 Syntax

J2119's operations are driven by a set of assertions containing the words "MUST" and "MAY" used in the style found in IETF RFCs, and specified in RFC 2119. It is organized in lines, each terminated with a single full stop. There are three formalisms, "roles", "types" and "constraints". For example:

A Message MUST have an object-array field named "Paragraphs"; each member is a "Paragraph".

In the above assertion, "Message" and "Paragraph" are roles, "string-array" is a type; the whole line says that a JSON node with the role "Message" is required to have a field named "Paragraphs" whose value is a JSON array containing only object values. It further says that when validating the object members of the array, they are considered to have the role "Paragraph".

The first line of the J2119 schema must be of the following form:

This document specifies a JSON object called a "Message".

This gives the root object the role "Message". Descendant nodes can be given roles based on their parentage (as in the first J2119 example above) and the presence or value of certain fields, and nodes can have multiple roles simultaneously.

The J2119 syntax was invented for the purpose of constructing a validator for one specific JSON DSL (the Amazon States Language) and, at time of writing, there is no claim that it is suitable as a general-purpose JSON schema facility. At this point, the best way to understand J2119 is by example. In the test fixtures directory of this package there is a file AWL.j2119, which may be considered a complete worked example of how to specify a DSL.

Schema Extensions

As WMFS were adapting this code from the AWS Labs original, it became apparent we had a couple of small extensions to ASL which caused our validation to fail. Rather than drop additional rules into the ASL definition, we defined our assertions as extensions. Extensions are organized exactly as above, except that the first line of a J2119 schema extension have the form:

This document specifies an extension to a JSON object called a "Message".

The object name must match that in the root schema. See the file TymlyExtension.j2119 for an example of a DSL extension.

To Do

At time of writing, the AWS Labs' package carries the following TODO

At the moment, the J2119 syntax is parsed via the brute-force application of overly complex regular expressions. It is in serious need of modularization, a real modern parser architecture, and perhaps the application of some natural-language processing techniques.

Aside from that, the framework of roles, conditionals, and constraints is simple enough and performs at acceptable speed.

Contributing

Bug reports and pull requests are welcome on GitHub. Please be aware of our Code of Conduct

License

Licensed under the terms of the MIT license. Copyright (c) 2018, 2024 West Midlands Fire Service