Installations
npm install schema-utils
Score
95.1
Supply Chain
100
Quality
78.2
Maintenance
100
Vulnerability
100
License
Developer
webpack
Developer Guide
Module System
CommonJS
Min. Node Version
>= 12.13.0
Typescript Support
Yes
Node Version
18.15.0
NPM Version
9.5.0
Statistics
247 Stars
190 Commits
39 Forks
10 Watching
4 Branches
86 Contributors
Updated on 26 Nov 2024
Bundle Size
156.29 kB
Minified
42.87 kB
Minified + Gzipped
Languages
JavaScript (99.89%)
Shell (0.11%)
Total Downloads
Cumulative downloads
Total Downloads
16,406,817,095
Last day
-6.1%
16,504,700
Compared to previous day
Last week
1.5%
87,145,171
Compared to previous week
Last month
12.2%
362,985,338
Compared to previous month
Last year
-7.5%
4,114,145,237
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
4
schema-utils
Package for validate options in loaders and plugins.
Getting Started
To begin, you'll need to install schema-utils
:
1npm install schema-utils
API
schema.json
1{ 2 "type": "object", 3 "properties": { 4 "option": { 5 "type": "boolean" 6 } 7 }, 8 "additionalProperties": false 9}
1import schema from "./path/to/schema.json"; 2import { validate } from "schema-utils"; 3 4const options = { option: true }; 5const configuration = { name: "Loader Name/Plugin Name/Name" }; 6 7validate(schema, options, configuration);
schema
Type: String
JSON schema.
Simple example of schema:
1{ 2 "type": "object", 3 "properties": { 4 "name": { 5 "description": "This is description of option.", 6 "type": "string" 7 } 8 }, 9 "additionalProperties": false 10}
options
Type: Object
Object with options.
1import schema from "./path/to/schema.json"; 2import { validate } from "schema-utils"; 3 4const options = { foo: "bar" }; 5 6validate(schema, { name: 123 }, { name: "MyPlugin" });
configuration
Allow to configure validator.
There is an alternative method to configure the name
andbaseDataPath
options via the title
property in the schema.
For example:
1{ 2 "title": "My Loader options", 3 "type": "object", 4 "properties": { 5 "name": { 6 "description": "This is description of option.", 7 "type": "string" 8 } 9 }, 10 "additionalProperties": false 11}
The last word used for the baseDataPath
option, other words used for the name
option.
Based on the example above the name
option equals My Loader
, the baseDataPath
option equals options
.
name
Type: Object
Default: "Object"
Allow to setup name in validation errors.
1import schema from "./path/to/schema.json"; 2import { validate } from "schema-utils"; 3 4const options = { foo: "bar" }; 5 6validate(schema, options, { name: "MyPlugin" });
1Invalid configuration object. MyPlugin has been initialised using a configuration object that does not match the API schema. 2 - configuration.optionName should be a integer.
baseDataPath
Type: String
Default: "configuration"
Allow to setup base data path in validation errors.
1import schema from "./path/to/schema.json"; 2import { validate } from "schema-utils"; 3 4const options = { foo: "bar" }; 5 6validate(schema, options, { name: "MyPlugin", baseDataPath: "options" });
1Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema. 2 - options.optionName should be a integer.
postFormatter
Type: Function
Default: undefined
Allow to reformat errors.
1import schema from "./path/to/schema.json"; 2import { validate } from "schema-utils"; 3 4const options = { foo: "bar" }; 5 6validate(schema, options, { 7 name: "MyPlugin", 8 postFormatter: (formattedError, error) => { 9 if (error.keyword === "type") { 10 return `${formattedError}\nAdditional Information.`; 11 } 12 13 return formattedError; 14 }, 15});
1Invalid options object. MyPlugin has been initialized using an options object that does not match the API schema. 2 - options.optionName should be a integer. 3 Additional Information.
Examples
schema.json
1{ 2 "type": "object", 3 "properties": { 4 "name": { 5 "type": "string" 6 }, 7 "test": { 8 "anyOf": [ 9 { "type": "array" }, 10 { "type": "string" }, 11 { "instanceof": "RegExp" } 12 ] 13 }, 14 "transform": { 15 "instanceof": "Function" 16 }, 17 "sourceMap": { 18 "type": "boolean" 19 } 20 }, 21 "additionalProperties": false 22}
Loader
1import { getOptions } from "loader-utils"; 2import { validate } from "schema-utils"; 3 4import schema from "path/to/schema.json"; 5 6function loader(src, map) { 7 const options = getOptions(this); 8 9 validate(schema, options, { 10 name: "Loader Name", 11 baseDataPath: "options", 12 }); 13 14 // Code... 15} 16 17export default loader;
Plugin
1import { validate } from "schema-utils"; 2 3import schema from "path/to/schema.json"; 4 5class Plugin { 6 constructor(options) { 7 validate(schema, options, { 8 name: "Plugin Name", 9 baseDataPath: "options", 10 }); 11 12 this.options = options; 13 } 14 15 apply(compiler) { 16 // Code... 17 } 18} 19 20export default Plugin;
Allow to disable and enable validation (the validate
function do nothing)
This can be useful when you don't want to do validation for production
builds.
1import { disableValidation, enableValidation, validate } from "schema-utils"; 2 3// Disable validation 4disableValidation(); 5// Do nothing 6validate(schema, options); 7 8// Enable validation 9enableValidation(); 10// Will throw an error if schema is not valid 11validate(schema, options); 12 13// Allow to undestand do you need validation or not 14const need = needValidate(); 15 16console.log(need);
Also you can enable/disable validation using the process.env.SKIP_VALIDATION
env variable.
Supported values (case insensitive):
yes
/y
/true
/1
/on
no
/n
/false
/0
/off
Contributing
Please take a moment to read our contributing guidelines if you haven't yet done so.
License
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
3 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
Reason
Found 8/20 approved changesets -- score normalized to 4
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack/schema-utils/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:37: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack/schema-utils/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:58: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack/schema-utils/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:79: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack/schema-utils/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:81: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack/schema-utils/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:93: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack/schema-utils/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:106: update your workflow using https://app.stepsecurity.io/secureworkflow/webpack/schema-utils/nodejs.yml/master?enable=pin
- Info: 0 out of 5 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 2 out of 2 npmCommand dependencies pinned
Reason
3 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/nodejs.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 26 are checked with a SAST tool
Score
4.5
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to schema-utils
@open-rpc/schema-utils-js
<center> <span> <img alt="CircleCI branch" src="https://img.shields.io/circleci/project/github/open-rpc/schema-utils-js/master.svg"> <img src="https://codecov.io/gh/open-rpc/schema-utils-js/branch/master/graph/badge.svg" /> <img alt="npm" sr
@fibery/schema
Fibery schema utils
@acot/schema-validator
A simple wrapper module for schema-utils.
@develar/schema-utils
webpack Validation Utils