{"id":20135538,"url":"https://github.com/raisely/json-conditions","last_synced_at":"2025-04-09T17:32:43.652Z","repository":{"id":39851732,"uuid":"291662599","full_name":"raisely/json-conditions","owner":"raisely","description":"Simple conditional logic to evaluate against javascript objects","archived":false,"fork":false,"pushed_at":"2022-09-15T16:07:14.000Z","size":38,"stargazers_count":21,"open_issues_count":2,"forks_count":6,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-23T19:39:12.212Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/raisely.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-31T08:46:32.000Z","updated_at":"2025-02-05T15:11:45.000Z","dependencies_parsed_at":"2022-07-15T06:16:15.209Z","dependency_job_id":null,"html_url":"https://github.com/raisely/json-conditions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fjson-conditions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fjson-conditions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fjson-conditions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fjson-conditions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/raisely","download_url":"https://codeload.github.com/raisely/json-conditions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248077557,"owners_count":21043984,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T21:15:24.850Z","updated_at":"2025-04-09T17:32:43.534Z","avatar_url":"https://github.com/raisely.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Simple conditional logic testing of JSON objects\n\nSimple json takes an array of conditions that can be compared against a JSON object to test if that object passes or fails the conditions.\n\n# Quickstart\n\n```sh\nnpm install json-conditions\n```\n\n```javascript\nconst checkConditions = require('json-conditions');\n\nconst reference = {\n\ttopLevelNumber: 3,\n\tuser: {\n\t\tpreferredName: 'Alex',\n\t\tage: 4,\n\t},\n\ttoy: {\n\t\tname: 'Model Train',\n\t\tprevTracks: 5,\n\t\ttracks: 18,\n\t\tengines: 1\n\t\tbattery: true,\n\t\tpreviousOwners: ['Alice', 'Ahmed'],\n\t\tbatteryStatus: [{\n\t\t\ttype: 'AA',\n\t\t\tcharge: 'empty',\n\t\t}, {\n\t\t\ttype: 'AA',\n\t\t\tcharge: 'full',\n\t\t}]\n\t}\n};\n\nconst simpleRules = [\n\t{ property: 'toy.engines', op: 'gt', value: 2 },\n\t{ property: 'batteries', op: 'eq', value: true },\n];\n\n// Returns true\ncheckConditions({\n\trules: simpleRules,\n\tsatisfy: 'ANY',\n\tlog: console.log,\n}, reference);\n\n// Returns false\ncheckConditions({\n\trules: simpleRules,\n\tsatisfy: 'ALL',\n\tlog: console.log,\n}, reference);\n\n// Returns true\ncheckConditions({\n\trules: [\n\t\t// A required condition must always be satisfied regardless of the value\n\t\t{ property: 'toy.tracks', op: 'gt', value: 2, required: true },\n\t\t{ property: 'batteries', op: 'eq', value: true },\n\t\t{ property: 'solarPanels', op: 'gte', value: 0 },\n\t],\n\tsatisfy: 'ANY',\n\tlog: console.log,\n}, reference);\n\n// Returns true\ncheckConditions({\n\trules: [\n\t\t// transformValueFn causes value to be substituted with ref[value], which is reference.topLevelNumber, which is 3\n\t\t{ property: 'toy.tracks', op: 'gt', value: 'topLevelNumber' },\n\t],\n\tsatisfy: 'ANY',\n\tlog: console.log,\n\ttransformValueFn: (val) =\u003e ref[val],\n}, reference);\n\n// Array rules - all return true\ncheckConditions({\n\trules: [\n\t\t// A required condition must always be satisfied regardless of the value\n\t\t{ property: 'toy.previousOwners[]', op: 'some', value: 'Alice' },\n\t],\n\tsatisfy: 'ANY',\n\tlog: console.log,\n}, reference);\n\ncheckConditions({\n\trules: [\n\t\t// A required condition must always be satisfied regardless of the value\n\t\t{ property: 'toy.batteryStatus[].type', op: 'all', value: 'AA' },\n\t],\n\tsatisfy: 'ANY',\n\tlog: console.log,\n}, reference);\n\ncheckConditions({\n\trules: [\n\t\t{ property: 'toy.tracks', op: 'crosses', value: 10 },\n\t],\n\tsatisfy: 'ANY',\n\tlog: console.log,\n\tpreviousValueFn: (ref) =\u003e ref.toy.prevTracks,\n}, reference);\n\ncheckConditions({\n\trules: [\n\t\t// A required condition must always be satisfied regardless of the value\n\t\t{ property: 'toy.batteryStatus[].type', op: 'none', value: 'AAA' },\n\t],\n\tsatisfy: 'ANY',\n\tlog: console.log,\n}, reference);\n```\n\n### Parameters\n\n| Param                     | Type     | Default | Description                                                                                                                                                        |\n| ------------------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| settings.log              | function |         | Optional function to log debug output from the evaluation                                                                                                          |\n| settings.rules            | object[] |         | Rules, see below                                                                                                                                                   |\n| settings.satisfy          | string   | ANY     | How many rules must be satisfied to pass, 'ALL' or 'ANY'                                                                                                           |\n| settings.previousValueFn  | function |         | Function that returns a previous value, takes arguments (reference, rule.property)                                                                                 |\n| settings.transformValueFn | function |         | If defined, the return value from settings.transformValueFn(rule.value, reference, rule.property) will be subsituted for rule.value when performing the comparison |\n| reference                 | object   |         | The javascript object to evaluate the rules against                                                                                                                |\n\n#### Rules\n\nEach rule is described by an object with the following properties\n| property | Type | Default | Description |\n|---|---|---|---|\n| op | string | | The logical operator to use for comparison (see below) |\n| property | string | | The property in the reference object to check (evaluated by [lodash.get()](https://lodash.com/docs/4.17.15#get) |\n| required | boolean | false | If true, this rule must always evaluate to true for the object to pass the conditions |\n| value | \\* | | Value to compare the property to |\n\nProperty is passed to [lodash.get](https://lodash.com/docs/4.17.15#get) to lookup the value in the object.\nSo effectively the rules are evaluated to `get(reference, rule.property) ${rule.op} ${rule.value}`\n\n#### Operators\n\nThe following operators can be used in rules. Operators use javascript coersion (ie == not ===)\nAdditionally, we assume that rule values may have come from a form, and so try to be forgiving when dealing with\nbooleans. If the value of the property is a boolean, then the strings 'true' and 'false' (case insensitive) will\nbe converted to booleans.\n\n| Operator   | Javascript operation                           | Notes              |\n| ---------- | ---------------------------------------------- | ------------------ |\n| eq         | ==                                             |                    |\n| neq        | !=                                             |                    |\n| ne         | !=                                             | (Alias for neq)    |\n| gt         | \u003e                                              |                    |\n| gte        | \u003e=                                             |                    |\n| lt         | \u003c                                              |                    |\n| lte        | \u003c=                                             |                    |\n| crosses    | Greater than, but previous value was less than | (See below)        |\n| absent     | !                                              |                    |\n| empty      | !                                              | (Alias for absent) |\n| present    | !!                                             |                    |\n| startsWith | \\_.toString(x).startsWith()                    |                    |\n| endsWith   | \\_.toString(x).endsWith()                      |                    |\n| contains   | \\_.toString(x).includes()                      |                    |\n\n# Array Syntax\n\nTo check arrays for matches use `[]` in the property path to indicate that the preceeding path is an array.\nYou can specify further paths to reference into if the array contains an object\neg `toy.batteryStatus[].type` in the example at the top\n\n| Operator | Javascript operation      | Notes                                       |\n| -------- | ------------------------- | ------------------------------------------- |\n| none     | !x.includes(value)        | Value is not in the array                   |\n| some     | x.includes(value)         | Value is present at least once in the array |\n| all      | !x.find(i =\u003e i !== value) | Every entry in the array matches value      |\n\n# Crosses\n\nSometimes it's not enough to know if a property is greater than a given value, but you want to know if this is\nthe first time it has risen above that value.\nFor example, if you were running conditions against updates to some data and you want the condition to pass\nthe first time a counter passes 10, but not after that. You could pass in an object like `{ oldCount, newCount }`\nand use the crosses operator.\n\nWhen using the crosses operator, you must pass in a `previousValueFn` that will return a previous value that can\nbe used to check if the value has crossed the intended value\n\n## License\n\nLicensed under the [NoHarm license](https://github.com/raisely/noharm)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraisely%2Fjson-conditions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fraisely%2Fjson-conditions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraisely%2Fjson-conditions/lists"}