Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
vue3-form-validation
Advanced tools
Opinionated Vue composition function for Form Validation.
npm install vue3-form-validation
Validation is async and is utilising Promise.allSettled
, which has not yet reached cross-browser stability. Example usage can be found in this Code Sandbox.
This package exports one function useValidation
, plus some type definitions for when using TypeScript.
const {
form,
errors,
submitting,
validateFields,
resetFields,
add,
remove
} = useValidation<T>(formData);
useValidation
takes the following parameters:formData
object
true
formData
.The formData
object has a structure that is similar to any other object you would write for v-model
data binding. The only difference being that together with every value you can provide rules to display validation errors.
Let's look at an example how the structure of some formData
object can be converted to an object with the addition of rules:
const formData = {
name: '',
email: '',
password: ''
};
// The above can be converted to the following
const formDataWithRules = {
name: {
$value: '',
$rules: [name => !name && 'Name is required']
},
email: {
$value: '',
$rules: [email => !email && 'E-Mail is required']
},
password: {
$value: '',
$rules: [
pw => pw.length > 7 || 'Password has to be longer than 7 characters'
]
}
};
The formData
object can contain arrays and can be deeply nested. At the leaf level, the object should contain Form Fields whose simplified type definition looks like the following:
type Field<T> = {
$value: Ref<T> | T;
$rules?: Rule<T>[];
};
To get better type inference while writing the useValidation
function, it's recommended to define the structure of your formData
upfront and pass it as the generic parameter T
. The type for the example above is pretty straightforward:
type FormData = {
name: Field<string>;
email: Field<string>;
password: Field<string>;
};
useValidation
exposes the following state:form
object
formData
object.submitting
Ref<boolean>
True
during validation after calling validateFields
.errors
ComputedRef<string[]>
Form
is a reactive object with identical structure as the formData
input, but with added metadata to every Form Field.
type TransformedField<T> = {
$uid: number;
$value: T;
$errors: string[];
$validating: boolean;
$onBlur(): void;
};
// The type of form in the example above would therefore be
const form: {
name: TransformedField<string>;
email: TransformedField<string>;
password: TransformedField<string>;
};
As you may have noticed, all of the properties are prefixed with the $
symbol, which is to distinguish them from other properties but also to avoid naming conflicts.
$uid
number
key
attribute in v-for
.$value
T
modelValue
of the Form Field which is meant to be used together with v-model
.$errors
string[]
$validating
boolean
True
while at least one rule is validating.$onBlur
function
useValidation
exposes the following methods:validateFields() -> Promise
Promise
which will reject if there are validation errors, and resolve with the formData
otherwise.resetFields() -> void
add(pathToArray: (string | number)[], value: any) -> void
pathToArray
- Tuple representing the path to an array in the formData
.value
- The value that will be pushed to the array at the given path.remove(pathToArray: (string | number)[], index: number) -> void
pathToArray
- Tuple representing the path to an array in the formData
.index
- Array index that will be remove.Rules are functions that should return a string
when the validation fails. They can be written purely as a function or together with a key
property in an object.
They can also alternatively return a Promise
when you have a rule that requires asynchronous code.
Typing:
type SimpleRule<T = any> = (value: T) => Promise<unknown> | unknown;
type KeyedRule<T = any> = { key: string; rule: SimpleRule<T> };
type Rule<T = any> = SimpleRule<T> | KeyedRule<T>;
Keyed rules that share the same key
will be executed together, this can be useful in a situation where rules are dependent on another. For example the Password
and Repeat Password
fields in a Login Form.
Rules will always be called with the latest modelValue
, to determine if a call should result in an error, it will check if the rule's return value is of type string
.
This allows you to write many rules in one line:
const required = value => !value && 'This field is required';
const min = value =>
value.length > 3 || 'This field has to be longer than 3 characters';
const max = value =>
value.length < 7 || 'This field is too long (maximum is 6 characters)';
Async rules allow you to perform network requests, for example checking if a username exists in the database:
const isNameTaken = name =>
new Promise(resolve => {
setTimeout(() => {
if (['foo', 'bar'].includes(name)) {
resolve();
} else {
resolve('This name is already taken');
}
}, 2000);
});
FAQs
Vue composition function for form validation
We found that vue3-form-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
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.