Skip to content

Commit

Permalink
fix: validation add service form in lab registration (#359)
Browse files Browse the repository at this point in the history
* fix: validation service form in registration

* fix: add handleRules file
  • Loading branch information
ciniiia authored Mar 11, 2022
1 parent 869fcc8 commit 73dc371
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 18 deletions.
80 changes: 80 additions & 0 deletions src/constants/error-messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Using Object.freeze for the constant means the object can't be changed. It's similar to enum on typescript

export default Object.freeze({
REQUIRED: "This field is required",
EMAIL: "Email is invalid. It should contain @ followed by a domain",
INCORRECT_PASSWORD: "The password you entered is incorrect",

/**
*
* @param {string} format
* @returns {string}
*/
FILE_FORMAT: (format) => {
const message = "The files uploaded are not in the supported file formats"
if (format) return message + ` (${format})`

return message
},

/**
*
* @param {number | string} size
* @returns {string}
*/
FILE_SIZE: (size) => {
const message = "The total file size uploaded exceeds the maximum file size allowed"
if (size) return message + ` ${size}MB`

return message
},

/**
*
* @param {number | string} number
* @returns {string}
*/
FILE_NUMBER: (number) => {
const message = "The total number of files uploaded exceeds the maximum file size allowed"
if (number) return message + ` (${number}) Files`

return message
},

/**
*
* @param {number | string} length
* @param {number | string} includeChars
* @returns {string}
*/
PASSWORD: (length = 0, includeChars) => {
const message = `Password must have at least ${length} character`
if (includeChars) return message + `, that include at least ${includeChars}`

return message
},

/**
*
* @param { string } label
* @returns {string}
*/
INVALID: (label) => {
const message = `${label} is Invalid`
return message
},

INPUT_CHARACTER: (type) => {
const message = `This field can only contain ${type}`
return message
},
/**
*
* @param {number | string} number
* @returns {string}
*/
MAX_CHARACTER: (number) => {
const message = `This field only allows ${number} characters`
return message
}
})
13 changes: 13 additions & 0 deletions src/constants/rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import errorMessages from "./error-messages"

export default Object.freeze({
FIELD_REQUIRED: val => !!val || errorMessages.REQUIRED,
ENGLISH_ALPHABET: val => (!!val && /^[A-Za-z0-9!@#$%^&*\\(\\)\-_=+:;"',.\\/? \n]+$/.test(val)) || errorMessages.INPUT_CHARACTER("English alphabet"),
MAX_CHARACTER: (max) => {
return val => (!!val && val.length <= max) || errorMessages.MAX_CHARACTER(max)
},
FILE_SIZE: (size) => {
return val => (!!val && val.size <= size) || errorMessages.FILE_SIZE(size / 1000000)
},
DEFAULT_ACCEPT_DOCUMENTS: val => (!!val && (val.type === "application/pdf" || val.type === "application/msword" || val.type === "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) || errorMessages.FILE_FORMAT("PDF/DOC/DOCX")
})
3 changes: 0 additions & 3 deletions src/store/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export default {
},
actions: {
async initApp({ commit }) {
const roleApp = process.env.NODE_ENV;
console.log('roleApp', roleApp)

const tokenName = process.env.VUE_APP_DEBIO_USE_TOKEN_NAME
const escrowETHAddress = process.env.VUE_APP_DEBIO_ESCROW_ETH_ADDRESS
const substrateWs = process.env.VUE_APP_DEBIO_SUBSTRATE_WS
Expand Down
36 changes: 21 additions & 15 deletions src/views/Dashboard/Lab/Registration/Services/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
placeholder="Service Name"
outlined
v-model="name"
:rules="[serviceNameRules, fieldRequiredRule]"
:rules="serviceNameRules"
></v-text-field>

<div class="d-flex">
Expand All @@ -81,7 +81,7 @@
step=".001"
outlined
v-model.number="price"
:rules="[priceRules, fieldRequiredRule]"
:rules="priceRules"
></v-text-field>
</v-col>
<v-col>
Expand All @@ -104,7 +104,7 @@
step=".001"
outlined
v-model.number="qcPrice"
:rules="[cqPriceRules, fieldRequiredRule]"
:rules="cqPriceRules"
></v-text-field>
</v-col>
</v-row>
Expand All @@ -116,7 +116,7 @@
placeholder="Short Description"
outlined
v-model="description"
:rules="[descriptionRules, fieldRequiredRule]"
:rules="descriptionRules"
></v-text-field>

<v-row >
Expand All @@ -130,7 +130,7 @@
outlined
type="number"
v-model="expectedDuration"
:rules="[expectedDurationRules, fieldRequiredRule]"
:rules="expectedDurationRules"
></v-text-field>
</v-col>
<v-col cols="4">
Expand All @@ -139,7 +139,7 @@
dense
v-model="selectExpectedDuration"
:items="listExpectedDuration"
:rules="[expectedDurationRules, fieldRequiredRule]"
:rules="fieldRequiredRule"
></v-select>
</v-col>
</v-row>
Expand All @@ -150,7 +150,7 @@
placeholder="Long Description"
outlined
v-model="longDescription"
:rules="[longDescriptionRules, fieldRequiredRule]"
:rules="longDescriptionRules"
></v-textarea>

<v-file-input
Expand Down Expand Up @@ -227,8 +227,8 @@ import DialogAlert from "@/components/Dialog/DialogAlert"
import serviceHandler from "@/mixins/serviceHandler"
import { toEther } from "@/lib/balance-format"
import { sendEmailRegisteredLab } from "@/lib/api/lab"
import rulesHandler from "@/constants/rules"
const englishAlphabet = val => (val && /^[A-Za-z0-9!@#$%^&*\\(\\)\-_=+:;"',.\\/? ]+$/.test(val)) || "This field can only contain English alphabet"
export default {
name: 'LabRegistrationServices',
Expand Down Expand Up @@ -299,46 +299,52 @@ export default {
fieldRequiredRule() {
return [
val => !!val || 'This field is required'
rulesHandler.FIELD_REQUIRED,
]
},
serviceNameRules() {
return [
val => (val && val.length <= 50) || 'This field only allows 50 characters',
englishAlphabet
rulesHandler.FIELD_REQUIRED,
rulesHandler.ENGLISH_ALPHABET,
rulesHandler.MAX_CHARACTER(50)
]
},
priceRules() {
return [
rulesHandler.FIELD_REQUIRED,
val => (val && val != 0) || 'Value on this field cannot 0',
val => /^\d*(\.\d{0,3})?$/.test(val) || this.isBiomedical || 'This field only allows 3 decimal characters.'
]
},
cqPriceRules() {
return [
rulesHandler.FIELD_REQUIRED,
val => /^\d*(\.\d{0,3})?$/.test(val) || this.isBiomedical || 'This field only allows 3 decimal characters.'
]
},
descriptionRules() {
return [
val => (val && val.length <= 100) || 'This field only allows 100 characters',
englishAlphabet
rulesHandler.FIELD_REQUIRED,
rulesHandler.ENGLISH_ALPHABET,
rulesHandler.MAX_CHARACTER(100)
]
},
longDescriptionRules() {
return [
val => (val && val.length <= 255) || 'This field only allows 255 characters',
englishAlphabet
rulesHandler.ENGLISH_ALPHABET,
rulesHandler.MAX_CHARACTER(255)
]
},
expectedDurationRules() {
return [
rulesHandler.FIELD_REQUIRED,
val => (val && val != 0) || 'Value on this field cannot 0'
]
},
Expand Down

0 comments on commit 73dc371

Please sign in to comment.