2.0.2 • Published 9 months ago

json-schema-matcher v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

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

OperatorDescriptionExample Link
equalsMatches if the value equals the expected valueExample
notEqualsMatches if the value does not equal the expected valueExample
greaterThanMatches if the value is greater than the expected valueExample
lessThanMatches if the value is less than the expected valueExample
existsChecks if the field exists or notExample
inListChecks if the value is in the listExample
notInListChecks if the value is not in the listExample
startsWithChecks if the string starts with the expected substringExample
endsWithChecks if the string ends with the expected substringExample
orConditionsMatches if at least one condition is trueExample
andConditionsMatches if all conditions are trueExample
referenceFieldMatches if the value equals another field in the documentExample
notConditionNegates the conditionExample

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

2.0.2

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago