coderofsalvation/typeshave 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

coderofsalvation/typeshave

Composer 安装命令:

composer require coderofsalvation/typeshave

包简介

Lightweight typesafe function wrapper for PHP/JS. Validate nested data for forms,api's and more with jsonschema (http://coderofsalvation.github.io/typeshave)

README 文档

README

Prevent functions from exploding with garbage-in garbage-out.

Guard your function's incoming data using typeshave wrappers in JS & PHP (typeshave website)

Usage

$ echo '{ "minimum-stability": "dev" }' > composer.json
$ composer require coderofsalvation/typeshave

and then

use coderofsalvation\TypeShave;

function foo2($foo){
  TypeShave::check( $foo, (object)array(
    "foo" => (object)array( "type" => "string", "minLength" => 1, "regex" => "/foo/" )
  ));
}

foo2( 123 );       // will throw exception 

or heck, we can even write jsonschema inline:

function foo2($foo){
  TypeShave::check( $foo, '{ "foo": { "type": "string", "minLength":1, "regex": "/foo/"}  }' );
}

typeshave uses the established jsonschema validation-format, which even supports validating nested datastructures. Re-usable in many other areas (database-, restpayload-, form-validation and so on)

or

// multiple arguments at once 

function foo($foo, $bar){
  TypeShave::check( func_get_args(), '{
    "foo": { "type": "string"  },
    "bar": { "type": "integer" }
  }');
  // do stuff
}

foo( true, true ); // will throw exception

or how about passing PHAT nested containers using separate jsonschema file without getting verbose

function phat($container){
  TypeShave::check( $container, __DIR__."/test.json" ); 
}

$container = (object)array(
  "foo" => "foo",
  "bar" => "bar2",
  "records" => array(
    (object)array( "foo" => "foo" )
  )
);

phat($container);

see test.json here

why non-typesafe is great, but not with PHAT nested objects

For example:

  • REST payloads
  • objects which represent configs or options
  • datastructures and resultsets for html-rendering or processing purposes

Are you still passing phat data around fingers-crossed-style? Still wondering why functions like this explode once in a while? :D

foo( (object)array( "foo"=>"bar", "bar"=>123, "records": array( 1, 2 )) );

Did you you try PITA-fying your code with if/else checks?

function foo($data){
  if( isset($data)          && 
      is_object($data)      && 
      isset($data->foo)     && 
      is_string($data->foo) &&
      .. 
      && 
      .. 
      && Argh this is a big PITA 
  // omg how do I even check properties recursively?
  foreach( $data->records as $record ){
    // PITA 
    // PITA 
    // PITA 
    // PITA 
  }
  ...
  // now finally we can do what the function should do :/
}

Conclusion

No more :

  • functions going out of control
  • assertions-bloat inside functions
  • complaining about javascript not being typesafe
  • typesafe nested datastructures
  • verbose unittests doing typesafe stuff

Typeshave deals with problems immediately when they occur to prevent this:

NOTE: arrays

the v4 jsonschema validator does support arrays by noticing the 'items'-variable, so please omit type: "array" which can be found in older jsonschema formats:

   "myarray": {
//   "type": array,     *REMOVE ME*
     "items": [{
        "type": "integer"  
      }]
   }

coderofsalvation/typeshave 适用场景与选型建议

coderofsalvation/typeshave 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 08 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 coderofsalvation/typeshave 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 coderofsalvation/typeshave 我们能提供哪些服务?
定制开发 / 二次开发

基于 coderofsalvation/typeshave 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-08-23