How to use the rulr.restrictToSchema 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});
github mariocoski / rest-api-node-typescript / src / presenter / express / auth / login / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { isEmail } from '../../../../utils/validate';
import { maybe, required, restrictToSchema, checkType } from 'rulr';
import { OK } from 'http-status-codes';

const validateLogin = maybe(
  restrictToSchema({
    email: required(isEmail),
    password: required(checkType(String))
  })
);

export default (config: Config) => {
  return catchErrors(config, async (req, res) => {
   
    validateLogin(req.body, ['user']);
   
    const {email, password} = req.body;
   
    const {user, token} = await config.service.login({
      email, password
    });
github mariocoski / rest-api-node-typescript / src / presenter / express / comments / getComments / 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_GET_COMMENTS } from '../../../../utils/constants';
import { maybe, optional,checkType, restrictToSchema }from 'rulr';
import { isValidSortObject } from '../../../../utils/validate';

const validateGetComments = maybe(
  restrictToSchema({
    limit: optional(checkType(String)),
    offset: optional(checkType(String)),
    sort: optional(isValidSortObject())
  }),
);

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

    hasPermission({ permissions, permissionName: CAN_GET_COMMENTS});

    validateGetComments(req.query,['comments']);

    const {limit, offset, sort} = req.query;
github mariocoski / rest-api-node-typescript / src / presenter / express / roles / getRoles / 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_GET_ROLES } from '../../../../utils/constants';
import { maybe, optional, checkType, restrictToSchema } from 'rulr';
import { isValidSortObject } from '../../../../utils/validate';

const validateGetRoles = maybe(
  restrictToSchema({
    limit: optional(checkType(String)),
    offset: optional(checkType(String)),
    sort: optional(isValidSortObject())
  }),
);

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

    validateGetRoles(req.query,['roles']);

    const {limit, offset, sort} = req.query;
github mariocoski / rest-api-node-typescript / src / presenter / express / users / getUsers / 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_GET_USERS } from '../../../../utils/constants';
import { maybe, optional, checkType, restrictToSchema } from 'rulr';
import { isValidSortObject } from '../../../../utils/validate';

const validateGetUsers = maybe(
  restrictToSchema({
    limit: optional(checkType(String)),
    offset: optional(checkType(String)),
    sort: optional(isValidSortObject())
  }),
);

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

    validateGetUsers(req.query,['users']);

    const {limit, offset, sort} = req.query;
github mariocoski / rest-api-node-typescript / src / presenter / express / permissions / getPermissions / 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_GET_PERMISSIONS } from '../../../../utils/constants';
import { maybe, optional, checkType, restrictToSchema} from 'rulr';
import { isValidSortObject } from '../../../../utils/validate';

const validateGetPermissions = maybe(
  restrictToSchema({
    limit: optional(checkType(String)),
    offset: optional(checkType(String)),
    sort: optional(isValidSortObject())
  }),
);

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

    validateGetPermissions(req.query,['roles']);

    const {limit, offset, sort} = req.query;
github mariocoski / rest-api-node-typescript / src / presenter / express / posts / getPosts / index.ts View on Github external
import Config from '../../Config';
import catchErrors from '../../utils/catchErrors';
import { Request, Response } from 'express';
import { OK } from 'http-status-codes';
import getAuthUserAndPermissions from '../../../../utils/jwt/getAuthUserAndPermissions';
import hasPermission from '../../../../utils/jwt/hasPermission';
import { CAN_GET_POSTS } from '../../../../utils/constants';
import { maybe, optional, checkType, restrictToSchema }from 'rulr';
import { isValidSortObject } from '../../../../utils/validate';

const validateGetPosts = maybe(
  restrictToSchema({
    limit: optional(checkType(String)),
    offset: optional(checkType(String)),
    sort: optional(isValidSortObject())
  }),
);

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

    validateGetPosts(req.query,['posts']);

    const {limit, offset, sort} = req.query;

rulr

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

MIT
Latest version published 20 days ago

Package Health Score

70 / 100
Full package analysis

Similar packages