Structural-matching for JavaScript.
Main uses cases
- validating user input / config files
- validating inbound HTTP request payloads
- writing expressive unit tests
Table of contents
- Getting started
- Syntactic sugar
- A more complex example
- Optional values
- Defining custom matchers
- Asserting on matchers
- Custom constraints
- A note on performance
Getting started
Syntactic sugar
The example above is actually syntactic sugar for:
This means all matchers are actually instances of s.Matcher, and can potentially take extra parameters.
Built-in matchers include(all classes)
- s.array({min, max, of, description})
- s.boolean({parse, description})
- s.duration({min, max, description})
- s.enum({name, values, verbose, description})
- s.func({arity})
- s.hashmap({keys, values})
- s.integer ({min, max, description})
- s.ip({version: 4, description})
- s.isoDate({time, description})
- s.number({min, max, parse, description})
- s.object(fields, {description})
- s.objectWithOnly(fields, {description})
- s.regex(reg, {description})
- s.string ({min, max, description})
- s.url({description})
- s.uuid({version, description})
- s.value(primitive, {description})
- s.email({domain, description})
- s.
hentai en castellano videos caseros españa
sexogratis incesto retro
fiestas xxx falsos casting porno
porno amas de casa xxx prostitutas
mujeres follando gratis cine para adultos gratis
concursos nudistas porno peludas españolas
porno loco analxxx
pajas de madres asiatica forzada
videos pornos muy guarros tata_latina
porno india cerdas com gratis
maduras tocandose me follo a
tuporn laura marano nude
torbe videos completos peliculas x en castellano
maduras sex pillada follando
joven folla por dinero se folla a su cuñada
mi madre me folla chupame las tetas
porno friki gay se la chupa a hetero
follando con abuelas fontanero cachondo
pillada follando porno italiano incesto
incestos lesbicos trios xoneOf([matcher], {description})
They all come with several usage examples. Matchers usually support both simple / complex usages, with nice syntactic sugar.
A more complex example
Here’s an example that mixes nested objects, arrays, and matches on different types with extra options.
You can of course extract matchers to reuse them, or to make the hierarchy more legible.
Optional values
By default, all matchers expect the value to exist. In other words every field is required in your schema definition.
You can make a field optional by using the special {optional: true} argument.,
Defining custom matchers
To define a customer matcher, simply inherit the s.Matcher prototype and implement the _match function.
Or you can use the helper function to create it:
You can use these matchers like any of the built-in ones.
Asserting on matchers
Matchers always return the following structure:
In some cases, you might just want to throw an error – for example in the context of a unit test. Strummer provides the s.assert function for that purpose:
Custom constraints
Custom constraints can be applied by passing a function as the second argument when creating the schema. This function will be run on match and you are able to return an array of errors.
Currently only objectWithOnly is supported.
An example use case is related optional fields
which will shows log like this:
When you trying to create your own matcher which supports jsonSchema, then you needs to impement the toJSONSchema option in the createMatcher, if toJSONSchema is not defined, when you call matcher.toJSONSchema() it will return nothing.
A note on performance
The 2 main rules for performance are:
-
If you need to validate many objects of the same kind, you should declare matchers upfront and reuse them.
-
All syntactic sugar is processed at creation time. This means shorthand notations don’t cause any performance overhead compared to their canonical equivalents.
Of course, actual performance depends on the complexity of your matchers / objects. If you’re interested in figures, some stats are printed as part of the unit test suite:








