better-prop-types
Advanced tools
Comparing version 1.0.0-alpha.5 to 1.0.0-alpha.6
@@ -0,1 +1,13 @@ | ||
# [1.0.0-alpha.6](https://github.com/ivangabriele/better-prop-types/compare/v1.0.0-alpha.5...v1.0.0-alpha.6) (2021-11-21) | ||
### Features | ||
* attach new modifiers to any validator ([968c05a](https://github.com/ivangabriele/better-prop-types/commit/968c05a2b49e8857e94650d2cf177395253e5e70)) | ||
* attach new modifiers to arrayOf & objectOf validators ([73fa84a](https://github.com/ivangabriele/better-prop-types/commit/73fa84ae752df3e95ee3367a7d13cc8fe467ee94)) | ||
* attach new modifiers to exact & shape validators ([c4a8b65](https://github.com/ivangabriele/better-prop-types/commit/c4a8b654fd14917ec61ea8f86229a858f32b9228)) | ||
* attach new modifiers to instanceOf validator ([6d97cc7](https://github.com/ivangabriele/better-prop-types/commit/6d97cc7661cd100314aef4cbde347b51cd947be3)) | ||
* attach new modifiers to node validator ([ebf5253](https://github.com/ivangabriele/better-prop-types/commit/ebf5253da787c0e726ef00686ab84e292befcf8f)) | ||
* attach new modifiers to oneOfType validator ([4a41a6d](https://github.com/ivangabriele/better-prop-types/commit/4a41a6d900203d7adef312ed874d025f107cf1c4)) | ||
# [1.0.0-alpha.5](https://github.com/ivangabriele/better-prop-types/compare/v1.0.0-alpha.4...v1.0.0-alpha.5) (2021-11-21) | ||
@@ -2,0 +14,0 @@ |
@@ -35,5 +35,5 @@ import PropTypes from 'prop-types'; | ||
const chainedCheckType = checkType.bind(null, false); | ||
chainedCheckType.isNullable = checkType.bind(null, 'NULLABLE'); | ||
chainedCheckType.isRequiredButNullable = checkType.bind(null, 'NULLABLE'); | ||
chainedCheckType.isRequired = checkType.bind(null, 'REQUIRED'); | ||
chainedCheckType.isNotNull = checkType.bind(null, 'UNNULLABLE'); | ||
chainedCheckType.isOptionalButNotNull = checkType.bind(null, 'UNNULLABLE'); | ||
return chainedCheckType; | ||
@@ -49,13 +49,13 @@ } | ||
const symbol = createBetterChainableTypeChecker(PropTypes.symbol); | ||
const any = Object.assign(PropTypes.any); | ||
const arrayOf = Object.assign(PropTypes.arrayOf); | ||
const any = createBetterChainableTypeChecker(PropTypes.any); | ||
const arrayOf = type => createBetterChainableTypeChecker(PropTypes.arrayOf(type)); | ||
const element = createBetterChainableTypeChecker(PropTypes.element); | ||
const elementType = createBetterChainableTypeChecker(PropTypes.elementType); | ||
const instanceOf = Object.assign(PropTypes.instanceOf); | ||
const node = Object.assign(PropTypes.node); | ||
const objectOf = Object.assign(PropTypes.objectOf); | ||
const instanceOf = expectedClass => createBetterChainableTypeChecker(PropTypes.instanceOf(expectedClass)); | ||
const node = createBetterChainableTypeChecker(PropTypes.node); | ||
const objectOf = type => createBetterChainableTypeChecker(PropTypes.objectOf(type)); | ||
const oneOf = types => createBetterChainableTypeChecker(PropTypes.oneOf(types)); | ||
const oneOfType = Object.assign(PropTypes.oneOfType); | ||
const shape = Object.assign(PropTypes.shape); | ||
const exact = Object.assign(PropTypes.exact); | ||
const oneOfType = type => createBetterChainableTypeChecker(PropTypes.oneOfType(type)); | ||
const shape = type => createBetterChainableTypeChecker(PropTypes.shape(type)); | ||
const exact = type => createBetterChainableTypeChecker(PropTypes.exact(type)); | ||
const BetterPropTypes = { | ||
@@ -62,0 +62,0 @@ any, |
@@ -1,28 +0,29 @@ | ||
import PropTypes, { InferProps, InferType, Requireable, ValidationMap, Validator } from 'prop-types'; | ||
import PropTypes, { InferProps, InferType, ValidationMap, Validator } from 'prop-types'; | ||
import { Isable } from './types'; | ||
declare type InstanceOf = <T>(expectedClass: new (...args: any[]) => T) => Requireable<T>; | ||
declare type ObjectOf = <T>(type: Validator<T>) => Requireable<{ | ||
export declare type ArrayOfValidator = <T>(type: Validator<T>) => Isable<T[]>; | ||
export declare type InstanceOfValidator = <T>(expectedClass: new (...args: any[]) => T) => Isable<T>; | ||
export declare type ObjectOfValidator = <T>(type: Validator<T>) => Isable<{ | ||
[K in keyof any]: T; | ||
}>; | ||
declare type OneOf = <T>(types: ReadonlyArray<T>) => Isable<T>; | ||
declare type OneOfType = <T extends Validator<any>>(types: T[]) => Requireable<NonNullable<InferType<T>>>; | ||
declare type Shape = <P extends ValidationMap<any>>(type: P) => Requireable<InferProps<P>>; | ||
declare type Exact = <P extends ValidationMap<any>>(type: P) => Requireable<Required<InferProps<P>>>; | ||
export declare type OneOfValidator = <T>(types: ReadonlyArray<T>) => Isable<T>; | ||
export declare type OneOfTypeValidator = <T extends Validator<any>>(types: T[]) => Isable<NonNullable<InferType<T>>>; | ||
export declare type ShapeValidator = <P extends ValidationMap<any>>(type: P) => Isable<InferProps<P>>; | ||
export declare type ExactValidator = <P extends ValidationMap<any>>(type: P) => Isable<Required<InferProps<P>>>; | ||
declare const BetterPropTypes: { | ||
any: PropTypes.Requireable<any>; | ||
any: Isable<any>; | ||
array: Isable<any[]>; | ||
arrayOf: <T>(type: PropTypes.Validator<T>) => PropTypes.Requireable<T[]>; | ||
arrayOf: ArrayOfValidator; | ||
bool: Isable<boolean>; | ||
element: Isable<PropTypes.ReactElementLike>; | ||
elementType: Isable<PropTypes.ReactComponentLike>; | ||
exact: Exact; | ||
exact: ExactValidator; | ||
func: Isable<(...args: any[]) => any>; | ||
instanceOf: InstanceOf; | ||
node: PropTypes.Requireable<PropTypes.ReactNodeLike>; | ||
instanceOf: InstanceOfValidator; | ||
node: Isable<PropTypes.ReactNodeLike>; | ||
number: Isable<number>; | ||
object: Isable<object>; | ||
objectOf: ObjectOf; | ||
oneOf: OneOf; | ||
oneOfType: OneOfType; | ||
shape: Shape; | ||
objectOf: ObjectOfValidator; | ||
oneOf: OneOfValidator; | ||
oneOfType: OneOfTypeValidator; | ||
shape: ShapeValidator; | ||
string: Isable<string>; | ||
@@ -29,0 +30,0 @@ symbol: Isable<symbol>; |
import { Requireable, Validator } from 'prop-types'; | ||
export interface Isable<T> extends Requireable<T | undefined | null> { | ||
isNotNull: Validator<T | undefined>; | ||
isNullable: Validator<T | null>; | ||
isOptionalButNotNull: Validator<T | undefined>; | ||
isRequiredButNullable: Validator<T | null>; | ||
} |
{ | ||
"name": "better-prop-types", | ||
"description": "Better PropTypes.", | ||
"version": "1.0.0-alpha.5", | ||
"version": "1.0.0-alpha.6", | ||
"license": "MIT", | ||
@@ -14,7 +14,5 @@ "type": "module", | ||
"test": "yarn test:lint && yarn test:type && yarn test:unit", | ||
"test:build": "jest --maxWorkers=50%", | ||
"test:lint": "eslint --ext ts .", | ||
"test:type": "tsc --noEmit", | ||
"test:unit": "jest --maxWorkers=50%", | ||
"test:watch": "yarn test:unit --watch" | ||
"test:unit": "jest" | ||
}, | ||
@@ -21,0 +19,0 @@ "dependencies": { |
@@ -8,14 +8,25 @@ # better-prop-types | ||
**better-prop-types** is a wrapper for the original **[prop-types][lnk-prop-types]** library adding a few more validators and | ||
modifiers to accurately handle `null` and `undefined` values (which shouldn't be considered similar!). | ||
The code is fully test-covered and actively used in production. | ||
The library includes the original `prop-types` as a dependency. | ||
And that's important to note that **better-prop-types** is only bundled as a full JavaScript module (ESM). | ||
## Features | ||
- Add `.isNotNull` and `.isNullable` modifiers to primitive validators, besides the original `.isRequired` prop: | ||
- `.isNotNull` keeps the prop optional but rejects `null` (= accepts `undefined` but not `null` values) | ||
- `.isNullable` marks the prop as required but accepts `null` (= accepts `null` but not `undefined` values) | ||
- Add `.isOptionalButNotNull` and `.isRequiredButNullable` modifiers to all validators, besides the original | ||
`.isRequired` one: | ||
- `.isOptionalButNotNull` keeps the prop as **optional** (accepting `undefined`) but rejects `null` values | ||
- `.isRequired` marks the prop as **required** but rejects both `null` & `undefined` values | ||
- `.isRequiredButNullable` marks the prop as **required** (rejecting `undefined`) but accepts `null` values | ||
## Usage | ||
## Installation | ||
### Installation | ||
```sh | ||
npm i -E better-prop-types | ||
yarn add -E better-prop-types@alpha | ||
``` | ||
@@ -26,6 +37,6 @@ | ||
```sh | ||
yarn add -E better-prop-types | ||
npm i -E better-prop-types@alpha | ||
``` | ||
## Example | ||
### Example | ||
@@ -37,4 +48,4 @@ ```ts | ||
anOptionalButNonNullStringProp = 'A default string', | ||
aRequiredAndNonNullableBooleanProp, | ||
aRequiredButNullableNumberProp, | ||
aRequiredAndNonNullableBooleanProp, | ||
}) => ( | ||
@@ -45,14 +56,15 @@ // ... | ||
MyComponent.propTypes = { | ||
anOptionalButNonNullStringProp: BetterPropTypes.string.isNotNull, | ||
aRequiredButNullableNumberProp: BetterPropTypes.number.isNullable, | ||
anOptionalButNonNullStringProp: BetterPropTypes.string.isOptionalButNotNull, | ||
aRequiredAndNonNullableBooleanProp: BetterPropTypes.bool.isRequired, | ||
aRequiredButNullableNumberProp: BetterPropTypes.number.isRequiredButNullable, | ||
} | ||
``` | ||
You can also use them with all the functional validators: `objectOf(/* */).isRequiredButNullable`, `shape(/* */).isOptionalButNotNull`, etc. | ||
## Roadmap | ||
- Add `.isNotNull` and `.isNullable` modifiers to ALL validators | ||
- Integrate some [prop-types-extra](https://github.com/react-bootstrap/prop-types-extra) extra types: | ||
- `all(...validators)` => `BetterPropsTypes.all(...validators)` | ||
- `deprecated(validator, reason)` => `BetterPropsTypes.string.isDeprecated(reason)` | ||
- `deprecated(validator, reason)` => `BetterPropsTypes.isDeprecated(validator, reason)` | ||
- `isRequiredForA11y(validator)` => `BetterPropsTypes.string.isRequiredForA11y` | ||
@@ -65,6 +77,7 @@ | ||
[img-license]: https://img.shields.io/github/license/ivangabriele/better-prop-types?style=flat-square | ||
[img-npm]: https://img.shields.io/npm/v/better-prop-types?style=flat-square | ||
[img-npm]: https://img.shields.io/npm/v/better-prop-types/alpha?style=flat-square | ||
[lnk-codecov]: https://codecov.io/gh/ivangabriele/better-prop-types/branch/alpha | ||
[lnk-github]: https://github.com/ivangabriele/better-prop-types/actions?query=branch%3Aalpha++ | ||
[lnk-license]: https://github.com/ivangabriele/better-prop-types/blob/alpha/LICENSE | ||
[lnk-npm]: https://www.npmjs.com/package/better-prop-types | ||
[lnk-npm]: https://www.npmjs.com/package/better-prop-types/v/alpha | ||
[lnk-prop-types]: https://github.com/facebook/prop-types |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24252
24
133
79