Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
validate-value
Advanced tools
validate-value validates values against JSON schemas.
Category | Status |
---|---|
Version | |
Dependencies | |
Dev dependencies | |
Build | |
License |
$ npm install validate-value
First you need to integrate validate-value into your application:
const { Value, isOfType } = require('validate-value');
If you use TypeScript, use the following code instead:
import { Value, isOfType } from 'validate-value';
Then, create a new instance and provide a JSON schema that you would like to use for validation:
const value = new Value({
type: 'object',
properties: {
username: { type: 'string' },
password: { type: 'string' }
},
additionalProperties: false,
required: [ 'username', 'password' ]
});
Afterwards, you may use the validate
function to validate a value:
const user = {
username: 'Jane Doe',
password: 'secret'
};
try {
value.validate(user);
} catch (ex) {
// ...
}
By default, the error message uses value
as identifier and .
as the separator for the object that is validated, but sometimes you may want to change this. Therefor, provide the desired identifier and separator as second parameter to the validate
function:
const user = {
username: 'Jane Doe',
password: 'secret'
};
try {
value.validate(user, { valueName: 'person', separator: '/' });
} catch (ex) {
// ...
}
From time to time, you may not be interested in the actual error, but only in the fact whether the given object is valid or not. For these cases, use the isValid
function:
const user = {
username: 'Jane Doe',
password: 'secret'
};
console.log(value.isValid(user));
// => true
To verify that a variable is of a specific type, use the isOfType
function. Hand over a value you would like to verify, and a JSON schema describing that type. The function returns true
if the given variable matches the schema, and false
if it doesn't:
const { isTypeOf } = require('validate-value');
const user = {
username: 'Jane Doe',
password: 'secret'
};
const schema = {
type: 'object',
properties: {
username: { type: 'string' },
password: { type: 'string' }
},
additionalProperties: false,
required: [ 'username', 'password' ]
};
if (isOfType(user, schema)) {
// ...
}
When using TypeScript, you may even specify a generic type parameter, and use the function as a type guard. Also it is recommended to use the constant values exported by validate-value instead of the string literals as above, as they are the type-safe versions of these literals:
import { isTypeOf, value as v }
interface User {
username: string;
password: string;
}
const user = {
username: 'Jane Doe',
password: 'secret'
};
const schema = {
type: v.object,
properties: {
username: { type: v.string },
password: { type: v.string }
},
additionalProperties: false,
required: [ 'username', 'password' ]
};
if (isOfType<User>(user, schema)) {
// ...
}
To run quality assurance for this module use roboter:
$ npx roboter
FAQs
validate-value validates values against JSON schemas.
The npm package validate-value receives a total of 59 weekly downloads. As such, validate-value popularity was classified as not popular.
We found that validate-value demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.