Coder Social home page Coder Social logo

hannibal's People

Contributors

oliverbrooks avatar orangemug avatar pasupulaphani avatar phpnode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

phpnode orangemug

hannibal's Issues

Renaming keys

Suggested API for renaming keys

"rename": {
  "foo.bar": "foo.baz"
}

foo.bar is the path in the object so for example the following

{
  foo: {
    bar: 3
  }
}

Would end up as

{
  foo: {
    baz: 3
  }
}

Transforms make valid invalid inputs

Example:

var simpleValidator = hannibal.create({
  type: 'object',
  schema: {
    shouldBeNumber: {
      type: 'number',
      transforms: 'toFloat'
    }
  }
});
simpleValidator({
  shouldBeNumber: 'notAnumber'
}).isValid; //returns true

Date fails too:

var simpleValidator2 = hannibal.create({
  type: 'object',
  schema: {
    shouldBeDate: {
      type: 'date',
      transforms: 'toDate'
    }
  }
});
simpleValidator2({
  shouldBeDate: 'notAdate'
}).isValid; //true again

Suggestion: Send whole object and path to transforms so they can be conditional on other aspects of an object

Currently transforms have a signature of (value, opts).

Suggest we pass two extra args so it's (value, opts, wholeObject, path). Where:

  • value = the value being validated
  • opts = options passed to the transform
  • wholeObject = the entire object (from root level) being validated
  • path = a lodash.get style object path such as items[0].url

This means if you have a custom transform or validator which depends on another value in the object it can be obtained.

For example, with the object:

{
  type: "fish",
  payload: {
    name: "Nemo",
    fins: 2
  }
}

The payload may want to be validated differently based on the type.

{
  type: "marsupial",
  payload: {
    name: "Skippy",
    pouchDepth: 43
  }
}

Non standard and unserialisable data structure returned from nested schemas

See https://runkit.com/eliothowes1090/hannibal-error-example for example of issue.

Given the following schema:

{
    type: 'object',
    schema: {
        foo: {
            type: 'array',
            validators: {
                max: 2
            },
            schema: {
                type: 'string',
                validators: {
                    enum: [
                        'fish', 
                        'chips'
                    ]
                }
            }
        }
    }
}

And the following data structure:

{
    foo: [
      '',
      'not fish',
      'not chips'
    ]
}

The following error object is returned:

foo: [
  {enum: "string:  is not one of: fish, chips"},
  {enum: "string: not chips is not one of: fish, chips"},
  {enum: "string: not chips is not one of: fish, chips"},
  max: "array is too long, requires: 2 items and was: 3"
]

Suggestion: Allow an array of schemas

If a schema is given such as:

{
  payload: {
    type: "object",
    schemas: [
      // Schema for a link with number of clicks
      {
        url: {
          type: "string"
        },
        clickCount: {
          type: "number"
        }
      },
      // schema for a user
      {
        firstName: {
          type: "string"
        },
        lastName: {
          type: "string"
        }
      }
    ]
  }
}

Then it will allow items through which pass one or more schemas.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.