Преглед изворни кода

:pencil: swapping out for joi

tags/0.0.1
TOJ пре 5 година
родитељ
комит
26806ca285

+ 2661
- 44
package-lock.json
Разлика између датотеке није приказан због своје велике величине
Прегледај датотеку


+ 1
- 1
package.json Прегледај датотеку

@@ -9,7 +9,7 @@
9 9
   "dependencies": {
10 10
     "ajv": "^8.2.0",
11 11
     "events": "^3.3.0",
12
-    "pouchdb-browser": "^7.2.2",
12
+    "joi": "^17.4.0",
13 13
     "process": "^0.11.10",
14 14
     "vue": "^3.0.5"
15 15
   },

+ 4
- 8
src/components/HelloWorld.vue Прегледај датотеку

@@ -29,19 +29,15 @@ const state = reactive({
29 29
 })
30 30
 
31 31
 state.profile = new Profile({
32
-    email: 'donot@disturb.com',
32
+    email: 'donot@disturb.org',
33 33
     street: '123 strawberry ln',
34
-    apt: '2',
34
+    apt: 2,
35 35
     city: 'candyland',
36 36
     state: 'lollipop',
37 37
     zip: 90002
38 38
 })
39
-api.removeAll()
40
-api.put(state.profile).then(p => {
41
-    state.loaded = true
42
-    console.log(p)
43
-    // api.sync()
44
-})
39
+console.log(state.profile)
40
+console.log(state.profile.isValid())
45 41
 </script>
46 42
 
47 43
 <style lang="postcss">

+ 3
- 3
src/entities/index.js Прегледај датотеку

@@ -1,9 +1,9 @@
1 1
 export * from './_modules'
2 2
 
3 3
 /** Single instance of your schema handler */
4
-import Ajv from 'ajv'
5
-const ajv = new Ajv()
6
-export { ajv } 
4
+import Joi from 'joi'
5
+
6
+export { Joi }
7 7
 
8 8
 /** Your different entities */
9 9
 export * from './profile'

+ 9
- 9
src/entities/profile/profile.js Прегледај датотеку

@@ -1,6 +1,6 @@
1 1
 /** @module entities/profile */
2 2
 
3
-import { _baseRecord, allModules, ajv } from '..'
3
+import { _baseRecord, allModules, Joi } from '..'
4 4
 import { profileSchema } from './profile.schema'
5 5
 
6 6
 /** Class representing a profile */
@@ -16,7 +16,7 @@ class Profile extends _baseRecord {
16 16
         super()
17 17
 
18 18
         this.type = this.constructor.name.toLowerCase()
19
-        
19
+
20 20
         /**  Fields */
21 21
         this.email = email // ! required
22 22
 
@@ -30,20 +30,20 @@ class Profile extends _baseRecord {
30 30
      * @return {boolean} is it valid or not?
31 31
      */
32 32
     isValid() {
33
-        const validate = ajv.compile(profileSchema)
33
+        const validate = profileSchema.validate(this)
34 34
 
35 35
         /**
36 36
          * Log out some useful error messages
37
-         * TODO: Send validate.errors to logging error handler
37
+         * TODO: Send validate.error to logging error handler
38 38
          */
39
-        if(validate(this) !== true && validate.errors && validate.errors.length > 0) {
40
-            validate.errors.forEach(err => {
41
-                console.error(`error: ${err.instancePath} field ${err.message} - ${this.type} validation`)
42
-            })
39
+        if(validate.error) {
40
+            console.error(
41
+                `error: ${validate.error} - ${this.type} validation`
42
+            )
43 43
         }
44 44
 
45 45
         /** validate(this) always returns something so force it to a bool */
46
-        return validate(this) === true ? true : false
46
+        return !validate.error ? true : false
47 47
     }
48 48
 }
49 49
 

+ 19
- 14
src/entities/profile/profile.schema.js Прегледај датотеку

@@ -1,5 +1,5 @@
1 1
 /** @module entities/profileSchema */
2
-import { allModules } from '..'
2
+import { allModules, Joi } from '..'
3 3
 
4 4
 /**
5 5
  * profile schema object
@@ -9,28 +9,33 @@ import { allModules } from '..'
9 9
 */
10 10
 const profileSchema = {
11 11
     type: 'object',
12
-    properties: {
12
+    properties: Joi.object().keys({
13 13
         /** _baseRecord fields */
14
-        createdAt: { type: 'string' },
15
-        _id: { type: 'string' },
16
-        lastUpdatedAt: { type: 'string' },
17
-        type: { type: 'string' },
14
+        createdAt: Joi.string(),
15
+        _id: Joi.string(),
16
+        lastUpdatedAt: Joi.string(),
17
+        type: Joi.string(),
18 18
 
19 19
         /** our fields */
20
-        email: { type: 'string' },
20
+        email: Joi.string().email({
21
+            minDomainSegments: 2,
22
+            tlds: { allow: ['com', 'net'] }
23
+        }).required(),
21 24
 
22 25
         /** tricky module system fields */
23 26
         ...allModules.location({
24
-            street: { type: 'string' },
25
-            apt: { type: 'string' },
26
-            city: { type: 'string' },
27
-            state: { type: 'string' },
28
-            zip: { type: 'number' }
27
+            street: Joi.string(),
28
+            apt: Joi.string(),
29
+            city: Joi.string(),
30
+            state: Joi.string(),
31
+            zip: Joi.number()
29 32
         })
30
-    },
33
+    }),
31 34
     /** fields required before saving */
32 35
     required: [ 'email' ],
33
-    additionalProperties: false
36
+    validate(instance) {
37
+        return this.properties.validate(instance)
38
+    }
34 39
 }
35 40
 
36 41
 export { profileSchema }

+ 6
- 28
src/utils/db.js Прегледај датотеку

@@ -1,49 +1,27 @@
1
-import PouchDB from 'pouchdb-browser'
2
-
3 1
 const domain = 'localhost'
4
-const port = 5984
2
+const port = 3306
5 3
 const remote = `http://${domain}:${port}`
6 4
 
7 5
 class Connector {
8
-    constructor(dbName) {
9
-        this.local = new PouchDB(dbName)
10
-        this.db = new PouchDB(`${remote}/${dbName}`)
11
-    }
6
+    constructor() { this.db = null }
12 7
     async put(entry) {
13 8
         /** from ajv schema validation */
14 9
         if(entry.isValid()) {
15
-            try {
16
-                const res = await this.local.put(entry)
17
-                if(!res.ok) throw 'put to PouchDB failed'
18
-                return res
19
-            }
10
+            try { }
11
+
20 12
             /** valid data but can't save */
21 13
             catch (err) {
22 14
                 console.error('can\'t put', entry)
23 15
             }
24 16
         }
17
+
25 18
         else {
26 19
             /** not valid data */
27 20
             console.error('NOT VALID!', entry.isValid())
28 21
         }
29 22
     }
30
-    async sync() {
31
-        this.local.sync(this.db).on('complete', () => {
32
-            console.log("sync'd!")
33
-        }).on('error', err => {
34
-            console.error("not sync'd!", err)
35
-        })
36
-    }
37 23
     /** !: DEV ONLY */
38
-    async removeAll() {
39
-        const allDocs = await this.local.allDocs({
40
-            include_docs: true,
41
-            deleted: true
42
-        })
43
-        return Promise.all(allDocs.rows.map(row => {
44
-            return this.local.remove(row.id, row.value.rev)
45
-        }))
46
-    }
24
+    // async removeAll() { }
47 25
 }
48 26
 
49 27
 export { Connector }

Loading…
Откажи
Сачувај