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

json-schema-matcher

v2.0.2

Published

A flexible JSON schema matcher for validating API responses using full operator names.

Downloads

168

Readme

JSON Schema Matcher

npm version

A flexible JSON schema matcher for validating API responses using full operator names.

Installation

npm install json-schema-matcher

Usage

Here’s how you can use the module to match JSON responses against a schema.

Basic Example

const { matchSchema } = require('json-schema-matcher');

// Sample JSON response
const response = {
    "status": "OK",
    "items": [
        { "status": "SUCCESS", "code": 123 },
        { "status": "FAILED", "code": 200 }
    ],
    "results": {
        "list": [
            { "name": "item1", "value": 50 },
            { "name": "item2", "value": 75 }
        ]
    }
};

// Schema for matching the response
const schema = {
    "match": "any",
    "path": {
        "status": { "equals": "OK" },
        "items[0].status": { "notEquals": "ERROR" },
        "items[1].code": { "greaterThan": 100, "lessThan": 300 },
        "items[1].status": { "inList": ["FAILED", "ERROR"] },
        "results.list[0].name": { "startsWith": "item", "contains": "item" },
        "results.list[2]": { "exists": false },
        "items[0].code": { "notCondition": { "equals": 200 } }
    }
};

console.log(matchSchema(response, schema)); // Will output true or false

Supported Operators

| Operator | Description | Example Link | |-------------------|---------------------------------------------------------|----------------------------------------------------------------| | equals | Matches if the value equals the expected value | Example | | notEquals | Matches if the value does not equal the expected value | Example | | greaterThan | Matches if the value is greater than the expected value | Example | | lessThan | Matches if the value is less than the expected value | Example | | exists | Checks if the field exists or not | Example | | inList | Checks if the value is in the list | Example | | notInList | Checks if the value is not in the list | Example | | startsWith | Checks if the string starts with the expected substring | Example | | endsWith | Checks if the string ends with the expected substring | Example | | orConditions | Matches if at least one condition is true | Example | | andConditions | Matches if all conditions are true | Example | | referenceField | Matches if the value equals another field in the document| Example | | notCondition | Negates the condition | Example |

Operator Examples

1. equals

const response = { "status": "OK" };
const schema = { 
  "match": "all",
  "path": { 
    "status": { "equals": "OK" }
  }
};
console.log(matchSchema(response, schema)); // true

2. notEquals

const response = { "status": "FAILED" };
const schema = { 
  "match": "all",
  "path": { 
    "status": { "notEquals": "SUCCESS" }
  }
};
console.log(matchSchema(response, schema)); // true

3. greaterThan

const response = { "score": 90 };
const schema = { 
  "match": "all",
  "path": { 
    "score": { "greaterThan": 80 }
  }
};
console.log(matchSchema(response, schema)); // true

4. lessThan

const response = { "age": 25 };
const schema = { 
  "match": "all",
  "path": { 
    "age": { "lessThan": 30 }
  }
};
console.log(matchSchema(response, schema)); // true

5. exists

const response = { "name": "John" };
const schema = { 
  "match": "all",
  "path": { 
    "name": { "exists": true },
    "age": { "exists": false }
  }
};
console.log(matchSchema(response, schema)); // true

6. inList

const response = { "role": "admin" };
const schema = { 
  "match": "all",
  "path": { 
    "role": { "inList": ["admin", "moderator"] }
  }
};
console.log(matchSchema(response, schema)); // true

7. notInList

const response = { "role": "guest" };
const schema = { 
  "match": "all",
  "path": { 
    "role": { "notInList": ["admin", "moderator"] }
  }
};
console.log(matchSchema(response, schema)); // true

8. startsWith

const response = { "name": "John" };
const schema = { 
  "match": "all",
  "path": { 
    "name": { "startsWith": "Jo" }
  }
};
console.log(matchSchema(response, schema)); // true

9. endsWith

const response = { "name": "Johnson" };
const schema = { 
  "match": "all",
  "path": { 
    "name": { "endsWith": "son" }
  }
};
console.log(matchSchema(response, schema)); // true

10. orConditions

const response = { "status": "OK" };
const schema = { 
  "match": "all",
  "path": { 
    "status": { 
      "orConditions": [
        { "equals": "FAILED" },
        { "equals": "OK" }
      ] 
    }
  }
};
console.log(matchSchema(response, schema)); // true

11. andConditions

const response = { "score": 95 };
const schema = { 
  "match": "all",
  "path": { 
    "score": { 
      "andConditions": [
        { "greaterThan": 90 },
        { "lessThan": 100 }
      ] 
    }
  }
};
console.log(matchSchema(response, schema)); // true

12. referenceField

const response = { "createdAt": "2022-10-01", "updatedAt": "2022-10-01" };
const schema = { 
  "match": "all",
  "path": { 
    "updatedAt": { "referenceField": "createdAt" }
  }
};
console.log(matchSchema(response, schema)); // true

13. notCondition

const response = { "status": "FAILED" };
const schema = { 
  "match": "all",
  "path": { 
    "status": { 
      "notCondition": { "equals": "SUCCESS" }
    }
  }
};
console.log(matchSchema(response, schema)); // true

License

MIT