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

@overture-stack/lectern-validation

v2.0.0-beta.3

Published

Logic for validating data using a Lectern dictionary

Downloads

119

Readme

Lectern Validation

TypeScript

Note

This may not be the module you are looking to import.

This is a sub-module used as a dependency by both the Lectern Client and Lectern Server.

If you are building an application that will interact with a Lectern Server over HTTP, or wants to validate data using a Lectern Dictionary, you likely want to import the Lectern Client.

This package provides tools to parse and validate data based on the schemas in Lectern Dictionaries.

Parsing Data

Parsing data involves reading string values for fields defined in a Lectern Schema and converting the values into properly typed data. For example, if a field has "dataType": "number" and the provided value "123" this will be converted from the string value into the numeric 123. A more complicated example would take a comma-separated array value, convert each element, and return the final array. If any values cannot be properly parsed and converted based on the schema's rules, an error is returned instead.

There are four separate parsing functions exported, mapping to different collections of data to be processed together:

  • parseFieldValue: Parse a string value for an individual field.
  • parseRecordValues: Parse all fields in an UnprocessedDataRecord based on a schema definition. Applies parseFieldValue to each field.
  • parseSchemaValues: Parse all records in a collection belonging to an individual schema. Applies parseRecordValues to each record.
  • parseDictionaryValues: Parse all records for multiple schemas in a dictionary. Applies parseSchemaValues to each array of records provided.

Each parsing function will return a Result object that indicates if the parsing completed successfully. When parsing completes without any errors the response will include the parsed data with all fields converted to the correct type.

If the parsing failed, part of the response will be an array of errors indicating which record and which fields had parsing errors. The response will also include the partially parsed data record(s); fields that were succesfully parsed will have been updated to their correct data types, but fields that failed to parse will still contain their original string values.

Validating Data

Validation functions are provided to test parsed DataRecords with all restrictions defined in a Lectern dictionary. These functions will identify all restrictions that must be tested from the provided schema and apply these to the given data, including resolving conditional restrictions. Different restrictions are applied depending on which data collection is provided; validating an individual fields will only test field level restrictions, while validating a whole schema will also validated unique and uniqueKey constraints.

There are four main validation functions, provided to validate data at the field, record, schema, and dictionary levels:

  • validateField: Validate the field value, testing all field level restrictions such as regex, and codeList
  • validateRecord: Validate all fields in a record, testing that all fields belong to that schema, that all required fields are present, and applying validateField to all fields in the record.
  • validateSchema: Validate a collection of records from a single schema, testing all unique and uniqueKey requirements for that schema and then applying validateRecord to all records.
  • validateDictionary: Validates multiple collections of records each belonging to schemas from a dictionary. This checks that each schema specified is a member of the given dictionary, and tests foreignKey restrictions on each schema, in addition to applying validateSchema to each colleciton.

All validation functions return a TestResult object that will indicate if the validation passed or failed. If the validation is successful then this result will simply indicate that the data is valid. If there were any errors then the response will include a error information.