![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Composable message-based validation
$ npm install not-valid --save
import { validate } from "not-valid";
// Use createValidator to specify a rule and the message given for breaking that rule
const mustContainA = createValidator<string>(v => v.indexOf("A") !== -1, "Value must contain the letter 'a'");
// pass in array of validation functions and a value to validate
validate([ mustContainA ], "cheese"); // [ "Value must contain the letter 'a'" ] - returns error messages
// can use a factory pattern for your validation methods to make things nice
const mustContain = (requirement: any) => {
return createValidator<string | Array<any>>(v => v.indexOf(requirement) !== -1, `Value must contain '${requirement}'`);
};
const lengthWithinBounds = (min: number, max: number) => {
return createValidator<string>(v => v.length < min || v.length > max, `Value must have length between ${min} and ${max}`);
};
// you can pass in multiple validators
validate([
mustContain("Z"),
lengthWithinBounds(2, 6)
], "Too long a string, they say!"); // [ "Value must contain 'Z'", "Value must have length between 2 and 6" ]
A number of validation functions come bundled with this package. You can use them like so:
import { validators } from "not-valid";
validate([
validators.validLength({ min: 6, max: 12 })
], "Good value");
The validators included are as follows:
requiredString
requiredNumber
validLength
validEmail
validAlphaNumeric
validOption
validPhoneNumber
validNINumber
validUKDrivingLicence
validSortCode
validBankAccountNumber
validVATNumber
A validation function must take in a value value
, and return Result.Pass
if value
is valid, or Result.Fail(message)
is value
is invalid. They can also return Result.Stop
, which will silently stop the validation cycle (no more errors).
This can be done with the helper method createValidator
in not-valid
:
import { createValidator } from "not-valid";
const mustContainA = createValidator<string>(v => v.indexOf("A") !== -1, "Value must contain the letter 'a'");
You can use factory patterns around this to make it nicer:
const mustContain = (requirement: any) => {
return createValidator<string | Array<any>>(v => v.indexOf(requirement) !== -1, `Value must contain '${requirement}'`);
};
All validators (except for validators that explicitly check for "required") should treat empty string, null and undefined as valid. This is because we can combine validators with "required" validators in order to enforce something being valid and not empty, but also allows us to accept nothing being entered if desired.
The third parameter of validate
is an object containing options.
interface ValidationOptions {
sequential?: boolean;
}
sequential
Default: true
The validation will break on the first error, therefore only returning a single validation error.
validate([ something, another ], 5, { sequential: false });
If something
fails validation, another
will not be called.
Made with :sparkling_heart: by NewOrbit in Oxfordshire, and licensed under the MIT Licence
FAQs
Composable message-based validation
The npm package not-valid receives a total of 225 weekly downloads. As such, not-valid popularity was classified as not popular.
We found that not-valid demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers 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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.