简体   繁体   English

服务器和客户端验证器-库

[英]Server and client side validators - library

What is best library to validate objects on server and client side ? 什么是在服务器和客户端上验证对象的最佳库? I'm searching something simpler - one simple config file and then in both sides something like: 我正在搜索更简单的东西-一个简单的配置文件,然后在两侧都类似:

var is_valid = validation(object, validationRule); var is_valid =验证(object,validationRule);

But It should have many validation options, example: 但是它应该有许多验证选项,例如:

obj: A, B, C, D;
A = something;
B, C - Required;
D - Not needed/hidden


D = something:
C - Required;
B - optional/has value
A - Not needed/hidden

On client side I use Aurelia framework. 在客户端,我使用Aurelia框架。 It has a validation plugin, but I have no idea how to use it on server. 它有一个验证插件,但我不知道如何在服务器上使用它。 It means that, I would like to use the same functionValidation and configValidation on server, and client; 这意味着,我想在服务器和客户端上使用相同的functionValidation和configValidation;

Or perhaps - there is a plugin which would work with aurelia-validation ? 或者,也许-有一个可以与aurelia-validation一起使用的插件?

On server I use Node.js as backend; 在服务器上,我使用Node.js作为后端;

I too am looking for this - it would be great it we didn't have to duplicate our work. 我也正在寻找这个-很好,我们不必重复我们的工作。

Joi looks excellent for server side only validation (it powers Walmart and PayPal and is very fast) - but it's only kinda possible to use it on the client. 寻找优秀的服务器端验证只(它的权力沃尔玛和PayPal是非常快的) -但它只是有点可以使用它在客户端上。

Then there's validate.js which looks like it might do both. 然后是validate.js ,看起来它可能同时完成这两项工作。

I opened a reddit question since I can't find much info on this. 我在Reddit上提出了一个问题,因为我找不到太多信息。

I was able to implement something using this commit as a reference: https://github.com/aurelia/validation/commit/1b701ab 我可以使用此提交作为参考来实现一些东西: https : //github.com/aurelia/validation/commit/1b701ab

I'm pretty new to aurelia so it was pretty much a copy-paste job, but it works so I guess I can be happy about that. 我对aurelia来说还很陌生,所以这几乎是一个复制粘贴工作,但它确实有效,所以我想我对此很满意。 This is all in typescript, but you can get the general idea for a JS implementation. 这些都是打字稿中的内容,但是您可以了解JS实现的总体思路。

import 'aurelia-polyfills';
import { initialize } from 'aurelia-pal-nodejs';
import { Container } from 'aurelia-dependency-injection';
import { configure as configureBindingLanguage } from 'aurelia-templating-binding';
import { configure as configureValidation, ValidationRules, Validator, Rule, ValidateResult, ControllerValidateResult } from 'aurelia-validation';
import { HandlerError } from 'api/handler.error';

class AureliaValidator {
  private validator: Validator

  constructor() {
    initialize();
    const container = new Container();
    configureBindingLanguage({ container });
    configureValidation({ container });

    this.validator = container.get(Validator);
  }

  validateObject(object: any, rules: Rule<any, any>[][]): Promise<ValidateResult[]> {
    return this.validator.validateObject(object, rules).then(results => {
      let isValid = results.every(r => r.valid);
      let errors = results.map(r => r.message).filter(m => !!m);

      if (!isValid) throw new Error(errors[0]);

      return results;
    });
  }
}

export const aureliaValidator = new AureliaValidator();

and then it's used as follows 然后按如下方式使用

import { ValidationRules } from 'aurelia-validation';
import { aureliaValidator } from "validator.ts";

let foo = {
  bar: undefined
}

let rules = ValidationRules.ensure('bar').required().rules

// This will fail
aureliaValidator.validateObject(req.body, rules)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM