
Research
/Security News
Contagious Interview Campaign Escalates With 67 Malicious npm Packages and New Malware Loader
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
use-validation
Advanced tools
Offering the most concise and elegant form validation API known to humankind, without sacrificing performance.
Weighing in at a whopping 1.1 kB gzipped and minified.
npm install use-validation
or
yarn add use-validation
const { fields, handleSubmit } = useValidation({
initialValues: { foo: '', bar: '' },
})
return (
<Wrapper>
<h1>Validation using hūX</h1>
<form
onSubmit={e => {
e.preventDefault()
handleSubmit()
}}
>
<Input {...fields.foo} name="foo" />
<Input {...fields.bar} name="bar" />
<Button type="submit">Submit</Button>
</form>
</Wrapper>
)
Try this example on CodeSandbox!
It's one object with six properties (and only one is required).
This snippet shows the API in its entirety:
const myValidationFunc = (
{ foo, bar },
{ someRandomThing, someOtherJunk },
) => ({
foo:
typeof foo !== 'number'
? 'Please enter a number'
: foo === someOtherJunk
? `foo cannot be ${someOtherJunk}`
: null,
bar: bar === someRandomThing ? `bar cannot be ${someRandomThing}` : null,
})
const { fields, handleSubmit } = useValidation({
initialValues: { foo: 'default value', bar: '' },
validate: myValidationFunc,
validationOptions: {
someRandomThing: 'asdf',
someOtherJunk: 123,
},
onSubmit: (values, { someRandomThing }) => {
updateUser(values)
doOtherThings({ someRandomThing })
},
defaultErrorMessage: `This won't have any effect in this case since we're using a custom validation function.`,
forceShowOnSubmit: false,
})
Name | Type | Description | Default Value | Required? |
---|---|---|---|---|
initialValues | { [string]: [any] } | An object with field names as keys and their initial values as values. Only field names in this object will be returned by the hook, so include the field here even if the initial value is undefined . | Yes | |
validate | function | A function that will receive two arguments: an object with field values by name and validationOptions. Should return an object with errors (or any falsy value to represent no error) by field name. | Simple, truthy validation function | No |
validationOptions | any | Allows any arbitrary second argument to be passed to validate and onSubmit . Used primarily for looking at values outside of your fields when validating. | No | |
onSubmit | function | Called by the hook when handleSubmit is invoked and all fields are valid. Passed the same arguments as validate . | No | |
defaultErrorMessage | string | The error message that will be set to a field by the default validation function. | "Looks like that didn't work. Please try again." | No |
forceShowOnSubmit | boolean | Causes touched to be set to true for all fields when handleSubmit is invoked. | true | No |
An object with two keys: fields
and handleSubmit
.
fields
is an object that contains the following keys for each field you passed in:
value
: the value of the fieldtouched
: whether the field has been blurrederror
: any error message returned from the validate function for this fieldonChange
: call this with either a value or an event to update the field's valueonBlur
: call this to set touched to true
for the fieldhandleSubmit
is a function that will call onSubmit
(if it was passed). If forceShowOnSubmit is true, touched
will be set to true on all fields when handleSubmit
is invoked.
FAQs
Astonishingly easy form validation for React using hooks
The npm package use-validation receives a total of 2 weekly downloads. As such, use-validation popularity was classified as not popular.
We found that use-validation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.