hasShape

hasShape :: { k: Function } → a → Boolean

Takes an object of validators defining the possible shape of an object and returns a new function that reports whether the received objects matches that shape or not.

import hasShape form 'deep-waters/hasShape';
import isString form 'deep-waters/isString';
import isNumber form 'deep-waters/isNumber';

const isUser = hasShape({
    name: isString,
    age: isNumber,
}); 

isUser({ name: 'Antonio Rù', age: 33 }); // → true
isUser({ foo: 'bar' }); // → false
isUser({ name: 'Antonio Rù', age: 33, meta: { privacy: true } }); // → true

The created validator isUser only makes sure the received object has all the defined keys and each key matches the defined validation. It does not check on properties not defined by the shape, meaning it will return true even if the object has additional properties.

hasShape could be easily used to define nested objects:

import hasShape form 'deep-waters/hasShape';
import isString form 'deep-waters/isString';
import isNumber form 'deep-waters/isNumber';
import isBoolean from 'deep-waters/isBoolean';

const isUser = hasShape({
    name: isString,
    age: isNumber,
    meta: hasShape({
        privacy: isBoolean,
    })
}); 

Last updated