Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mutator): restore greater than/less than in jsonmatch constraints #5063

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading