How to use the rulr.composeRules function in rulr

To help you get started, we’ve selected a few rulr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mariocoski / rest-api-node-typescript / src / presenter / express / users / revokeUserRole / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_REVOKE_ROLE, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maybe, required, checkType, composeRules, restrictToSchema } from 'rulr';

const validateRevokeRole = maybe(composeRules([
  restrictToSchema({
    user_id: required(checkType(String)),
    role_id: required(checkType(String))
  })
]));

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
  
    const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
    
    hasPermission({permissions, permissionName: CAN_REVOKE_ROLE});

    validateRevokeRole(req.params, ['user']);

    await config.service.revokeUserRole(req.params);
github mariocoski / rest-api-node-typescript / src / presenter / express / permissions / createPermission / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_CREATE_PERMISSION, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';

import { maybe, required, optional, checkType,composeRules, first, restrictToSchema } from 'rulr';
import * as R from 'ramda';

const validateCreatePermission = maybe(composeRules([
  restrictToSchema({
    name: required(maxLength(VARCHAR_FIELD_LENGTH)),
    label: optional(maxLength(VARCHAR_FIELD_LENGTH)),
    description: optional(maxLength(TEXT_FIELD_LENGTH)),
  })
]));

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
  
    const { permissions } = await getAuthUserAndPermissions({req, service: config.service});

    hasPermission({permissions, permissionName: CAN_CREATE_PERMISSION});
 
    validateCreatePermission(req.body, ['Permission']);
github mariocoski / rest-api-node-typescript / src / presenter / express / permissions / updatePermission / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes'; 
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_UPDATE_PERMISSION,VARCHAR_FIELD_LENGTH,TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, optional,composeRules, restrictToSchema } from 'rulr';

const validateUpdatePermission = maybe(composeRules([
  restrictToSchema({
    name: optional(maxLength(VARCHAR_FIELD_LENGTH)),
    label: optional(maxLength(VARCHAR_FIELD_LENGTH)),
    description: optional(maxLength(TEXT_FIELD_LENGTH)),
  })
]));

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
  
    const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
    
    hasPermission({permissions, permissionName: CAN_UPDATE_PERMISSION});
 
    validateUpdatePermission(req.body, ['permission']);
github mariocoski / rest-api-node-typescript / src / presenter / express / roles / updateRole / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes'; 
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_UPDATE_ROLE, VARCHAR_FIELD_LENGTH,TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, optional, composeRules, restrictToSchema } from 'rulr';

const validateUpdatePost = maybe(composeRules([
  restrictToSchema({
    name: optional(maxLength(VARCHAR_FIELD_LENGTH)),
    description: optional(maxLength(TEXT_FIELD_LENGTH)),
  })
]));

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
  
    const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
    
    hasPermission({permissions, permissionName: CAN_UPDATE_ROLE});
 
    validateUpdatePost(req.body, ['role']);
    
    const {role_id} = req.params;
github mariocoski / rest-api-node-typescript / src / presenter / express / users / assignUserRole / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_ASSIGN_ROLE } from '../../../../utils/constants';
import { maybe, required, checkType, composeRules, restrictToSchema } from 'rulr';

const validateAssignUserRole = maybe(composeRules([
  restrictToSchema({
    role_id: required(checkType(String))
  })
]));

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
  
    const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
    
    hasPermission({permissions, permissionName: CAN_ASSIGN_ROLE});
 
    validateAssignUserRole(req.body, ['user']);

    const {user_id} = req.params;
    const {role_id} = req.body;
github mariocoski / rest-api-node-typescript / src / presenter / express / auth / resetPassword / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import { minLength, validateMatchingPasswords } from '../../../../utils/validate';
import { maybe, required, checkType, composeRules, first, restrictToSchema } from 'rulr';

const validateResetPassword = maybe(composeRules([
  restrictToSchema({
    token: required(checkType(String)),
    password: required(minLength(6)),
    password_confirmation: required(checkType(String)),
  }),
  first(checkType(Object), validateMatchingPasswords)
]));

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
   
    validateResetPassword(req.body, ['user']);

    const {token, password } = req.body;

    await config.service.resetPassword({token, password});
github mariocoski / rest-api-node-typescript / src / presenter / express / posts / updatePost / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_UPDATE_POST, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, optional, checkType,composeRules, restrictToSchema }from 'rulr';

const validateUpdatePost = maybe(composeRules([
  restrictToSchema({
    user_id: optional(checkType(Number)),
    title: optional(maxLength(VARCHAR_FIELD_LENGTH)),
    body: optional(maxLength(TEXT_FIELD_LENGTH)),
  })
]));

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
  
    const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
    
    hasPermission({permissions, permissionName: CAN_UPDATE_POST});
 
    validateUpdatePost(req.body, ['post']);
github mariocoski / rest-api-node-typescript / src / presenter / express / roles / createRole / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_CREATE_ROLE, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, required, optional, composeRules, restrictToSchema } from 'rulr';

const validateCreateRole = maybe(composeRules([
  restrictToSchema({
    name: required(maxLength(VARCHAR_FIELD_LENGTH)),
    description: optional(maxLength(TEXT_FIELD_LENGTH)),
  })
]));

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
  
    const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
    
    hasPermission({permissions, permissionName: CAN_CREATE_ROLE});
 
    validateCreateRole(req.body, ['role']);
    
    const createRole = await config.service.createRole(req.body);
github mariocoski / rest-api-node-typescript / src / presenter / express / roles / assignRolePermission / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_ASSIGN_PERMISSION } from '../../../../utils/constants';
import { maybe, required, checkType, composeRules, restrictToSchema } from 'rulr';

const validateAssignRolePermission = maybe( composeRules([
  restrictToSchema({
    permission_id: required(checkType(String))
  })
]));

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
  
    const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
    
    hasPermission({permissions, permissionName: CAN_ASSIGN_PERMISSION});
 
    validateAssignRolePermission(req.body, ['role']);

    const {role_id} = req.params;
    const {permission_id} = req.body;
github mariocoski / rest-api-node-typescript / src / presenter / express / roles / revokeRolePermission / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_REVOKE_PERMISSION, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maybe, required, checkType,composeRules, restrictToSchema } from 'rulr';

const validateRevokeRolePermission = maybe(composeRules([
  restrictToSchema({
    permission_id: required(checkType(String)),
    role_id: required(checkType(String))
  })
]));

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
  
    const { permissions } = await getAuthUserAndPermissions({req, service: config.service});
    
    hasPermission({permissions, permissionName: CAN_REVOKE_PERMISSION});

    validateRevokeRolePermission(req.params, ['role']);

    await config.service.revokeRolePermission(req.params);

rulr

Validation and unit conversion errors in TypeScript at compile-time. Started in 2016.

MIT
Latest version published 4 days ago

Package Health Score

72 / 100
Full package analysis