validate identifier

This commit is contained in:
Moriz Wahl 2023-05-16 06:09:30 +02:00
parent c204ede28f
commit b0fb6f8858
3 changed files with 145 additions and 144 deletions

View File

@ -125,108 +125,108 @@
</div>
</template>
<script>
import { SEND_TYPES } from '@/pages/Send'
import InputIdentifier from '@/components/Inputs/InputIdentifier'
import InputAmount from '@/components/Inputs/InputAmount'
import InputTextarea from '@/components/Inputs/InputTextarea'
import { user as userQuery } from '@/graphql/queries'
import { isEmpty } from 'lodash'
import { SEND_TYPES } from '@/pages/Send'
import InputIdentifier from '@/components/Inputs/InputIdentifier'
import InputAmount from '@/components/Inputs/InputAmount'
import InputTextarea from '@/components/Inputs/InputTextarea'
import { user as userQuery } from '@/graphql/queries'
import { isEmpty } from 'lodash'
export default {
name: 'TransactionForm',
components: {
InputIdentifier,
InputAmount,
InputTextarea,
},
props: {
balance: { type: Number, default: 0 },
identifier: { type: String, default: '' },
amount: { type: Number, default: 0 },
memo: { type: String, default: '' },
selected: { type: String, default: 'send' },
},
data() {
return {
form: {
identifier: this.identifier,
amount: this.amount ? String(this.amount) : '',
memo: this.memo,
},
radioSelected: this.selected,
userName: '',
}
},
methods: {
onValidation() {
this.$refs.formValidator.validate()
},
onSubmit() {
if (this.gradidoID) this.form.identifier = this.gradidoID
this.$emit('set-transaction', {
selected: this.radioSelected,
identifier: this.form.identifier,
amount: Number(this.form.amount.replace(',', '.')),
memo: this.form.memo,
userName: this.userName,
})
},
onReset(event) {
event.preventDefault()
this.form.identifier = ''
this.form.amount = ''
this.form.memo = ''
this.$refs.formValidator.validate()
if (this.$route.query && !isEmpty(this.$route.query))
this.$router.replace({ query: undefined })
},
},
apollo: {
UserName: {
query() {
return userQuery
},
fetchPolicy: 'network-only',
variables() {
return { identifier: this.gradidoID }
},
skip() {
return !this.gradidoID
},
update({ user }) {
this.userName = `${user.firstName} ${user.lastName}`
},
error({ message }) {
this.toastError(message)
},
},
},
computed: {
disabled() {
if (
this.form.identifier.length > 5 &&
parseInt(this.form.amount) <= parseInt(this.balance) &&
this.form.memo.length > 5 &&
this.form.memo.length <= 255
) {
return false
}
return true
},
isBalanceDisabled() {
return this.balance <= 0 ? 'disabled' : false
},
sendTypes() {
return SEND_TYPES
},
gradidoID() {
return this.$route.query && this.$route.query.gradidoID
},
},
mounted() {
if (this.form.identifier !== '') this.$refs.formValidator.validate()
},
}
export default {
name: 'TransactionForm',
components: {
InputIdentifier,
InputAmount,
InputTextarea,
},
props: {
balance: { type: Number, default: 0 },
identifier: { type: String, default: '' },
amount: { type: Number, default: 0 },
memo: { type: String, default: '' },
selected: { type: String, default: 'send' },
},
data() {
return {
form: {
identifier: this.identifier,
amount: this.amount ? String(this.amount) : '',
memo: this.memo,
},
radioSelected: this.selected,
userName: '',
}
},
methods: {
onValidation() {
this.$refs.formValidator.validate()
},
onSubmit() {
if (this.gradidoID) this.form.identifier = this.gradidoID
this.$emit('set-transaction', {
selected: this.radioSelected,
identifier: this.form.identifier,
amount: Number(this.form.amount.replace(',', '.')),
memo: this.form.memo,
userName: this.userName,
})
},
onReset(event) {
event.preventDefault()
this.form.identifier = ''
this.form.amount = ''
this.form.memo = ''
this.$refs.formValidator.validate()
if (this.$route.query && !isEmpty(this.$route.query))
this.$router.replace({ query: undefined })
},
},
apollo: {
UserName: {
query() {
return userQuery
},
fetchPolicy: 'network-only',
variables() {
return { identifier: this.gradidoID }
},
skip() {
return !this.gradidoID
},
update({ user }) {
this.userName = `${user.firstName} ${user.lastName}`
},
error({ message }) {
this.toastError(message)
},
},
},
computed: {
disabled() {
if (
this.form.identifier.length > 5 &&
parseInt(this.form.amount) <= parseInt(this.balance) &&
this.form.memo.length > 5 &&
this.form.memo.length <= 255
) {
return false
}
return true
},
isBalanceDisabled() {
return this.balance <= 0 ? 'disabled' : false
},
sendTypes() {
return SEND_TYPES
},
gradidoID() {
return this.$route.query && this.$route.query.gradidoID
},
},
mounted() {
if (this.form.identifier !== '') this.$refs.formValidator.validate()
},
}
</script>
<style>
span.errors {

View File

@ -27,43 +27,43 @@
</validation-provider>
</template>
<script>
export default {
name: 'InputEmail',
props: {
rules: {
default: () => {
return {
required: true,
validIdentifier: true,
}
},
},
name: { type: String, required: true },
label: { type: String, required: true },
placeholder: { type: String, required: true },
value: { type: String, required: true },
disabled: { type: Boolean, required: false, default: false },
},
data() {
return {
currentValue: this.value,
}
},
computed: {
labelFor() {
return this.name + '-input-field'
},
},
watch: {
currentValue() {
this.$emit('input', this.currentValue)
},
value() {
if (this.value !== this.currentValue) {
this.currentValue = this.value
}
this.$emit('onValidation')
},
},
}
export default {
name: 'InputEmail',
props: {
rules: {
default: () => {
return {
required: true,
validIdentifier: true,
}
},
},
name: { type: String, required: true },
label: { type: String, required: true },
placeholder: { type: String, required: true },
value: { type: String, required: true },
disabled: { type: Boolean, required: false, default: false },
},
data() {
return {
currentValue: this.value,
}
},
computed: {
labelFor() {
return this.name + '-input-field'
},
},
watch: {
currentValue() {
this.$emit('input', this.currentValue)
},
value() {
if (this.value !== this.currentValue) {
this.currentValue = this.value
}
this.$emit('onValidation')
},
},
}
</script>

View File

@ -5,9 +5,10 @@ import { checkUsername } from '@/graphql/queries'
import { validate as validateUuid, version as versionUuid } from 'uuid'
// taken from vee-validate
// eslint-disable-next-line no-useless-escape
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
const usernameRegex = /^(?=.{3,20}$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9])*$/
const usernameRegex = /^(?=.{3,20}$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+?)*$/
export const loadAllRules = (i18nCallback, apollo) => {
configure({
@ -147,7 +148,7 @@ export const loadAllRules = (i18nCallback, apollo) => {
extend('usernameUnique', {
validate(value) {
if (value.match(/^(?=.{3,20}$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+?)*$/)) {
if (value.match(usernameRegex)) {
return apollo
.query({
query: checkUsername,
@ -169,8 +170,8 @@ export const loadAllRules = (i18nCallback, apollo) => {
validate(value) {
const isEmail = !!value.match(emailRegex)
const isUsername = !!value.match(usernameRegex)
// const isGradidoId = validateUuid(value) && versionUuid(value) === 4
return isEmail || isUsername // || isGradidoId
const isGradidoId = validateUuid(value) && versionUuid(value) === 4
return isEmail || isUsername || isGradidoId
},
message: (_, values) => i18nCallback.t('form.validation.valid-identifier', values),
})