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

@trantoanvan/js-struct-validator

v1.0.6

Published

js library helps validate an object follow a predefined specification structure

Downloads

111

Readme

js-struct-validator

js library helps validate an object follow a predefined structure.

Usage Guide

Table of Contents

  1. Structure Overview
  2. Validation Process
  3. Basic Usage
  4. Usage Cases
  5. StructValidator Requirements Specification
  6. Development

Structure Overview

The validation structure uses native JavaScript objects with a special syntax to define data structure's schemas:

  • Field names ending with $ are required.
  • type specifies the expected data type (string, number, boolean, object, array).
  • default specifies a default value for the field.
  • For objects, use properties to define nested fields.
  • For arrays, use items to define the structure of array elements.

Validation Process

The validation process involves the following steps:

  1. Retrieve the event structure from a structure.
  2. Use StructValidator to validate the event data against the structure.
  3. The validator checks:
    • Presence of all required fields (marked with $)
    • Correct data types for all fields
    • Validity of nested objects and arrays
    • Application of default values where necessary

If any validation errors occur, an error is thrown with a descriptive message indicating the nature and location of the error in the event data.

Basic Usage

import StructValidator from '@trantoanvan/js-struct-validator'

// Define an event structure
const eventStructure = {
    user$: {
        id$: 'string',
        name: 'string',
        age: 'number'
    },
    action$: 'string'
};

const eventData = {
  user: {
    id: "user123",
    name: "John Doe",
    age: 30
  },
  action: "login"
};

// Validate an event
try {
  StructValidator.validate(eventData, eventStructure); 
  console.log("Event is valid");
} 
catch (error) {
  console.error("Validation error:", error.message);
}

Usage Cases

const SimpleEvent = {
    name$: 'string',
    timestamp$: 'number',
    value: 'number'
};

// Valid event
const validSimpleEvent = {
  name: "Simple Event",
  timestamp: 1623456789,
  value: 42
};

// Invalid event (missing required field)
const invalidSimpleEvent = {
  name: "Invalid Simple Event",
  value: 42
};

import StructValidator from '@trantoanvan/js-struct-validator'

StructValidator.validate(validSimpleEvent, SimpleEventSpecification); // do not throw error
StructValidator.validate(invalidSimpleEvent, SimpleEventSpecification); // throw error: missing timestamp field 

More about Specification

Nested Objects

const NestedEvent = {
    user$: {
        id$: 'string',
        name: 'string',
        age: 'number'
    },
    action$: 'string'
};

// Valid nested event
const validNestedEvent = {
  user: {
    id: "user123",
    name: "John Doe",
    age: 30
  },
  action: "login"
};

// Invalid nested event (missing required nested field)
const invalidNestedEvent = {
  user: {
    name: "Jane Doe",
    age: 25
  },
  action: "logout"
};

Arrays

const ArrayEvent = {
    items$: [{
        id$: 'string',
        quantity: 'number'
    }],
    total$: 'number'
};

// Valid array event
const validArrayEvent = {
  items: [
    { id: "item1", quantity: 2 },
    { id: "item2", quantity: 1 }
  ],
  total: 3
};

// Invalid array event (missing required field in array item)
const invalidArrayEvent = {
  items: [
    { id: "item1", quantity: 2 },
    { quantity: 1 }
  ],
  total: 3
};

Optional Fields

const OptionalFieldsEvent = {
    id$: 'string',
    name: 'string',
    description: 'string'
};

// Valid event with optional fields
const validOptionalEvent = {
  id: "event123",
  name: "Optional Event"
};

// Also valid (includes all fields)
const anotherValidOptionalEvent = {
  id: "event456",
  name: "Another Optional Event",
  description: "This field is optional"
};

Default Values

const DefaultValueEvent = {
    type$: 'string = standard',
    priority: 'number = 1'
};

// Valid event using defaults
const validDefaultEvent = {
  type: "standard"
};

// Valid event overriding defaults
const invalidDefaultEvent = {
  type: "special",
  priority: 1
};

const invalidDefaultEvent = {
  type: "standard",
  priority: 3
};

Mixed Types

const MixedTypeEvent = {
    id$: 'string',
    data$: {
        value: 'number',
        tags: ['string'],
        active: 'boolean'
    },
    timestamp$: 'number'
};

// Valid mixed type event
const validMixedTypeEvent = {
  id: "mixed123",
  data: {
    value: 42.5,
    tags: ["important", "urgent"],
    active: true
  },
  timestamp: 1623456789
};

// Invalid mixed type event (wrong type for array item)
const invalidMixedTypeEvent = {
  id: "mixed456",
  data: {
    value: 30,
    tags: ["critical", 123], // 123 is not a string
    active: false
  },
  timestamp: 1623456790
};

StructValidator Requirements Specification

1. Purpose

The StructValidator class is designed to validate data against a specified structure, ensuring that the data conforms to the expected types and constraints.

2. Structure Definition

The structure can be defined in the following ways:

2.1. Primitive Types:

  • Defined as a string: 'string', 'number', 'boolean'
  • Example: 'string'

2.2. Arrays:

  • Defined as an array with a single element describing the structure of array items
  • Example: ['string'], [{ id: 'string' }]
  • Required array could not be empty, null or undefined.

2.3. Objects:

  • Defined as an object with key-value pairs
  • Example: { id: 'string', name: 'string' }
  • Required object could not be empty, null or undefined.

2.4. Nested Structures:

  • Any combination of the above
  • Example: { users: [{ id: 'string', age: 'number' }] }

3. Field Specifications

3.1. Required Fields:

  • Indicated by appending '$' to the field name
  • Example: { id$: 'string' }

3.2. Optional Fields:

  • Regular field names without '$'
  • Example: { name: 'string' }

3.3. Default Values:

  • Specified after the type, separated by '='
  • Example: { status: 'string = active|inactive' }

4. Validation Rules

4.1. Type Checking:

  • Values must match the specified type

4.2. Required Fields:

  • Must be present in the data
  • Cannot be undefined or null

4.3. Optional Fields:

  • Can be missing from the data
  • Can be undefined or null

4.4. Default Values:

  • If specified, the value must match one of the provided options

4.5. Arrays:

  • Each item in the array is validated against the specified item structure

4.6. Objects:

  • Each present field is validated against its specification
  • Missing optional fields are ignored

5. Error Handling

5.1. Error Throwing:

  • Errors are thrown immediately upon encountering a validation failure
  • Validation stops at the first error

5.2. Error Messages:

  • Include the path of the invalid field
  • Describe the nature of the validation failure

6. Special Cases

6.1. Top-level Structures:

  • Can be primitive types, arrays, or objects without field names
  • Example: 'string', ['string'], { id: 'string' }

6.2. Nested Structures:

  • Follow the same rules as top-level structures

6.3. Empty Arrays:

  • Allowed if the field is not required

6.4. Null Values:

  • Allowed for optional fields
  • Not allowed for required fields

7. Method Specifications

7.1. validate(data, structure):

  • Entry point for validation
  • Throws an error if validation fails

Development

Installation

Test


npm run test

Build


npm run build

Publish to NPM

NPM Guilde