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

Support Array validation #13

Open
kettanaito opened this issue Jun 25, 2020 · 4 comments
Open

Support Array validation #13

kettanaito opened this issue Jun 25, 2020 · 4 comments

Comments

@kettanaito
Copy link
Member

What

Need to support Array validation.

  1. Array as a value of object's property.
  2. Root-level Array.

How

  • Have a "mask" approach, where all Array members must satisfy a predicate.
  • Have an index-based approach, where each Array member may have unique predicate.
@marcosvega91
Copy link

marcosvega91 commented Jul 30, 2020

For arrays you can use this approach. What do you have in mind?

useSchema(
  {
    'users.name': (value, pointer) => value.length > 10,
  },
  {
    users: [
      name: 'Marco'
   ],
  },
)

@kettanaito
Copy link
Member Author

That looks like a good start, @marcosvega91!

I was thinking that useSchema should be able to accept a root-level array as well. Something like:

useSchema([1, 2, 3], [1, 2, 3]) // OK!

The complexity comes with describing a spec of expected data.

// Validate each Array member against a predicate.
useSchema([1, 2, 3], (value) => !isNaN(value)) 

// Validate each Array member against a unique predicate.
useSchema(
  [1, 'admin', { firstName: 'John' }], 
  () => [
    (value) => !isNaN(value),
    (value) => value === 'admin',
    (value) => ({
    // Regular schema validation
      firstName: value => value === 'John'
    })
  ]
)

It gets more complex, when you wish to validate a portion of Array against a general predicate, and a portion against exact predicate functions.

I think we should approach this from the specification point of view, defining what features should be supported.

@marcosvega91
Copy link

Array of course should be implemented 💯
I'm thinking about the use of array with multiple predicate...mmmm.. Do you have a real example of this use?

@kettanaito
Copy link
Member Author

Validating a tuple, for example:

type UserInfo = [string, number]

const user: UserInfo = ['John', 32]

useSchema(user, [validateUsername, validateAge])

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