Format
This section describes the format (the sanitation schema) we give to Sandhands to define how it should sanitize the input.
Primitives Basics
Null and undefined can be passed in as is, and it will expect strict equality.
const {valid} = require('sandhands')
valid('', null) // false
valid(undefined, null) // false
valid(null, null) // true
valid('', undefined) // false
valid(null, undefined) // false
valid(undefined, undefined) // true
Numbers, Strings, and Booleans can be passed in by using their class names.
const {valid} = require('sandhands')
valid(12, String) // false
valid(String, String) // false
valid('hello world', String) // true
valid('ganondorf', Number) // false
valid(Number, Number) // false
valid(42, Number) // true
Strings
The following options are available for strings:
Numbers
Objects
We can sanitize objects by creating our own object representing how we expect the input to be formatted.
Here's an example of the sanitation behavior using these options
const {valid} = require('sandhands')
console.log(valid({}, {})) // true
console.log(valid({}, {a: String})) // false
console.log(valid({a: 'hello world'}, {a: String})) // true
console.log(valid({}, {_: {a: String}, optionalProps: ["a"]})) // true
console.log(valid({b: 12}, {})) // false
console.log(valid({b: 12}, {}, {strict: false})) // true
Arrays
We can sanitize arrays by creating our own array representing how we expect the input to be formatted. The default array behavior might seem confusing at first, however once you understand it in practice it is simple and expressive.
Here's an example of the sanitation behavior using these options
const {valid} = require('sandhands')
console.log(valid([], [])) // true
console.log(valid([], [Number])) // true - Because the format array is only one element long the first value is set as the firstAsStandard format, and strict is not assumed to be true.
console.log(valid([12], [Number])) // true
console.log(valid([54, 25], [Number])) // true - As you can see
console.log(valid([12, 12], [Number, Number])) // true - Because the format array is longer than one element firstAsStandard is not assumed and strict is set to true.
console.log(valid([52, 63, 14], [Number, Number])) // false - Because strict is assumed extra array elements will cause validation to fail.
console.log(valid(['a', 15, 25], {_:[Number, String], firstAsStandard: true})) // true - We can also use firstAsStandard alongside with specific formats for certain array indexes by setting it explicitly in the inline options. The first index (Number) will be the standard format for all indexes, and the second index will now become the beginning of the array of custom formats for specific indexes, meaning the first array index will be expected to be a string, and all following indexes will be expected to be numbers.
Functions
Functions automatically support all of the object options because functions are effectively also objects.
Universal Options
All
The All format is a special class we can import that acts as a wildcard. Please be careful with this as any kind of dangerous input could be passed. Please only use this if you know what you are doing.
Here's an example of the sanitation behavior using these options
const {valid, All} = require('sandhands')
console.log(valid("Hello :)", All)) // true
console.log(valid(Infinity, All)) // true
console.log(valid({a: 'hello world', b: 12}, {a: String, b: All})) // true