How to use the rulr.required 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.

github mariocoski / rest-api-node-typescript / src / presenter / express / auth / forgetPassword / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { isEmail } from '../../../../utils/validate';
import { maybe, required, restrictToSchema}from 'rulr';
import { ModelNotFoundError} from '../../../../utils/errors';
import {v4} from 'uuid';
import {OK} from 'http-status-codes';

const validateForgetPassword = maybe(
  restrictToSchema({
    email: required(isEmail)
  })
);

export default (config: Config) => {
  return catchErrors(config, async (req, res)=> {
   
    validateForgetPassword(req.body, ['user']);
   
    const {email} = req.body;
    const errorId = v4();
    
    try {
      await config.service.forgetPassword({email});
      const message = config.translator.passwordReminderSent(email);
      res.status(OK).json({message});
    }catch(err){
github mariocoski / rest-api-node-typescript / src / presenter / express / comments / createComment / 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_COMMENT, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, required, checkType, composeRules, restrictToSchema } from 'rulr';

const validateCreateComment = maybe(composeRules([
  restrictToSchema({
    user_id: required(checkType(Number)),
    post_id: required(checkType(Number)),
    body: required(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_COMMENT});
 
    validateCreateComment(req.body, ['comment']);
    
    const createdComment = await config.service.createComment(req.body);
github mariocoski / rest-api-node-typescript / src / presenter / express / posts / createPost / 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_POST, VARCHAR_FIELD_LENGTH, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, required, checkType, composeRules, restrictToSchema } from 'rulr';

const validateCreatePost = maybe(composeRules([
  restrictToSchema({
    user_id: required(checkType(String)),
    title: required(maxLength(VARCHAR_FIELD_LENGTH)),
    body: required(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_POST});
 
    validateCreatePost(req.body, ['post']);

    const createdPost = await config.service.createPost(req.body);

    res.status(CREATED).json(createdPost);
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});
   
    const message = config.translator.passwordChangedSuccessfully();
    
    res.status(OK).json({message});
github mariocoski / rest-api-node-typescript / src / presenter / express / auth / register / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import { minLength, isEmail, validateMatchingPasswords } from '../../../../utils/validate';
import { maybe, required, restrictToSchema, checkType, composeRules, first }from 'rulr';

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

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
    
    validateRegister(req.body, ['user']);
   
    const {email, password, bio, firstname, lastname} = req.body;
   
    const {user, token} = await config.service.register({
      email, password, bio, firstname, lastname
    });

    res.status(CREATED).json({user,token});
github mariocoski / rest-api-node-typescript / src / presenter / express / auth / register / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { CREATED } from 'http-status-codes';
import { minLength, isEmail, validateMatchingPasswords } from '../../../../utils/validate';
import { maybe, required, restrictToSchema, checkType, composeRules, first }from 'rulr';

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

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
    
    validateRegister(req.body, ['user']);
   
    const {email, password, bio, firstname, lastname} = req.body;
   
    const {user, token} = await config.service.register({
      email, password, bio, firstname, lastname
    });
github mariocoski / rest-api-node-typescript / src / presenter / express / comments / createComment / 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_COMMENT, TEXT_FIELD_LENGTH } from '../../../../utils/constants';
import { maxLength } from '../../../../utils/validate';
import { maybe, required, checkType, composeRules, restrictToSchema } from 'rulr';

const validateCreateComment = maybe(composeRules([
  restrictToSchema({
    user_id: required(checkType(Number)),
    post_id: required(checkType(Number)),
    body: required(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_COMMENT});
 
    validateCreateComment(req.body, ['comment']);
    
    const createdComment = await config.service.createComment(req.body);
    
    res.status(CREATED).json(createdComment);
  });
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);

    res.status(OK).json({success: true});
  });

rulr

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

MIT
Latest version published 21 days ago

Package Health Score

67 / 100
Full package analysis

Similar packages