🔧 (validate document) add script to generate the validate function

This commit is contained in:
Julien Calixte
2020-03-01 00:19:31 +01:00
parent b06617b860
commit ef2ee8d709
4 changed files with 66 additions and 23 deletions

31
package-lock.json generated
View File

@@ -3438,6 +3438,12 @@
"readable-stream": "^2.0.6" "readable-stream": "^2.0.6"
} }
}, },
"arg": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
"dev": true
},
"argparse": { "argparse": {
"version": "1.0.10", "version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
@@ -6144,6 +6150,12 @@
"integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==", "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==",
"dev": true "dev": true
}, },
"diff": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
"dev": true
},
"diff-sequences": { "diff-sequences": {
"version": "24.9.0", "version": "24.9.0",
"resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-24.9.0.tgz", "resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-24.9.0.tgz",
@@ -14990,6 +15002,19 @@
} }
} }
}, },
"ts-node": {
"version": "8.6.2",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-8.6.2.tgz",
"integrity": "sha512-4mZEbofxGqLL2RImpe3zMJukvEvcO1XP8bj8ozBPySdCUXEcU5cIRwR0aM3R+VoZq7iXc8N86NC0FspGRqP4gg==",
"dev": true,
"requires": {
"arg": "^4.1.0",
"diff": "^4.0.1",
"make-error": "^1.1.1",
"source-map-support": "^0.5.6",
"yn": "3.1.1"
}
},
"tsconfig": { "tsconfig": {
"version": "7.0.0", "version": "7.0.0",
"resolved": "https://registry.npmjs.org/tsconfig/-/tsconfig-7.0.0.tgz", "resolved": "https://registry.npmjs.org/tsconfig/-/tsconfig-7.0.0.tgz",
@@ -16514,6 +16539,12 @@
"decamelize": "^1.2.0" "decamelize": "^1.2.0"
} }
}, },
"yn": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
"integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
"dev": true
},
"yorkie": { "yorkie": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/yorkie/-/yorkie-2.0.0.tgz", "resolved": "https://registry.npmjs.org/yorkie/-/yorkie-2.0.0.tgz",

View File

@@ -6,7 +6,8 @@
"serve": "vue-cli-service serve", "serve": "vue-cli-service serve",
"build": "vue-cli-service build", "build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit", "test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint" "lint": "vue-cli-service lint",
"build-couch-design": "ts-node src/scripts/validate-doc-update.ts"
}, },
"dependencies": { "dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.25", "@fortawesome/fontawesome-svg-core": "^1.2.25",
@@ -57,6 +58,7 @@
"node-sass": "^4.12.0", "node-sass": "^4.12.0",
"sass-loader": "^7.3.1", "sass-loader": "^7.3.1",
"ts-jest": "^24.1.0", "ts-jest": "^24.1.0",
"ts-node": "^8.6.2",
"typescript": "~3.8.2", "typescript": "~3.8.2",
"vue-template-compiler": "^2.6.10" "vue-template-compiler": "^2.6.10"
} }

View File

@@ -1,21 +0,0 @@
import IAccount from '@/models/IAccount'
import ITransaction from '@/models/ITransaction'
import IExchange from '@/models/IExchange'
type Doc = IAccount | ITransaction | IExchange
interface IUserContext {
db: string
name: string | null
roles: string[]
}
export default {
validate_doc_update(newDoc: Doc, oldDoc: Doc, userCtx: IUserContext) {
if (newDoc.doctype === 'account' && oldDoc.doctype === 'account') {
if (newDoc.userIds.indexOf(userCtx.name || '') < 0) {
throw { forbidden: 'You do not have rights to update this account' }
}
}
}
}

View File

@@ -0,0 +1,31 @@
import IAccount from '@/models/IAccount'
import ITransaction from '@/models/ITransaction'
import IExchange from '@/models/IExchange'
type Doc = IAccount | ITransaction | IExchange
interface IUserContext {
db: string
name: string | null
roles: string[]
}
// tslint:disable-next-line: variable-name only-arrow-functions
const validate_doc_update = function(
newDoc: Doc,
oldDoc: Doc,
userCtx: IUserContext
) {
if (!userCtx.name) {
throw { forbidden: 'You need to be logged in to update accounts' }
}
if (newDoc.doctype === 'account' && oldDoc.doctype === 'account') {
if (true) {
throw { forbidden: 'You do not have rights to update this account' }
}
}
}
// tslint:disable-next-line:no-console
console.info(validate_doc_update.toString().replace(/\n/g, ''))