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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@domain-schema/validation

v0.0.36

Published

Validation for Domain Schema

Readme

Domain Schema Validation

npm version Twitter Follow

Installation

yarn add @domain-schema/validation

or

npm install @domain-schema/validation

Validation

Domain Validation intended to check values whether they comply with structure of domain schema fields.

Usage

Firstly import DomanValidator from domain-schema validation package

    import DomainValidator from '@domain-schema/validation'

Then call DomanValidator static method validate

    DomainValidator.validate(schema, values)

With such params:

  • schema - DomainSchema instance
  • values - Formatted object from form or another source with values which DomainValidator will check on conformity of DomainSchema
    • key - Field name correspond to schema field
    • value - Entity which must pass validation
       { name : "Jack" }

You can register domain-schema field validation using following approaches:

  • Type defining
     isAdmin: {
         type: Boolean
     }
  • Build-in validators
  • Custom validators

For more accurate validation you could use several approaches simultaneously

Built-in validators

  • optional - By default, all keys are required. Set optional: true to change that.

      name = {
        ...
        optional: true,
        ...
      }
    • matches - Checks if the value matches some specific field
      password = {
        ...
      };
      passwordConfirmation = {
        ...
        matches: 'password',
        ...
      }
    • max - Checks if the value or value length does not exceed a specified number (works with both numbers and strings)
      // Checks length if type is String...
      name = {
        ...
        type: String,
        max: 6,
        ...
      }
      // ...and value when type is Number
      age = {
        ...
        type: Number,
        max: 16,
        ...
      }
    • min - Checks if the value or value length not less then a specified number (works with both numbers and strings)
      // Checks length if type is String...
      name = {
        ...
        type: String,
        min: 6,
        ...
      }
      // ...and value when type is Number
      age = {
        ...
        type: Number,
        min: 16,
        ...
      }
    • email - Checks if the value corresponds to an email
      name = {
        ...
        email: true,
        ...
      }
    • alphaNumeric - Checks if the value consists of alphanumeric characters
      text = {
        ...
        alphaNumeric: true,
        ...
      }
    • phoneNumber - Checks if the value corresponds to a phone number
      phone = {
        ...
        phoneNumber: true,
        ...
      }
    • equals - Checks if the value equals to a specific value
      role = {
        ...
        equals: 'admin',
        ...
      }

Nested Schema Validation

Be careful when establishing relation via schema field on your domain schema

class Product extends Schema {
  __ = { name: 'Product', tablePrefix: '' };
  id = DomainSchema.Int;
  name = {
    type: String,
    searchText: true
  };
  category = {
    type: Category
   };
}
class Category extends Schema {
 __ = { name: 'Category', tablePrefix: '' };
 id = DomainSchema.Int;
 name = {
   type: String,
   searchText: true,
 };
 products = {
   type: [Product]
 };
}

In this case domainValidator also will check all fields of nested Category schema and then Product again.

So object must have the following structure in order to pass validation

{
  id : 1,
  name : 'Iphone X'
  category : {
    id : 1,
    name : 'Phones',
    products : ...
  }
}

To prevent check of nested schema use blackbox attribute

  products = {
    type: [Product],
    blackbox: true
  };

Now the following object will pass validation

{
  id : 1,
  name : 'Iphone X'
  category : {
    id : 1,
    name : 'Phones'
  }
}

Customizing Validation Messages

Validation error messages can be overridden by defining messages in one place as a single object and passing it to the setValidationMessages function. In that object the keys are validator names and values can be either strings or functions, as follows:

  • Values are strings:
    import DomainValidation from '@domain-schema/validation';

    DomainValidation.setValidationMessages({
      required: 'This field is required',
      phoneNumber: 'Error! Not a phone number!'
    });
  • Values are callback functions:
    import DomainValidation from '@domain-schema/validation';

    DomainValidation.setValidationMessages({
      required: ({fieldName}) => {
        return `Field '${fieldName}' is required`
      },
      phoneNumber: ({values, fieldName}) => {
        return `Error! '${values[fieldName]}' is not a phone number`
      }
    });

Callback functions, in turn, get object with the following properties:

  • values - the validation object
  • fieldName - the current field name
  • schema - the domain schema definition object

We can also define a custom validation error message for a specific field right in the schema:

    ...
    name = {
        type: String,
        ...
        required: {
          value: true,
          msg: 'Required Name'
        }
    }
    ...

Custom validators

A user is able to add any number of custom validators whenever it need by passing functions to the validators property of a schema field:

    password = {
        type: String,
        ...
        validators: [(value, values) => {
          return value.length > 3 ? undefined : 'Must Be more than 3 characters';
        }]
    };

Validation callback function gets the following params:

  • value - the value for being validated
  • values - the form object

NOTE: validation function must return a string with an error message or undefined.

License

Copyright © 2018 SysGears INC. This source code is licensed under the MIT license.