Skip to content

Commit

Permalink
fix(mutator): restore greater than/less than in jsonmatch constraints
Browse files Browse the repository at this point in the history
Fixes #2474
  • Loading branch information
rexxars committed Oct 24, 2023
1 parent 6fee81b commit 36306d5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/@sanity/mutator/src/jsonpath/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ const attributeCharMatcher = /^[a-zA-Z0-9_]$/
const attributeFirstCharMatcher = /^[a-zA-Z_]$/

const symbols: Record<SymbolClass, string[]> = {
// NOTE: These are compared against in order of definition,
// thus '==' must come before '=', '>=' before '>', etc.
operator: ['..', '.', ',', ':', '?'],
comparator: ['>', '>=', '<', '<=', '==', '!='],
comparator: ['>=', '<=', '<', '>', '==', '!='],
keyword: ['$', '@'],
boolean: ['true', 'false'],
paren: ['[', ']'],
Expand Down
30 changes: 30 additions & 0 deletions packages/@sanity/mutator/test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
46 changes: 46 additions & 0 deletions packages/@sanity/mutator/test/patchExamples/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down

0 comments on commit 36306d5

Please sign in to comment.