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

validate using another fields value? #426

Open
hongkongkiwi opened this issue Sep 10, 2024 · 1 comment
Open

validate using another fields value? #426

hongkongkiwi opened this issue Sep 10, 2024 · 1 comment

Comments

@hongkongkiwi
Copy link

hongkongkiwi commented Sep 10, 2024

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?

@tywalch
Copy link
Owner

tywalch commented Sep 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants