-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (102 loc) · 4.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var Ajv = require("ajv")
/**
* UNIVERSAL SCHEDULE STANDARD VALIDATOR
* Takes an object or string, checks whether it's valid JSON, then validates against the USS schema.
* Returns an object with isValid boolean, an info object and arrays of errors and warnings, if any.
* This is written in ECMA Script 5 to ensure wide compatibility
*
* @param {Object|string} item - The object or string to be validated against the USS schema.
* @returns {Object} - An object containing the validation result, info object, and arrays of errors and warnings.
*/
var schema_v100 = require('./schemas/schema_v1.0.0')
var error = require('./utils/errors')
var getArrayLengthForKey = require('./utils/getArrayLengthForKey')
var getValueForKey = require('./utils/getValueForKey')
var isJson = require('./utils/isJson')
var isObjectASchedule = require('./utils/isObjectASchedule')
var breakdownElementIdsInElements = require('./parity/breakdownElementIdsInElements')
var elementsHaveCategory = require('./parity/elementsHaveCategory')
var linkedElementsInElements = require('./parity/linkedElementsInElements')
var boardsBreakdownIdsEqualsBreakdowns = require('./parity/boardsBreakdownIdsEqualsBreakdowns')
var boardsBreakdownIdsExistsInBreakdowns = require('./parity/boardsBreakdownIdsExistsInBreakdowns')
var stripboardsHaveCalendar = require('./parity/stripboardsHaveCalendar')
var calendarsHaveStart = require('./parity/calendarsHaveStart')
// PRIMARY FUNCTION
module.exports = function(item) {
// establish response object
var response = {
isValid: false,
info: {
breakdowns: null,
categories: null,
elements: null,
stripboards: null,
calendars: null,
isSchedule: null,
name: null,
source: null,
ussVersion: null,
},
errors: [],
warnings: []
}
try {
if(isJson(item)) {
// create an object from the JSON
var objParsed = isJson(item, 'object')
// build ajv validator
var ajv = new Ajv()
// determine which schema to use
var validateUSS
switch(objParsed?.universalScheduleStandard?.ussVersion) {
case '1.0.0':
validateUSS = ajv.compile(schema_v100)
break
default:
break
}
if(!validateUSS) {
response.isValid = false
response.errors.push(error.createError({title: 'Schema Error', message: 'The file\'s USS version is not supported by this validator'}))
}
// if the object was successfully parsed
if(objParsed!==null) {
// check validation, collect any errors
response.isValid = validateUSS(objParsed)
response.errors = error.parseAjvErrors(validateUSS.errors)
if(response.errors.length===0) { // if there are no schema errors
// update info object
response.info.breakdowns = getArrayLengthForKey(objParsed, 'breakdowns')
response.info.categories = getArrayLengthForKey(objParsed, 'categories')
response.info.elements = getArrayLengthForKey(objParsed, 'elements')
response.info.stripboards = getArrayLengthForKey(objParsed, 'stripboards')
response.info.calendars = getArrayLengthForKey(objParsed, 'calendars')
response.info.isSchedule = isObjectASchedule(objParsed)
response.info.name = getValueForKey(objParsed, 'name')
response.info.source = getValueForKey(objParsed, 'source')
response.info.ussVersion = getValueForKey(objParsed, 'ussVersion')
// check parity between object id arrays
response.warnings = breakdownElementIdsInElements(objParsed, response.warnings)
response.warnings = elementsHaveCategory(objParsed, response.warnings)
response.warnings = linkedElementsInElements(objParsed, response.warnings)
response.warnings = boardsBreakdownIdsEqualsBreakdowns(objParsed, response.warnings)
response.warnings = boardsBreakdownIdsExistsInBreakdowns(objParsed, response.warnings)
response.warnings = stripboardsHaveCalendar(objParsed, response.warnings)
response.warnings = calendarsHaveStart(objParsed, response.warnings)
}
} else { // the item couldn't be converted into a JSON object
response.isValid = false
response.errors.push(error.createError({title: 'JSON Error', message: 'This data cannot be converted into a JSON object'}))
}
} else { // obj is not valid JSON
response.isValid = false
response.errors.push(error.createError({title: 'JSON Error', message: 'This is not a valid JSON object'}))
}
} catch(e) { // catch all other errors
response.isValid = false
response.errors.push(error.createError({message: e.message || 'There was an unknown error in the USS Validator'}))
} finally {
// return response object
return response
}
}