0.1.0 • Published 4 years ago

prop-types-prod v0.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

prop-types-prod

✅ Runtime type checking for production

Override "prop-types"'s natural behaviour to be silent in prod, instead, it'll throw an error.

Example use of schema validation

const {
	checkPropTypes,
	number,
	string
} = require('prop-types-prod');

const schema = {
	name: string.isRequired,
	age: number
};

const info = {
	name: 'Aaron',
	age: 'Six'
};

try {
	checkPropTypes(schema, info, 'info', 'my-api-endpoint');
} catch (error) {
	error.message
	// Throws 'Failed property type: Invalid property `age` of type `string` supplied to `my-api-endpoint`, expected `number`.'
}

Use as a request handler

module.exports = (request, response) => {
	try {
		checkPropTypes(schema, request.body, 'info', 'my-api-endpoint');
		// Do more things
		response.status(200).type('txt').send('Yeah, good');
	} catch (error) {
		response.status(400).type('txt').send(error.message);
	}
}

Use of prop-types is not overridden. It will throw errors instead of logging to console

require('prop-types-prod');
const PropTypes = require('prop-types')