Last active
September 26, 2017 16:10
-
-
Save TGOlson/6d7a1baa768b6e9a394b to your computer and use it in GitHub Desktop.
Functional validations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Validator = require('simple-validate'); | |
| var validateTenantArgs = Validator.validateOrThrow({ | |
| title: 'somePred', | |
| etc: '...' | |
| }); | |
| // current style | |
| function postTenantToSystemTenant(systemTenant, tenantArgs) { | |
| validateTenantArgs(tenantArgs); | |
| return systemTenant.post(tenantArgs); | |
| } | |
| // proposed | |
| // MjEntity -> Object -> MjEntity | |
| // validateOrThrow throws an error if an argument is invalid | |
| // otherwise it passes the arguments along | |
| var postTenantToSystemTenant = compose(createTenant, validateTenantArgs); | |
| function createTenant(systemTenant, tenantArgs) { | |
| return systemTenant.post(tenantArgs); | |
| } | |
| // really style needs to be: | |
| // so system tenant gets passed along to create tenant | |
| // and validate tenant args passes the validated arguments along | |
| var postTenantToSystemTenant = useWith(createTenant, R.I, validateTenantArgs); | |
| // or create a utility to handle this | |
| var postTenantToSystemTenant = Utils.makePostHandler( | |
| validateTenantArgs, | |
| // conversion function? | |
| createTenant | |
| ); | |
| // Fn -> Fn -> (MjEntity -> Object -> MjEntity) | |
| Utils.makePostHandler = function(validationFn, creationFn) { | |
| return useWith(creationFn, R.identity, validationFn); | |
| }; | |
| // Fn -> Fn -> (MjEntity -> Object -> MjEntity) | |
| Utils.makePostHandler = function(validationFn, creationFn) { | |
| // MjEntity -> Object -> MjEntity | |
| return function(targetEntity, entityArgs) { | |
| validationFn(entityArgs); | |
| return creationFn(targetEntity, entityArgs); | |
| }; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Utils.makePostHandler = function(config) { | |
| var creationFn = config.creation, | |
| validateTarget = config.validateTarget || R.I, | |
| validateArgs = config.validateArgs || R.I, | |
| conversionFn = config.conversion || R.I, | |
| validateAndConvertArgs = R.compose(conversionFn, validateArgs); | |
| if(!creationFn) { | |
| throw new Error('Cannot make post handler without a creation function'); | |
| } | |
| return R.useWith(creationFn, validateTarget, validateAndConvertArgs); | |
| }; | |
| var validateTarget = Validator.validateOrThrow({ | |
| dsKey: R.eq('system') | |
| }); | |
| var validateTenantArgs = Validator.validateOrThrow({ | |
| title: Validator.required(R.is(String)), | |
| description: R.is(String) | |
| }); | |
| // var convertArgs = function(args) { | |
| // if(args.img.data) { | |
| // return convertImgData(args); | |
| // } | |
| // }; | |
| function createTenant(systemTenant, tenantArgs) { | |
| return systemTenant.post(tenantArgs); | |
| } | |
| module.exports = Utils.makePostHandler({ | |
| validateTarget: validateTarget, | |
| validateArgs: validateTenantArgs, | |
| creationFn: createTenant | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment