1.2.1 • Published 10 years ago

strulidator v1.2.1

Weekly downloads
6
License
MIT
Repository
github
Last release
10 years ago

strulidator

Declarative validator for complex deep structures.

npm.io npm.io npm.io

npm.io npm.io npm.io npm.io

npm.io npm.io npm.io

npm.io npm.io

npm.io npm.io

npm.io

TODO

At current moment strulidator isn't ready for production. If you want to help me with release please read the first issue.

Reference

type Validator = (value: any) => boolean

type Constraint = (value: any) => ConstraintResult

type ConstraintResult = {
  valid: boolean,
  message: string | void,
  children: ConstraintResultMap | ConstraintResultArray | void
}

type ConstraintResultMap = {
  [key: string]: ConstraintResult
}

type ConstraintResultArray = Array<ConstraintResult>

Usage

const signInValidator = combineConstraints({
  keys: createObjectKeysConstraint({
    email: true,
    password: true,
    items: false,
  }),
  values: createObjectConstraint({
    email: combineConstraints({
      notNull,
      notEmpty,
      string,
      email,
    }),
    password: string,
    items: combineConstraints({
      notEmpty,
      array: createArrayConstraint(
        string
      )
    })
  })
})

print(signInValidator({
  email: 'vslinko@yahoo.com',
  password: '',
  items: ['']
}))
{ valid: true,
  message: null,
  children:
   { keys: { valid: true, message: null, children: null },
     values:
      { valid: true,
        message: null,
        children:
         { email:
            { valid: true,
              message: null,
              children:
               { notNull: { valid: true, message: null, children: null },
                 notEmpty: { valid: true, message: null, children: null },
                 string: { valid: true, message: null, children: null },
                 email: { valid: true, message: null, children: null } } },
           password: { valid: true, message: null, children: null },
           items:
            { valid: true,
              message: null,
              children:
               { notEmpty: { valid: true, message: null, children: null },
                 array:
                  { valid: true,
                    message: null,
                    children: [ { valid: true, message: null, children: null } ] } } } } } } }
1.2.1

10 years ago

1.2.0

10 years ago

1.1.4

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.0

10 years ago