Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
match-json
Advanced tools
match-json
is a light assertion library built with JSON APIs in mind.
JSON API's can only carry JSON types: strings, numbers, booleans, arrays and objects. This library uses that in favor to use functions and regexp to assert JSON API's. You should bring your favorite test library.
npm install match-json
Match JSON Primitives.
// Numbers
match(3.1415, 3.1415); // => true
//Strings
match("Uno Dos Tres", "Uno Dos Tres"); // => true
// Booleans
match(false, false); // => true
// And with undefined and null values
match(undefined, undefined); // => true
match(null, null); // => true
Match Structures (objects and arrays).
match({ name: "Link", color: "green" }, { name: "Link", color: "green" }); // => true
match(["deku", "goron", "zora"], ["deku", "goron", "zora"]); // => true
Matching using Functions
match({ name: "Samus" }, (hero) => hero.name.length >= 5); // => true
Matching using regular expressions
match(
"fmcloud@nintendo.jp",
/[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}/
); // => true
Also, match-json
can check JSON types using constructor functions
match(5, Number); // => true
match("Hola, mundo", String); // => true
match(false, Boolean); // => true
And everything together!
match(
{
name: { first: "Walter", last: "White" },
age: 51,
breakingBad: true,
},
{
name: { first: /[\w]*/, last: "White" },
age: (age) => age > 18,
breakingBad: Boolean,
}
); // => true
In an object, the default behavior of partial interpret as an error any extra field received. Partial mode ignores potential extra fields received.
Partial mode is enabled by using the partial
function exposed from match-json
instead of match-json
itself. The rest of functionality is not changed.
NOTE: Only objects (and not arrays) are affected by partial mode.
import match, { partial } from 'match-json';
// No partial mode
match({ id : 5, name: 'john' }, { id: Number }) // => false
// Partial mode
partial({ id : 5, name: 'john' }, { id: Number }) // => true
match-json
also provides a bake
function that can be used to predefine an expected pattern.
const nameIsLarge = match.bake({ name: (name) => name.length > 10 });
nameIsLarge("Tom"); // => false
nameIsLarge("Tooooooooom"); // => true
match( a : T, b : T, partialMode : boolean? ) : boolean
match( a : T, test : RegExp, partialMode : boolean? ) : boolean
match( a : T, test : PredicateFunction, partialMode : boolean? ) : boolean
match( a : T, test : JSONTypeConstructorFunction, partialMode : boolean? ) : boolean
bake( a: T, partialMode : boolean? ) : PredicateFunction
( where PredicateFunction = ( w : T ) : boolean )
( where JSONTypeConstructorFunction = Number, String OR Boolean )
Feel free to open an issue and/or make a PR if you found a bug or think in a way this lib or even the README can be improved.
MIT
FAQs
A light assertion library built with JSON APIs in mind.
We found that match-json 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.