Last active
December 19, 2016 04:01
-
-
Save mokjpn/8c4c9d8a7b6338e11045055573a26303 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This gist is obsoleted. Refer https://github.com/mokjpn/Define2Validate/ for the new version. | |
library(R4DSXML) | |
library(testthat) | |
library(validate) | |
define2validate <- function(domain, file="exampleRules.yaml", definexml="Odm_Define.xml") { | |
varmd <- subset(getVarMD(definexml), IGD_Name == domain) | |
cat(file=file, append=FALSE, "") | |
out <- function(...) cat(file=file, append=TRUE, paste(...,"\n",sep="")) | |
out("rules:") | |
expect_gt(nrow(varmd), 0) | |
for(row in 1:nrow(varmd)) { | |
varname <- varmd[row, "ID_Name"] | |
varLength <- varmd[row, "ID_Length"] | |
if(!is.na(varLength)) { | |
out("-") | |
out(" expr: nchar(as.character(", varname, ")) <= ", varLength) | |
out(" name: Length of ", varname) | |
} | |
varMandatory <- varmd[row, "IR_Mandatory"] | |
expect_false(is.na(varMandatory)) | |
if(varMandatory == "Yes") { | |
out("-") | |
out(" expr: '!is.na(", varname, ")'") | |
out(" name: ",varname, " is mandatory") | |
} | |
varDataType <- varmd[row, "ID_DataType"] | |
expect_false(is.na(varDataType)) | |
if(varDataType == "integer") { | |
out("-") | |
out(" expr: regexpr(\"^[0-9-]+$\",as.character(", varname,")) == 1") | |
out(" name: ",varname, " should be integer") | |
} | |
if(varDataType == "date") { | |
out("-") | |
out(" expr: regexpr(\"^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})$\",as.character(", varname,")) == 1") | |
out(" name: ", varname, " should be Date") | |
} | |
varCodeList <- varmd[row, "ID_CodeListOID"] | |
if(!is.na(varCodeList)) { | |
out("-") | |
out(" expr: as.character(", varname, ") %in% CT[CT$OID == \"", varCodeList, "\", \"CodedValue\"]" ) | |
out(" name: ", varname, " should follow codelist " , varCodeList) | |
} | |
} | |
out("---") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment