首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FrontEnd中的RAML API验证

FrontEnd中的RAML API验证
EN

Stack Overflow用户
提问于 2017-08-30 00:19:41
回答 1查看 557关注 0票数 1

我正在React应用程序中编写验证代码,以检查输入是否与RAML规范匹配。我使用'raml-validate'

我还不能弄清楚的是,在发送POST请求之前,我如何添加代码来验证与RAML规范匹配的输入。

有可能实现这一点吗?

我的代码如下

Login.js

代码语言:javascript
复制
import React from 'react';
import axios from 'axios';
import {FormRow} from './Forms';
import validate from '../validate';

export default class LoginForm extends React.Component {

constructor(props) {
super(props);
this.state = {email: '',
          email_error: '',
          password_error: '',
          password: ''};

this.emailChanged = this.emailChanged.bind(this);
this.passwordChanged = this.passwordChanged.bind(this);
this.login = this.login.bind(this);
}


login(e) {
e.preventDefault();
if (!this.validateInput()) return;
var self = this;
if (!this.validate.user) {
axios.post('/login', {email: this.state.email,
              password:this.state.password})
        .then(function(response) {
    self.props.onLogin(response.data.email,
               response.data.tenant);
    }).catch(function(error) {
    if (error.response.status == 401) {
        self.setState({password_error: "Invalid email/password combination"});
    } else {
        console.log(error.message);
    }
    });
  } else {
    alert('error');
  }
}

emailChanged(e) {
console.log('error');
this.setState({email: e.target.value});
}

passwordChanged(e) {
this.setState({password: e.target.value});
}

validateInput() {
var valid = true;
if (!this.state.email) {
    console.log('error');
    this.setState({email_error: "Please enter your email"});
    valid = false;
} else {
    this.setState({email_error: ""});
}

if (!this.state.password) {
    this.setState({password_error: "Please enter your password"});
    valid = false;
} else {
    this.setState({password_error: ""});
}
return valid;
}

render() {
return (
    <div className="container">
      <form className="form-signin">
    <h2 className="form-signin-heading">Twyla Client Application</h2>
    <FormRow fieldType={"email"}
         placeholder={"Email"}
         value={this.state.email}
         onChange={this.emailChanged}
         error={this.state.email_error}
         autoFocus={true} />
    <FormRow fieldType={"password"}
         placeholder={"Password"}
         value={this.state.password}
         error={this.state.password_error}
         onChange={this.passwordChanged} />
    <input className="btn btn-lg btn-primary btn-block"
           value="Log in"
           type="submit" onClick={this.login} />
      </form>
      <p>{this.user.email}</p>
      <p>ahaha</p>
    </div>
);
}
}

validate.js

代码语言:javascript
复制
var RAMLVersion = 'RAML10'

var user = validate({
  email: {
    type: 'string',
    required: true
},
password: {
    type: 'string',
    required: true
 }}, RAMLVersion);

export const validate = () => {
 let errors = {}
 return errors
 }

api-spec.raml

代码语言:javascript
复制
protocols: [ HTTPS ]
baseUri: https://www.twylahelps.com/api/{version}
version: v1

schemas:
  - bad request:
   body:
    application/json:
      example: |
        {"error": "bad request"}

- unauthorized:
   body:
    application/json:
      example: |
        {"error": "unauthorized"}


/login:
 post:
 description: logs in a user
 body:
  application/json:
    schema: |
      {
        "$schema": "http://json-schema.org/draft-04/schema",
        "type" : "object",
        "properties" : {
          "email": {
            "type": "string"
          },
          "password": {
            "type": "string"
          }
        },
        "required": ["email", "password"],
        "additionalProperties": false
      }
 reponses:
  200:
   description: User logged in successfully
   body:
    application/json:
      example: {'email': "username@email.xyz", 'tenant': "abc"}
  401:
   body:
    application/json:
      schema: unauthorized
EN

回答 1

Stack Overflow用户

发布于 2017-09-01 20:43:36

我决定不使用'raml-validate‘。相反,我使用了'ajv(Another JSON Validator)。它工作得很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45943936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档