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

Conditional validation type #38

Closed
OldSneerJaw opened this issue Jul 11, 2018 · 1 comment
Closed

Conditional validation type #38

OldSneerJaw opened this issue Jul 11, 2018 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@OldSneerJaw
Copy link
Owner

OldSneerJaw commented Jul 11, 2018

Feature Request

Description

Currently each property or element can be defined with only one validation type (e.g. string, integer, datetime, etc.). However, there are cases where it may be beneficial to allow a value to be one of several types (e.g. a date as either an ISO 8601 date/time string or as a Unix timestamp) while still being able to set some constraints on them.

Add a new validation type (e.g. conditional) that selects one of several different validators to apply to a property or element based on some condition (specified as a function).

Examples

propertyValidators: {
  date: {
    type: 'conditional',
    required: true,
    skipValidationWhenValueUnchangedStrict: true,
    validationCandidates: [
      {
        condition: function(doc, oldDoc, currentItemEntry, validationItemStack) {
          return typeof currentItemEntry.itemValue === 'string';
        },
        validator: {
          type: 'datetime',
          minimumValue: '1970-01-01T00:00Z'
        }
      },
      {
        condition: function(doc, oldDoc, currentItemEntry, validationItemStack) {
          return typeof currentItemEntry.itemValue === 'number';
        },
        validator: {
          type: 'integer',
          minimumValue: 0
        }
      }
    ]
  }
}
@OldSneerJaw OldSneerJaw added the enhancement New feature or request label Jul 11, 2018
@OldSneerJaw OldSneerJaw self-assigned this Aug 27, 2018
OldSneerJaw added a commit that referenced this issue Aug 27, 2018
The `conditional` validation type allows an item's value to conform to any one of several candidate validators. Each validator is accompanied by a condition that specifies whether to apply the validator to the item's value. If none of the conditions are satisfied, the value is rejected. This allows, for example, an array to contain a mix of data types or an object property to expect a different string format based on the state of other properties of the object.
dkichler added a commit that referenced this issue Aug 31, 2018
Issue #38: Implementation of conditional validation type
@dkichler
Copy link
Collaborator

Addressed by #39

OldSneerJaw added a commit that referenced this issue Sep 1, 2018
Verifies that an outer conditional validator's constraints are inherited by inner candidate validators regardless of whether they are static or dynamic constraints.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants