validate using another fields value?
After reading through the docs it wasn't immediately obvious how I would do this, so I thought I would ask.
How would I go about validating using a different field? For example, I have two properties:
Properties: {
required: true,
type: 'map',
properties: {
Channels: {
type: 'set',
items: 'number',
default: [0, 1],
required: false,
},
DiarizationEnabled: {
type: 'boolean',
default: false,
required: false,
}
}
}
I would like to ensure that if DiarizationEnabled is true, then Channels is only allowed to have length 1.
Is it possible to use validation for this? I've got kind of a workaround of this below, but it's a bit ugly:
Properties: {
required: true,
type: 'map',
properties: {
Channels: {
type: 'set',
items: 'number',
default: [0, 1],
required: false,
watch: ["DiarizationEnabled"],
set: (value, {DiarizationEnabled}) => {
if (DiarizationEnabled && value && value.length > 1) {
throw new Error("Only a single channel is allowed when 'DiarizationEnabled' is true")
}
return value;
}
},
DiarizationEnabled: {
type: 'boolean',
default: false,
required: false,
watch: ["Channels"],
set: (value, {Channels}) => {
if (Channels && Channels.length > 1) {
throw new Error("DiariazationEnabled cannot be set to true when we are using more than one channel")
}
return value;
}
}
}
}
Any better way?
This type of validation is likely better achieved with condition checks on write. The set callback will only ever have access to the values provided to the client, and because DynamoDB allows operations like add and remove on a Set Attribute, there'd be no way to evaluate the updated length in application code; it must be done via a condition check.
In practice, it would look like this