New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

deku-prop-types

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deku-prop-types - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

75

lib/index.js

@@ -7,5 +7,2 @@ 'use strict';

return {
get name() {
return name;
},
get isRequired() {

@@ -17,9 +14,13 @@ this.required = true;

if (this.required && prop === undefined) {
throw new Error(key + ' is required');
return new Error(key + ' is required');
}
if (prop !== undefined) {
if (typeof validator === 'string' && (typeof prop === 'undefined' ? 'undefined' : _typeof(prop)) !== validator || typeof validator === 'function' && !validator(prop)) {
throw new TypeError(key + ' should be of type `' + name + '`');
if (typeof validator === 'string' && (typeof prop === 'undefined' ? 'undefined' : _typeof(prop)) !== validator) {
return new TypeError(key + ' should be of type `' + name + '`');
}
if (typeof validator === 'function') {
return validator(prop, key);
}
}

@@ -34,9 +35,12 @@

get any() {
return checkerFactory('any', function (prop) {
return prop !== undefined;
return checkerFactory('any', function () {
return null;
});
},
get array() {
return checkerFactory('array', function (prop) {
return Array.isArray(prop);
return checkerFactory('array', function (prop, key) {
if (!Array.isArray(prop)) {
return new TypeError(key + ' should be of type `array`');
}
return null;
});

@@ -46,7 +50,11 @@ },

return function (validator) {
return checkerFactory('arrayOf', function (prop) {
prop.forEach(function (p) {
return validator.validate(p);
return checkerFactory('arrayOf', function (prop, key) {
var anyErrors = prop.some(function (p) {
return validator.validate(p) instanceof Error;
});
return true;
if (anyErrors) {
return new TypeError(key + ' does not consist of the correct type');
}
return null;
});

@@ -67,2 +75,30 @@ };

},
get oneOf() {
return function (allowedValues) {
return checkerFactory('oneOf', function (prop, key) {
var isAllowed = allowedValues.some(function (value) {
return value === prop;
});
if (!isAllowed) {
return new TypeError(key + ' is not one of the allowed values');
}
return null;
});
};
},
get oneOfType() {
return function (allowedTypes) {
return checkerFactory('oneOfType', function (prop, key) {
var isAllowed = !allowedTypes.every(function (type) {
return type.validate(prop) instanceof Error;
});
if (!isAllowed) {
return new TypeError(key + ' is not one of the allowed types');
}
return null;
});
};
},
get string() {

@@ -75,7 +111,14 @@ return checkerFactory('string', 'string');

Object.keys(propTypes).forEach(function (key) {
var result = undefined;
if (typeof propTypes[key] === 'function') {
propTypes[key](props, key);
result = propTypes[key](props, key);
} else {
propTypes[key].validate(props[key], key);
result = propTypes[key].validate(props[key], key);
}
if (result instanceof Error) {
throw result;
}
return null;
});

@@ -82,0 +125,0 @@ };

{
"name": "deku-prop-types",
"version": "0.1.2",
"version": "0.2.0",
"description": "Prop type validation for Deku components",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -58,3 +58,3 @@ # deku-prop-types

import {element} from 'deku'
import {validate} from 'deku-prop-types'
import {propTypes, validate} from 'deku-prop-types'

@@ -80,2 +80,35 @@ const NamesList = ({props}) => <div>

Validate prop is an object
### propTypes.oneOf
Validate prop is one of the allowed values
```jsx
import {element} from 'deku'
import {propTypes, validate} from 'deku-prop-types'
const Color = ({props}) => <div>{props.color}</div>
Color.propTypes = {
color: propTypes.oneOf(['red', 'green', 'blue'])
}
export default validate(Color)
```
### propTypes.oneOfType
Validate prop is one of the allowed types
```jsx
import {element} from 'deku'
import {propTypes, validate} from 'deku-prop-types'
const Age = ({props}) => <div>{props.age}</div>
Age.propTypes = {
age: propTypes.oneOfType([propTypes.number, propTypes.number])
}
export default validate(Age)
```
### propTypes.string

@@ -100,4 +133,6 @@ Validate prop is a string

if (props[propName] < 10) {
throw new Error('count must be less than 10')
return new Error('count must be less than 10')
}
return null
}

@@ -104,0 +139,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc