Giter Club home page Giter Club logo

obey's People

Contributors

bdaniels11 avatar dependabot[bot] avatar devsli avatar ksafranski avatar psvet avatar stephen-belcher-ta avatar theprivileges avatar tomfrost avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

obey's Issues

Schema def prop `allow` support definition objects

Since a schema property can be shared it can create a single-source for configurations. As such, one area where this applies well is allow; it's accepted values and any metadata associated with those, for example:

status: { type: 'string', allow: [ 'prog', 'comp', 'arch' ] }

Maintaining enums in a separate location for the allow "keys" ([ 'prog', 'comp', 'arch' ]) presents the challenge of maintaining this in multiple locations.

Proposed Solution

Currently allow supports either a string or an array. The proposal would be to expand this to also allow an object:

status: { type: 'string', allow: { 'prog': 'in progress', 'comp': 'completed', 'arch': 'archived' } }

The above would simply process to allow the keys of the object as values, but the whole object could then be maintained in a lib (or in the model) and shared easily.

Types: array, allow empty

Type array should have allowEmpty property to allow placeholder array to be set in a property or represent 0-length collection.

Account for dynamic keys in nested conditional requires

In cases where objects are validated against values, any keys are allowed. If a value is dependent on another nested inside an object, the requiredIf or requiredIfNot rules use dot notation for finding the target value (requiredIfNot: 'rootObj.someVal'). This requires a user to explicitly declare the key name in the model definition, which does not play well with dynamic keys.

const model = obey.model({
  admin: {
    type: 'object',
    values: {
      type: 'object',
      required: true,
      keys: {
        valA: {
          type: 'string',
          requiredIfNot: 'valB' // This would check for a top-level valB prop,
        },                      // instead of the intended admin.<whateverKey>.valB
        valB: {
          type: 'string',
          requiredIfNot: 'valA' // and vice versa
        }
      }
    }
  }
})

Add 'any' type

Allows an assert that a field exists in any form, without specifying what it should be.

Fields not required failing

Despite setting required: false the following fails lname because the type check runs and fails. Need to only check present fields, then run check if required fields are missing.

const test = obey.model({
    fname: { type: 'string' },
    lname: { type: 'string', required: false }
});

test.validate({ fname: 'bob' })

Custom methods on model?

Hi, this project looks great!

Is it possible to add custom methods to a model, something like the following?

userModel.methods.getFullName = function() {
    return this.fname + ' ' + this.lname;
}

Make `types` expandable with qualifiers

For common types which have the possibility of multiple variants, allow : delimited pass-through for specific validation settings

For example; phone numbers could use type phone or more specifically phone:us, phone:uk, etc to indicate specific regional format.

Bypass default type check if allow: null exists

We have a number of fields (mostly timestamps) that should allow a null value, but because they're specified as type: 'number' in the obey models, they throw validation errors. The current work-around is including a custom type for every field that accepts null along with its main type, but if allow: null is passed, the default type check should be bypassed.

Non-string zip codes with leading 0 invalid

const model = obey.model({
  zip: { type: 'zip' }
})

await model.validate({ zip: 02680 }) ๐Ÿ‘Ž// ValidationError: zip (2680): Value must be a valid US zip code
await model.validate({ zip: '02680' }) ๐Ÿ‘ 

Both should be valid.

Conditional Requires

Allow properties to specify a conditional require based on presence of another property's value:

Example:

const user = Obey.model({
  // ...
  phone: { type: 'phone', description: 'User phone number' },
  phoneType: { type: 'string', requireIf: 'phone', 'Type of phone, i.e. cell or home' }
  // ...
})

The phoneType property is only required if phone is supplied.

Should support nesting using dot notation 'parent.child' which refers to the parent object-type with nested child property.

Conditional requires in arrays of objects

const model = obey.model({
  list: {
    type: 'array',
    values: {
      type: 'object',
      keys: {
        someQualifier: {
          type: 'string',
          allow: [ 'a', 'b', 'c' ]
        },
        someDependentProp: {
          type: 'string',
          requiredIfNot: { 'list[thisIndex].someQualifier': 'b' } // This won't work
        }
      }
    }
  }
})

Error Response Objects; include validation type

For error response objects, currently:

{ key: 'fname', value: 1234, message: 'Value must be a string' }

Expand to include validation rule that failed:

{ type: 'string', sub: 'default', key: 'fname', value: 1234, message: 'Value must be a string' }

This will allow those parsing the error to setup rules-based responses.

Creators/modifiers that return null values fail with allowNull

obey.modifier('returnNullIfInvalid', val => {
  return val && arrayOfValidValues.includes(val) ? val : null
})

const model = obey.model({
  name: { type: 'string', modifier: 'returnNullIfInvalid', allowNull: true }
})

model.validate({ name: 'some invalid string' }) // 'Value must be a string'

More permissive email regex

The email regex included with obey does not match many valid emails. I've created a regexr with valid email addresses for example: http://regexr.com/3d9cg

This was copied to one of our FE apps for validation, though it is too restrictive to use for validation as it would prevent users from submitting valid email addresses. We may use it as a warning, but I think we should consider a more permissive rule that does not rule out valid email addresses. If we can get a rule that does not rule out any valid email, then we can use it for submission validation.

New feature request: "equalsTo" validator

When I validate registration form, I want to validate password confirmation with schema like this:

let model = obey.model({
	/* Unrelated fields omitted */
	password: {
		type: 'string',
		min: 8
	},
	passwordConfirm: {
		type: 'string',
		equalsTo: 'password'
	}
});

Enforce strict validation by default

When objects are validated we need a way to enforce strict checking to ensure other key-val's don't come through. Should have a flag for disabling strict-mode.

Will need to be applied to object rules, but also inherit from a model definition.

Using `allow` with `empty` is broken for strings

Because the allow rule is evaluated before empty, empty strings will be erroneously declared invalid.

const model = obey.model({
  foo: { type: 'string', allow: [ 'bar' ], empty: true }
})
model.validate({ foo: '' }) // Error: Value '' is not allowed

Warnings for non-native properties

Currently there's no way to tell if an invalid property is supplied, so for instance; if you misspelled modifer: 'someVal' there's no indicator that the property won't be acted upon. Since we want to allow custom properties my suggestion is to "throw" a warning if the key in a definition is not recognized, then create a obey.customProps(<array|string>) that allows for centrally declaring custom props to prevent the warning.

Phone Validation Strictness: "(aaa) bbb-cccc" is invalid

Besides some obvious current limitations that have already been discussed (such as extensions), the regex also fails on phone numbers formatted in the fairly standard (aaa) bbb-cccc style.

(111) 222-3333 // fails
(111)222-3333 // fails (probably less common)
(111)-222-3333 // passes

Ironically my computer recognizes the first two as phone numbers, but not the last one.

'default' validator doesn't work for booleans

Specifically when the default is true, but the record's value is false:

obey.model({
  enabled: { type: 'boolean', default: true }
})

// then elsewhere
validate({ enabled: false }) ==> { enabled: true }

Need to add a type check before returning the final value (for numbers as well, in case of 0).

initData is not updated

When you use requireIf, requireIfNot or equalsTo (soon) then you may find weird issue - first value of referenced fields are not updated.

Example

let schema = obey.model({
	phone: { type: "string" },
	phoneType: { type: "string", requireIf: "phone" }
});

schema.validate({ phone: "123" }).then(() => {
	console.error('Bad: Form is invalid, you should not see this message')
})
.catch(() => {

	let muchNewSchema = obey.model({
		phone: { type: "string" },
		phoneType: { type: "string", requireIf: "phone" }
	});

	muchNewSchema.validate({}).then(() => {
		console.log('Good: Bug is fixed');
	})
	.catch((e) => {
		console.error('Bad: Form is valid, you should not see this message');
		console.error(e.message);
	})

})

Result:

- Bad: Form is valid, you should not see this message
- phoneType (undefined): Value required because 'phone' exists

The problem is when you run validation second time - obey.rules.initData is already set so it doesn't updated.
https://github.com/TechnologyAdvice/obey/blob/master/src/rules.js#L85

Should type `number` fail on NaN?

Just browsin' through the source code and wanted to point it out just in case it wasn't intentional:

https://github.com/TechnologyAdvice/obey/blob/master/src/types/number.js

export default (context) => {
  if (typeof context.value !== 'number') {
    context.fail('Value must be a number')
  }
}
import isNumber from 'types/number'

isNumber(Number('foo')) // passes

If NaN isn't handled here, perhaps an additional type to catch this case? I've been bitten by this in the past and didn't want it to slip through.

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.