Просмотр исходного кода

:construction: Started validation for survey procedures

tags/0.0.3^2
tomit4 3 лет назад
Родитель
Сommit
81c6eca827

+ 1
- 5
frontend/src/components/onboarding/FormInput.vue Просмотреть файл

@@ -23,11 +23,7 @@ export default {
23 23
     }),
24 24
     methods: {
25 25
         handleSubmit(answerInfo) {
26
-            if (answerInfo.question.survey_stage === 'password') {
27
-                this.$emit('update-answers') // don't reveal password here...
28
-            } else {
29
-                this.$emit('update-answers', answerInfo)
30
-            }
26
+            this.$emit('update-answers', answerInfo)
31 27
         },
32 28
         submitImage() {
33 29
             let payload = {

+ 9
- 2
frontend/src/components/onboarding/QuestionResponse.vue Просмотреть файл

@@ -26,16 +26,23 @@ export default {
26 26
             { answer: 4 },
27 27
             { answer: 5 },
28 28
         ],
29
+        isAnswered: false
29 30
     }),
30 31
     methods: {
31 32
         onUpdate(e) {
33
+            this.isAnswered = true
32 34
             this.$emit('updated', { ...this.question, answer: e + 1 })
33 35
         },
36
+        // TODO: render message to user on why they cannot proceed
34 37
         updateAnswers() {
35
-            this.$emit('update-is-answered', this.question.id)
38
+            this.isAnswered ?
39
+            this.$emit('update-is-answered', this.question.id) :
40
+            console.error('no selection made')
36 41
         },
37 42
         updateAll() {
38
-            this.$emit('update-all')
43
+            this.isAnswered ? 
44
+            this.$emit('update-all') : 
45
+            console.error('no selection made')
39 46
         }
40 47
     },
41 48
 }

+ 31
- 0
frontend/src/entities/survey/survey.answer.schema.js Просмотреть файл

@@ -0,0 +1,31 @@
1
+import Joi from 'joi'
2
+
3
+/**
4
+ * answers schema object
5
+ */
6
+const answersSchema = {
7
+    type: 'object',
8
+    properties: Joi.object().keys({
9
+        name: Joi.string().required(),
10
+        email: Joi.string().email({ minDomainSegments: 2, tlds: false }),
11
+        // TODO: Refine password regex to have more secure requirements
12
+        password: Joi.string().min(10).max(30).pattern(new RegExp('[a-zA-Z0-9]+')),
13
+        // TODO: Change if going international (only works in usa)
14
+        zipcode: Joi.string().min(5).max(5).pattern(new RegExp('^[0-9]{5}$')),
15
+        seeking: Joi.string(),
16
+        urgency: Joi.string(),
17
+        presence: Joi.string(),
18
+        duration: Joi.string(),
19
+        pronouns: Joi.string(),
20
+        language: Joi.string(),
21
+        image: Joi.any(),
22
+        distance: Joi.string(),
23
+        blurb: Joi.string(),
24
+        aspects: Joi.array().items(Joi.number().allow(null))
25
+    }),
26
+    validate(instance) {
27
+        return this.properties.validate(instance)
28
+    },
29
+}
30
+
31
+export { answersSchema }

+ 9
- 0
frontend/src/entities/survey/survey.js Просмотреть файл

@@ -1,6 +1,7 @@
1 1
 /** @module survey/survey */
2 2
 import { _baseRecord } from '../index.js'
3 3
 import { surveySchema } from './survey.schema.js'
4
+import { answersSchema } from './survey.answer.schema.js'
4 5
 
5 6
 const SCORED = [1, 2, 3, 4, 5, 6]
6 7
 const _isScored = id => SCORED.includes(id)
@@ -38,6 +39,14 @@ class Survey extends _baseRecord {
38 39
         console.log('this.aspectQuestions: ', JSON.stringify(this.aspectQuestions))
39 40
     }
40 41
 
42
+    validateAnswer(answer) {
43
+        const validate = answersSchema.validate(answer)
44
+        if (validate.error) {
45
+            console.error(`error: ${validate.error}`)
46
+        }
47
+        return !validate.error ? true: false
48
+    }
49
+
41 50
     isValid() {
42 51
         const validate = surveySchema.validate(this)
43 52
 

+ 8
- 1
frontend/src/views/OnboardingView.vue Просмотреть файл

@@ -61,9 +61,16 @@ export default {
61 61
         },
62 62
         updateAnswers(payload) {
63 63
             // null payload is passed on splash page
64
-            if (payload) {
64
+            if (this.currentStep !== 0) {
65
+                console.log('this.survey >>', this.survey)
65 66
                 const k = payload.question.survey_stage
66 67
                 this.answered[k] = payload.input
68
+                // TODO: render message to user on why they cannot proceed
69
+                if (!this.survey.validateAnswer(this.answered)) {
70
+                    return console.error('invalid answer')
71
+                }
72
+                // once validated, don't log password in answered object
73
+                this.answered[k] = k === 'password' ? undefined : payload.input 
67 74
                 console.log(`Updated answers: ${JSON.stringify(this.answered)}`)
68 75
                 if (k === 'aspects') return
69 76
             }

Загрузка…
Отмена
Сохранить