How to use the rulr.first 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 / users / updateUser / index.ts View on Github external
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_UPDATE_USER } from '../../../../utils/constants';
import { minLength, isEmail, validateMatchingPasswords } from '../../../../utils/validate';
import { maybe, optional, checkType, composeRules, first, restrictToSchema }from 'rulr';
import * as R from 'ramda';

const validateUpdateUser = maybe(composeRules([
  restrictToSchema({
    firstname: optional(checkType(String)),
    lastname: optional(checkType(String)),
    bio: optional(checkType(String)),
    email: optional(isEmail),
    password: optional(minLength(6)),
    password_confirmation: optional(checkType(String)),
  }),
  first(checkType(Object), optional(validateMatchingPasswords))
]));

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

    const fillable = [
      'firstname', 'lastname', 'bio', 'email', 'password'
    ];
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 / users / createUser / index.ts View on Github external
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_CREATE_USER } from '../../../../utils/constants';
import { minLength, isEmail, validateMatchingPasswords } from '../../../../utils/validate';
import { maybe, required, optional, checkType, composeRules, first, restrictToSchema } from 'rulr';
import * as R from 'ramda';

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

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

    const fillable = [
      'firstname', 'lastname', 'bio', 'email', 'password'
    ];
    
    const data: any = R.pickBy((val:any, key:any)=>{
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});
  });
}

rulr

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

MIT
Latest version published 29 days ago

Package Health Score

67 / 100
Full package analysis