Gathering detailed insights and metrics for react-validated-proxy
Gathering detailed insights and metrics for react-validated-proxy
Gathering detailed insights and metrics for react-validated-proxy
Gathering detailed insights and metrics for react-validated-proxy
(unmaintained, don't use) React <Validate> component based on ES6 Proxy.
npm install react-validated-proxy
Typescript
Module System
Node Version
NPM Version
TypeScript (65.69%)
JavaScript (16.08%)
CSS (16.01%)
HTML (2.22%)
Total Downloads
1,194
Last Day
1
Last Week
3
Last Month
22
Last Year
178
MIT License
8 Stars
29 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jan 28, 2023
Minified
Minified + Gzipped
Latest Version
0.1.0
Package Id
react-validated-proxy@0.1.0
Unpacked Size
17.81 kB
Size
6.61 kB
File Count
19
NPM Version
5.6.0
Node Version
8.10.0
Cumulative downloads
Total Downloads
Last Day
-66.7%
1
Compared to previous day
Last Week
-40%
3
Compared to previous week
Last Month
37.5%
22
Compared to previous month
Last Year
-34.6%
178
Compared to previous year
3
4
An approach to form validation in React that makes use of ES6 Proxy, by way of @poteto's validated-proxy
library.
react-validated-proxy
exports a single component <Validate>
, which you can use to validate changes to any object.
Note: this library is in alpha and is not ready for production use.
1$ npm i react-validated-proxy
1import Validate from 'react-validated-proxy'; 2import { isPresent, hasLength } from './validations'; 3 4const user = { 5 firstName: 'Billy', 6 lastName: 'Bob', 7 // ... 8}; 9 10const Adult = { 11 firstName: [isPresent(), hasLength({ min: 2 })], 12 lastName: [isPresent(), hasLength({ min: 2 })], 13 // ... 14}; 15 16<Validate model={user} as={Adult}> 17 {({ model, set, reset, isPristine, hasErrors, flush }) => ( 18 <form onSubmit={flush}> 19 <input type="text" value={model.firstName} onChange={e => set('firstName', e.target.value)} /> 20 <input type="text" value={model.lastName} onChange={e => set('lastName', e.target.value)} /> 21 <button type="submit" disabled={isPristine || hasErrors}>Save</button> 22 <button type="button" onClick={reset}>Reset</button> 23 </form> 24 )} 25</Validate>
See the full code for this example under example/
. You can also clone this repo and run npm install && npm start
to see the example running live.
ES6 Proxies have many cool use cases – one of which is validating changes to objects. You can modify an object as you normally would, and a Proxy
can intercept those modifications to add custom behavior.
In the case of this library, the <Validate>
component accepts a model
object, as well as a map of validations
in the format defined by validated-proxy
. <Validate>
will then wrap your model
in a Proxy, and pass the Proxy to your children
render prop. You can then interact with the Proxy as your model
, but with the added benefit of knowing when and why changes to the model
are invalid. (Specifically, you can easily retrieve error messages from the Proxy.)
react-validated-proxy
owes its existence to validated-proxy
and ember-changeset
by Lauren Tan. It's mostly an experiment in distilling ember-changeset
to its framework-agnostic core.
<Validate model={T} as={IValidationMap}>...</Validate>
Pass in a data model that you want to validate, as well as a validation map of validators for your data model. The validation map should be in the format expected by validated-proxy
.
1const user = { 2 firstName: 'Jim', 3 lastName: 'Bob', 4 age: 15, 5}; 6 7const Adult = { 8 firstName: [isPresent(), hasLength({ min: 2 })], 9 lastName: [isPresent(), hasLength({ min: 2 })], 10 age: isNumber({ op: '>=', value: 18 }), 11}; 12 13<Validate model={user} as={Adult}> 14 {({ model, set, reset, isPristine, hasErrors, flush }) => ( 15 <form>{/* ... */}</form> 16 )} 17</Validate>
<Validate>
accepts a render prop as its children
, and will pass the wrapped model
(and some helper properties and functions) to the render prop:
1interface HelperProps { 2 model: BufferedProxy 3 set: <T>(name: string, value: T) => void 4 reset: () => void 5 isPristine: boolean 6 hasErrors: boolean 7 flush: () => void 8}
Jason Tu · GitHub @nucleartide · Twitter @nucleartide
No vulnerabilities found.
No security vulnerabilities found.