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
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