Last active
December 2, 2019 09:51
-
-
Save potato4d/397f699c5d2a84398215a76640694152 to your computer and use it in GitHub Desktop.
フロントエンドのバリデーションを hapi の joi で行うと手軽で便利 ref: https://qiita.com/potato4d/items/e2c88c940516aeb369b0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="app"> | |
<form> | |
<p> | |
<label for="user_id">UserID</label> | |
<input @blur="validate('user_id')" type="text" v-model="formData.user_id" id="user_id"> | |
<span class="error" v-if="errors.user_id"><br>ユーザーIDが不正です</span> | |
</p> | |
<p> | |
<label for="email">Eメールアドレス</label> | |
<input @blur="validate('email')" v-model="formData.email" type="text" id="user_id"> | |
<span class="error" v-if="errors.email"><br>メールアドレスが不正です</span> | |
</p> | |
<p> | |
<label for="password">パスワード</label> | |
<input @blur="validate('password')" v-model="formData.password" type="text" id="password"> | |
<span class="error" v-if="errors.password"><br>パスワードが不正です</span> | |
</p> | |
<p> | |
<input type="submit" :disabled="!isValid"> | |
</p> | |
</form> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new Vue({ | |
el: '#app', | |
data () { | |
return { | |
formData: { | |
user_id: '', | |
email: '', | |
password: '' | |
}, | |
errors: { | |
user_id: false, | |
email: false, | |
password: false | |
}, | |
touched: { | |
user_id: false, | |
email: false, | |
password: false | |
} | |
} | |
}, | |
methods: { | |
validate (name) { | |
this.touched[name] = true | |
Object.keys(this.errors).forEach((k)=>{ | |
this.errors[k] = false | |
}) | |
const result = Joi.validate({...this.formData}, this.schema, { abortEarly: false }) | |
console.log(result) | |
if (result.error) { | |
result.error.details.forEach((detail) => { | |
const name = detail.path[0] | |
if(this.touched[name]) this.errors[name] = true | |
}) | |
} | |
} | |
}, | |
computed: { | |
isValid () { | |
let isValid = true | |
Object.entries(this.errors).forEach(([k,v]) => { | |
if(v) isValid = false | |
}) | |
Object.entries(this.touched).forEach(([k,v]) => { | |
if(!v) isValid = false | |
}) | |
return isValid | |
}, | |
schema () { | |
return Joi.object().keys({ | |
user_id: Joi.string().required(), | |
email: Joi.string().email(), | |
password: Joi.string().min(12).required() | |
}) | |
} | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Joi from 'joi-browser' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"message": "\"email\" must be a valid email", | |
"path": [ | |
"email" | |
], | |
"type": "string.email", | |
"context": { | |
"value": "invalid_string", | |
"key": "email", | |
"label": "email" | |
} | |
}, | |
{ | |
"message": "\"password\" length must be at least 12 characters long", | |
"path": [ | |
"password" | |
], | |
"type": "string.min", | |
"context": { | |
"limit": 12, | |
"value": "12345678", | |
"key": "password", | |
"label": "password" | |
} | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ yarn add joi-browser |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node index.js | jq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment