sqler sql 转rest api 数据校验的处理

早期版本(2.0 之前)使用rules 进行数据校验处理,2.0 之后进行了修改使用
validators,这样更加明确

参考格式

 
addpost {
    // if any rule returns false, 
    // SQLer will return 422 code, with invalid rules.
    // 
    // $input is a global variable holds all request inputs,
    // including the http headers too (prefixed with `http_`)
    // all http header keys are normalized to be in this form 
    // `http_x_header_example`, `http_authorization` ... etc in lower case.
    validators {
        post_title_length = "$input.post_title && $input.post_title.trim().length > 0"
        post_content_length = "$input.post_content && $input.post_content.length > 0"
        post_user = "$input.post_user"
    }
    bind {
        title = "$input.post_title"
        content = "$input.post_content"
        user_id = "$input.post_user"
    }
    exec = <<SQL
        INSERT INTO posts(user_id, title, content) VALUES(:user_id, :title, :content);
        SELECT * FROM posts WHERE id = LAST_INSERT_ID();
    SQL
}
 
 

参考资料

https://github.com/alash3al/sqler

posted on 2019-01-14 10:08  荣锋亮  阅读(258)  评论(0编辑  收藏  举报

导航