How to use the rulr.optional 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 / 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;

    const posts = await config.service.getPosts({limit, offset, order: sort});
github mariocoski / rest-api-node-typescript / src / presenter / express / users / updateUser / 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_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;
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;
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']);
    
    const {post_id} = req.params;
    
    const updatePost = await config.service.updatePost({id: post_id, data: req.body});
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']);
    
    const createdPermission = await config.service.createPermission(req.body);

    res.status(CREATED).json(createdPermission);
  });
github mariocoski / rest-api-node-typescript / src / presenter / express / users / updateUser / 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_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']);
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;

    const posts = await config.service.getPosts({limit, offset, order: sort});
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);

    res.status(CREATED).json(createRole);
  });

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