diff --git a/packages/@sanity/mutator/src/jsonpath/tokenize.ts b/packages/@sanity/mutator/src/jsonpath/tokenize.ts index 238efda26ed..7f64ffeb22e 100644 --- a/packages/@sanity/mutator/src/jsonpath/tokenize.ts +++ b/packages/@sanity/mutator/src/jsonpath/tokenize.ts @@ -14,8 +14,10 @@ const attributeCharMatcher = /^[a-zA-Z0-9_]$/ const attributeFirstCharMatcher = /^[a-zA-Z_]$/ const symbols: Record = { + // NOTE: These are compared against in order of definition, + // thus '==' must come before '=', '>=' before '>', etc. operator: ['..', '.', ',', ':', '?'], - comparator: ['>', '>=', '<', '<=', '==', '!='], + comparator: ['>=', '<=', '<', '>', '==', '!='], keyword: ['$', '@'], boolean: ['true', 'false'], paren: ['[', ']'], diff --git a/packages/@sanity/mutator/test/parse.test.ts b/packages/@sanity/mutator/test/parse.test.ts index dbafdc1fe9d..372f3517aa9 100644 --- a/packages/@sanity/mutator/test/parse.test.ts +++ b/packages/@sanity/mutator/test/parse.test.ts @@ -151,6 +151,36 @@ const cases = { ], type: 'union', }, + 'variants[stock >= 20].stock': { + type: 'path', + nodes: [ + { + type: 'attribute', + name: 'variants', + }, + { + type: 'union', + nodes: [ + { + type: 'constraint', + operator: '>=', + lhs: { + type: 'attribute', + name: 'stock', + }, + rhs: { + type: 'number', + value: 20, + }, + }, + ], + }, + { + type: 'attribute', + name: 'stock', + }, + ], + }, } Object.keys(cases).forEach((path) => { diff --git a/packages/@sanity/mutator/test/patchExamples/set.ts b/packages/@sanity/mutator/test/patchExamples/set.ts index 08ff855fe9f..b70cce235cb 100644 --- a/packages/@sanity/mutator/test/patchExamples/set.ts +++ b/packages/@sanity/mutator/test/patchExamples/set.ts @@ -112,6 +112,52 @@ const examples: PatchExample[] = [ ], }, }, + { + name: 'Attribute greater than or equal filter', + before: { + variants: [ + {name: 'a', stock: 20}, + {name: 'b', stock: 30}, + {name: 'c', stock: 10}, + ], + }, + patch: { + id: 'a', + set: { + 'variants[stock >= 20].stock': 5, + }, + }, + after: { + variants: [ + {name: 'a', stock: 5}, + {name: 'b', stock: 5}, + {name: 'c', stock: 10}, + ], + }, + }, + { + name: 'Attribute less than or equal filter', + before: { + variants: [ + {name: 'x', stock: 99}, + {name: 'y', stock: 50}, + {name: 'z', stock: 10}, + ], + }, + patch: { + id: 'a', + set: { + 'variants[stock <= 50].stock': 5, + }, + }, + after: { + variants: [ + {name: 'x', stock: 99}, + {name: 'y', stock: 5}, + {name: 'z', stock: 5}, + ], + }, + }, { name: 'Set new key', before: {},