Skip to content

Instantly share code, notes, and snippets.

@rudfoss
Created March 10, 2021 12:38
Show Gist options
  • Save rudfoss/b57fb59232a50186b53711635fad6e6c to your computer and use it in GitHub Desktop.
Save rudfoss/b57fb59232a50186b53711635fad6e6c to your computer and use it in GitHub Desktop.
A recursive schema for building conditions within Sanity.io Condition functions must be implemented as separate input components and handled accordingly, but the basic structure is present.
export const conditionFunction = {
title: "Condition function",
name: "conditionFunction",
type: "object",
fields: [
{
title: "Function name",
name: "name",
type: "string",
description: "The type of condition",
options: {
list: [
{
title: "Existence",
value: "existence"
},
{
title: "Equality",
value: "equality"
}
]
},
validation: (rule) => rule.required()
},
{
title: "Options",
name: "options",
type: "string",
description: "JSON representation of the condition options."
}
]
}
export const conditionGroup = {
title: "Condition group",
name: "conditionGroup",
type: "object",
fields: [
{
title: "Relation",
name: "relation",
type: "string",
description:
'Choose whether any or all of the conditions within this group must be met. If not specified "All" is used',
options: {
list: [
{
title: "All (default)",
value: "all"
},
{
title: "Any",
value: "any"
}
]
}
},
{
title: "Conditions",
name: "conditions",
type: "array",
of: [
{
title: "Nested condition group",
name: "subConditionGroup",
type: "conditionGroup"
},
{
title: "Condition function",
name: "conditionFn",
type: "conditionFunction"
}
]
}
],
preview: {
select: {
relation: "relation"
},
prepare: ({ relation = "" }) => ({
title: "Condition group",
subtitle: relation
})
}
}
export const condition = {
title: "Condition",
name: "condition",
type: "object",
fields: [
{
title: "Item",
name: "onItem",
type: "string",
description: "Reference to the item in the form this condition will apply to.",
validation: (rule) => rule.required()
},
{
title: "Conditions",
name: "conditions",
type: "conditionGroup"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment