TypeScript icon, indicating that this package has built-in type declarations

0.12.0 • Public • Published


The core primitive data structures and functions for a validation library.

The primitives in @condenast/cross-check are used to implement @condenast/cross-check-dsl. In general, you probably want to use that library, even for primitive use-cases.

The data structures and core functions are extracted into a separate library to create a small, well-defined composition boundary. Any validators written against the interfaces in @condenast/cross-check will compose reliably with validators created using the higher level abstractions (including the object oriented abstractions) provided using @condenast/cross-check-dsl.

Composition Goals

At its core, the goal of cross-check is to define a validation primitive that satisfies a number of composition goals.

Validation is More Than Schema

First, a traditional schema library is not a good replacement for a validation library. Schema libraries typically "accept" or "reject" entire entities, throwing an exception if the schema is rejected. In other words, schema libraries assume that users have attempted to produce a valid document, and provide protection against programmer errors.

In contrast, validation libraries provide rich, interactive error information to a human end-user, typically presented via an interactive UI.

This means that attempting to validate an entire object must:

  • produce a list of validation errors
  • identify precisely where the validation errors occured in a way that is consumable by an interactive UI
  • provide the error messages in a format that can be localized into various languages and contexts

In our original design exploration for cross-check, we wrote:

The accurate placement of errors in a form (a UI concern!) is a key requirement of a good validation library.

This is still a good guiding principle for this library. A good validation library cannot avoid considering UI concerns in its core design.

Framework Agnostic, But Framework Friendly

cross-check was extracted from the requirements of a working validation system that powered Condé Nast's CMS, which is written in Ember.

One of the main goals of the extraction was to allow people to define validation rules for their forms without needing to understand Ember or its object model.

However, we wanted to make it possible to write validators that could be shared between vanilla environments and frameworks.

This mostly amounts to two concerns.

First, applications should be able to expose well-defined "services" to validations. If a validator depends on such a service, and somebody wants to use the validator in a different environment (such as React Native), they will have a clear, pure-JavaScript definition of what they will need to implement.

In practice, this means that integrations can expose things like a configuration service or feature flagging infrastructure.

Second, it should be possible to write a validator that looks up properties on an object, agnostic to how those properties should be looked up. For example, looking up a property in an Immutable.js Map requires the user to use .get(). Knockout turns computed getters into functions (to look up firstName on a Person object, you say person.firstName()).

In the case of both of these issues (services and getters), the philosophy of cross-check is to expose hooks on an "ObjectModel" (see below) that framework integrators can use to abstract these distinctions. Validators receive this object model as a parameter, and if validator definitions work through the ObjectModel (e.g. looking up properties by using objectModel.get rather than direct indexing), they will be reusable in more environments and with more kinds of data structures.

Because it can be difficult to remember to work through the object model all the time, the @condenast/cross-check-dsl library provides a number of abstractions that do the work for you. For example, the object() validator provided by @condenast/cross-check-dsl automatically looks up sub-properties by using objectModel.get.

Values, Not Models

At the core, cross-check validates individual values, not a bunch of fields on an object like many other validation libraries.

This makes it possible to validate that a single string matches a particular format, or that a number is greater than some lower bound.

To validate an object, you validate the object as a "value" and use composition to validate its constituent parts. Several design decisions of this library make such composition possible:

  • A single validator can return any number of errors. This makes it possible for a single validator to run sub-validators on sub-parts of the object in question.
  • Validation errors include a path member, which provides the path to the place where the error occurred. Validations that validate sub-properties, like the object validator, prepend the path that they plucked off. This also allows arbitrary nesting: each validator that plucks off a sub-part of an object prepends the path it added.

These design decisions also make array validations relatively consistent: an array validator runs the same sub-validator on every element of the array, and produces an error whose path is the index into the array.

This makes validating an array of objects containing members that are themselves arrays of objects a standard composition in this system.

Key point: There is no distinction between a "single value" and an object or array. A value is a value is a value. Composition takes care of the rest.

Functional, Not Effectful

Unlike many other validation libraries, this validation library never mutates the underlying objects in order to validate them or report errors. It also does not throw exceptions in order to report validation errors like many "schema validation" libraries.

Instead, the validation process reads data from objects and produces (asynchronously) an array of error messages.

Localization Agnostic, But Formatting Friendly

Validation libraries usually make formatting a responsibility of individual validators. This forces a hard choice: provide no localization solution at all, hardcode support for a specific localization library, or force people to write new implementations of the validations for each localization solution they want to use.

The validation process in cross-check produces a data structure containing enough information to create localized, formatted error messages, rather than making formatting an additional responsibility of validators.

  "path": ["lat"],
  "message": {
    "key": "gt",
    "args": { "expected": 0, "actual": -50 }

Validators themselves are responsible for producing these error message data structures, but not for formatting. This allows validators to remain compact and still relatively easy to write, while allowing for robust and high-quality localizations.

In practice, higher-level libraries written on top of these primitives should expose integration with localization libraries, but validators themselves can remain agnostic to those questions.

Different Validation Rules for Different Contexts

An article that's ready to be "published" has stricter validation rules than an article that's still a "draft". "Autosave" has a higher tolerance for invalid content than "commit". It's important to check for username uniqueness on "create" but not "update".

These are examples of the motivation for "validation contexts" in cross-check, which allow an application to define validations as only applying to certain contexts.

The validate function takes a context as a parameter, and the core validation descriptor data structure has a field to enumerate the fields in which it should be tested.

Validators themselves don't need to know about these contexts, the validate loop is responsible for ignoring rules that don't apply to the requested context.

The Object Model

Once you have constructed a validation descriptor, you validate a value by calling validate(objectModel, value, descriptor, context).

The first parameter, objectModel, is an object that gets passed into each validation function.

The ObjectModel has two mandatory methods:

  • get
  • asList

The get Function

The get function takes an object and key and returns another value.

A simple implementation of the get function is:

const SimpleObjectModel = {
  get(obj, key) {
    return obj && obj[key];

As described above, this allows users of frameworks like Ember and Knockout, as well as libraries like Immutable.js, to describe how to look up properties.

Validators implemented using @condenast/cross-check-dsl automatically use this interface to look up sub-properties, which means that normally-written validators will work just fine in many environments.

The asList Function

The simplest implementation of an object model is:

const SimpleObjectModel = {
  get(obj, key) {
    return obj && obj[key];

  asList(obj) {
    return Array.isArray(obj) ? obj : null;

Service Functions

It is sometimes desirable to pass additional context into validators. For example, your application might have a configuration service that defines how strict a validation needs to be.

You can implement a specialized ObjectModel:

const AppObjectModel {
  get(obj, key) {
    return obj && obj[key];

  asList(obj) {
    return Array.isArray(obj) ? obj : null;

  config = {
    strict: true

Then, a validator that wants to use that service simply depends on the specialized object model. If you're using TypeScript, the type signature tells the whole story:

function format(env: AppObjectModel, options: RegExp) {
  return value => {
    return new Task(async run => {
      if (env.config.strict && options.test(value)) return;
      if (!env.config.strict && options.test(value.trim())) return;

      return [{ path: [], message: { key: 'format', args: options }}];

Specialized Object Models, Philosophy

By looking at the definition of this validator factory, you can tell that it can only be used with an implementation of AppObjectModel.

General-purpose validators should avoid relying on specialized object models, but applications should use them to be explicit about validator dependencies.

If an application wants to reuse some validator definitions in another implementation (such as a native app), the specialized object model definition will fully describe what the other implementation needs to do in order to use validator definitions built for the application.

Basic Usage

// A validator function
function format(options) {
  return value => {
    return new Task(async run => {
      if (options.regexp.test(value)) {
        return [];
      } else {
        return [{
          path: [],
          message: { key: 'format', args: options }

function formatFactory(env, options) {
  return format(options);

const emailDescriptor = {
  factory: formatFactory,
  options: { regexp: /\d{4}/ },
  contexts: []


cross-check was originally extracted from Condé Nast's CMS, and the work to extract it and release it as open source was funded by Condé Nast.




Package Sidebar


npm i @condenast/cross-check

Weekly Downloads






Unpacked Size

112 kB

Total Files


Last publish


  • deepak_mohan
  • gshankar-in
  • pradeep_nayak
  • yarramanohar
  • somsekhardash
  • sabarni-condenast
  • dev-anand-94
  • anubalakrishnan
  • naveen1601
  • ganavi_j
  • sheetalparsa
  • vishnusimvisfear
  • vgoel
  • zeeshan-wani
  • vinayashreeram
  • mitchellstewart
  • nhrqz
  • ronjr_owlbear
  • snehak
  • raveena-ram
  • ankitkumar02
  • swati_verso
  • noman-cn
  • saahithisri
  • prasil1107
  • aditijhacn
  • imran-conde
  • saniyashk
  • mithunsathyanmits
  • sanjana_s
  • jyoti_3009
  • rajashreearvikar19
  • mala-shanbhag
  • ac432
  • ssingh417
  • nvillato
  • ricardofbarros
  • vmehrotr
  • speluri
  • jakir-coder
  • pranjal_sharma306500
  • tdscondenast
  • omsun22
  • midhunmurali001
  • saicharan7766
  • surabhidesai
  • suhitrathi-condenast
  • dkorenblyum
  • bbui
  • tejasvi-bg
  • vnallasa
  • vikas-sc
  • skumar7
  • cyrilpanicker-condenast
  • boygao1992
  • saikat_conde
  • imleodc
  • jasonmarlin
  • arunconde
  • roberlander
  • nprabhu-cn
  • juliendevlin
  • unviradiya
  • vinitha_thiagarajan
  • areadman
  • samruddhidube24
  • gapurdev
  • akshayjain10
  • utkarsh24
  • suma_k
  • piyush_das
  • cnmanju
  • cn-rahul-tiwari
  • dhanashri027
  • bharathikarunanithi
  • cn_priyanka14
  • prashant_singhparihar
  • m-mallikarjuna
  • smantriconde
  • machelslackatcondenast
  • karthik_cn
  • muthuprakashvelumani
  • issanjana
  • a-rena
  • colin-alexa
  • ramkun
  • namansingh_bhandari
  • cnarkhede
  • hebsibal_selvaraj
  • akeshavamurthy
  • priyanka_jha
  • klnarayanan
  • spoorthi.chandrashekar
  • erikveland
  • asinghconde
  • eunianina
  • raxs
  • vivekans
  • conde_jode
  • jordi.escude
  • nnasirov
  • unnatik
  • psundar
  • arhovale
  • rashmicn
  • siddhu-23
  • atp-engineering
  • danj-cn
  • maila-labib
  • anilc93
  • lavishgambhir
  • ksriniva2
  • andregcab
  • tchathur
  • kangkanbora
  • vinay-pr
  • josephrussell-cn
  • anaedzm
  • cn-connorbrannigan
  • prajwal_keshav
  • babincondenast
  • edistel
  • balar20
  • cn_jenkins
  • frandevinney
  • renovate
  • condenastadmin
  • copilot-robot
  • fennen529
  • fmadrie
  • gmedina
  • bigzed
  • biku
  • joshcondenast
  • rashmi-rao
  • imakshath
  • nishkalaadiga
  • niharanil
  • katiasfihivogue
  • alokreddycn
  • prakashn37
  • andi.anderson522
  • davidkofahl
  • tollmanz
  • amalamchtal
  • tdshap
  • sayeedcn
  • pitchfork-dev
  • danhaller
  • rajasinghcondenast
  • derrickatcn
  • tce
  • cnid_engineering
  • lilyhealey
  • prajaktak
  • manthanbhosle
  • psharma2
  • bhumikakhatri
  • igostu
  • cwoldt
  • mtzhang
  • manthanraut
  • drosenbaum
  • aswani_gupta
  • garryyao
  • yakshita
  • kalyanikasar
  • shreyapa
  • natelawscn
  • leahzxxz
  • katemont
  • eddleston
  • rtt
  • eshno
  • anwaukoni
  • eduardoveras
  • mrgentax
  • cl4m
  • spollini
  • fedeava
  • guidop91
  • nithya10
  • abigaild45
  • mb_dev
  • emilyatk
  • luis_gomez
  • karmenn
  • khiaraortiz
  • dmitriy_komarov-cn
  • jmcamacho
  • ishita_tiwari
  • hariprasath_cn
  • iamsuneeth
  • srikanthns
  • pushp-1992
  • sudiptacondenast
  • utkarsh_sanjivan-conde
  • mallica
  • shobith
  • agururajan
  • upenpanging
  • rjain198
  • ashwini_uppar
  • pmasade
  • jbergdol
  • suresharam
  • dhanraj_cn
  • pratik-gupta
  • ilankumaran
  • samtiffin
  • adityaanand534
  • chandan-condenast
  • vijayalakshmi-sunagar
  • namankumarsinha
  • sumitmaurya21
  • pashoka
  • nidhiarya-condenast
  • smita2022
  • ankushvijay93
  • likhita
  • priyankanandi
  • soumyagundu
  • cnrahulrkr
  • ramya-cn
  • dikshita_khandke
  • ambay_chaurasia
  • manikandancn
  • rghvndr99
  • mulla2