UNPKG

mapeo-schema

Version:

JSON schema and flow types for Mapeo

848 lines 40.4 kB
'use strict'; var equal = require('ajv/lib/compile/equal'); var validate = (function() { var refVal = []; return function validate(data, dataPath, parentData, parentDataProperty, rootData) { 'use strict'; /*# sourceURL=http://mapeo.world/schemas/field.json */ var vErrors = null; var errors = 0; if ((data && typeof data === "object" && !Array.isArray(data))) { if (true) { var errs__0 = errors; var valid1 = true; for (var key0 in data) { var isAdditional0 = !(false || validate.schema.properties.hasOwnProperty(key0)); if (isAdditional0) { valid1 = false; validate.errors = [{ keyword: 'additionalProperties', dataPath: (dataPath || '') + "", schemaPath: '#/additionalProperties', params: { additionalProperty: '' + key0 + '' }, message: 'should NOT have additional properties' }]; return false; break; } } if (valid1) { if (data.id === undefined) { valid1 = false; validate.errors = [{ keyword: 'required', dataPath: (dataPath || '') + "", schemaPath: '#/required', params: { missingProperty: 'id' }, message: 'should have required property \'id\'' }]; return false; } else { var errs_1 = errors; if (typeof data.id !== "string") { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.id', schemaPath: '#/properties/id/type', params: { type: 'string' }, message: 'should be string' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { var data1 = data.key; if (data1 === undefined) { valid1 = false; validate.errors = [{ keyword: 'required', dataPath: (dataPath || '') + "", schemaPath: '#/required', params: { missingProperty: 'key' }, message: 'should have required property \'key\'' }]; return false; } else { var errs_1 = errors; var errs__1 = errors, prevValid1 = false, valid1 = false, passingSchemas1 = null; var errs_2 = errors; if (typeof data1 !== "string") { var err = { keyword: 'type', dataPath: (dataPath || '') + '.key', schemaPath: '#/properties/key/oneOf/0/type', params: { type: 'string' }, message: 'should be string' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid2 = errors === errs_2; if (valid2) { valid1 = prevValid1 = true; passingSchemas1 = 0; } var errs_2 = errors; if (Array.isArray(data1)) { var errs__2 = errors; var valid2; for (var i2 = 0; i2 < data1.length; i2++) { var errs_3 = errors; if (typeof data1[i2] !== "string") { var err = { keyword: 'type', dataPath: (dataPath || '') + '.key[' + i2 + ']', schemaPath: '#/properties/key/oneOf/1/items/type', params: { type: 'string' }, message: 'should be string' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid3 = errors === errs_3; if (!valid3) break; } } else { var err = { keyword: 'type', dataPath: (dataPath || '') + '.key', schemaPath: '#/properties/key/oneOf/1/type', params: { type: 'array' }, message: 'should be array' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid2 = errors === errs_2; if (valid2 && prevValid1) { valid1 = false; passingSchemas1 = [passingSchemas1, 1]; } else { if (valid2) { valid1 = prevValid1 = true; passingSchemas1 = 1; } } if (!valid1) { var err = { keyword: 'oneOf', dataPath: (dataPath || '') + '.key', schemaPath: '#/properties/key/oneOf', params: { passingSchemas: passingSchemas1 }, message: 'should match exactly one schema in oneOf' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; validate.errors = vErrors; return false; } else { errors = errs__1; if (vErrors !== null) { if (errs__1) vErrors.length = errs__1; else vErrors = null; } } var valid1 = errors === errs_1; } if (valid1) { var data1 = data.type; if (data1 === undefined) { valid1 = false; validate.errors = [{ keyword: 'required', dataPath: (dataPath || '') + "", schemaPath: '#/required', params: { missingProperty: 'type' }, message: 'should have required property \'type\'' }]; return false; } else { var errs_1 = errors; if (typeof data1 !== "string") { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.type', schemaPath: '#/properties/type/type', params: { type: 'string' }, message: 'should be string' }]; return false; } var schema1 = validate.schema.properties.type.enum; var valid1; valid1 = false; for (var i1 = 0; i1 < schema1.length; i1++) if (equal(data1, schema1[i1])) { valid1 = true; break; } if (!valid1) { validate.errors = [{ keyword: 'enum', dataPath: (dataPath || '') + '.type', schemaPath: '#/properties/type/enum', params: { allowedValues: schema1 }, message: 'should be equal to one of the allowed values' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { if (data.label === undefined) { valid1 = true; } else { var errs_1 = errors; if (typeof data.label !== "string") { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.label', schemaPath: '#/properties/label/type', params: { type: 'string' }, message: 'should be string' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { if (data.readonly === undefined) { valid1 = true; } else { var errs_1 = errors; if (typeof data.readonly !== "boolean") { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.readonly', schemaPath: '#/properties/readonly/type', params: { type: 'boolean' }, message: 'should be boolean' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { var data1 = data.appearance; if (data1 === undefined) { valid1 = true; } else { var errs_1 = errors; if (typeof data1 !== "string") { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.appearance', schemaPath: '#/properties/appearance/type', params: { type: 'string' }, message: 'should be string' }]; return false; } var schema1 = validate.schema.properties.appearance.enum; var valid1; valid1 = false; for (var i1 = 0; i1 < schema1.length; i1++) if (equal(data1, schema1[i1])) { valid1 = true; break; } if (!valid1) { validate.errors = [{ keyword: 'enum', dataPath: (dataPath || '') + '.appearance', schemaPath: '#/properties/appearance/enum', params: { allowedValues: schema1 }, message: 'should be equal to one of the allowed values' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { if (data.snake_case === undefined) { valid1 = true; } else { var errs_1 = errors; if (typeof data.snake_case !== "boolean") { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.snake_case', schemaPath: '#/properties/snake_case/type', params: { type: 'boolean' }, message: 'should be boolean' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { var data1 = data.options; if (data1 === undefined) { valid1 = true; } else { var errs_1 = errors; if (Array.isArray(data1)) { var errs__1 = errors; var valid1; for (var i1 = 0; i1 < data1.length; i1++) { var data2 = data1[i1]; var errs_2 = errors; var errs__2 = errors; var valid2 = false; var errs_3 = errors; if (typeof data2 !== "string") { var err = { keyword: 'type', dataPath: (dataPath || '') + '.options[' + i1 + ']', schemaPath: '#/properties/options/items/anyOf/0/type', params: { type: 'string' }, message: 'should be string' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid3 = errors === errs_3; valid2 = valid2 || valid3; if (!valid2) { var errs_3 = errors; if (typeof data2 !== "boolean") { var err = { keyword: 'type', dataPath: (dataPath || '') + '.options[' + i1 + ']', schemaPath: '#/properties/options/items/anyOf/1/type', params: { type: 'boolean' }, message: 'should be boolean' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid3 = errors === errs_3; valid2 = valid2 || valid3; if (!valid2) { var errs_3 = errors; if (typeof data2 !== "number") { var err = { keyword: 'type', dataPath: (dataPath || '') + '.options[' + i1 + ']', schemaPath: '#/properties/options/items/anyOf/2/type', params: { type: 'number' }, message: 'should be number' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid3 = errors === errs_3; valid2 = valid2 || valid3; if (!valid2) { var errs_3 = errors; if (data2 !== null) { var err = { keyword: 'type', dataPath: (dataPath || '') + '.options[' + i1 + ']', schemaPath: '#/properties/options/items/anyOf/3/type', params: { type: 'null' }, message: 'should be null' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid3 = errors === errs_3; valid2 = valid2 || valid3; if (!valid2) { var errs_3 = errors; if ((data2 && typeof data2 === "object" && !Array.isArray(data2))) { if (true) { var errs__3 = errors; var valid4 = true; if (data2.label === undefined) { valid4 = true; } else { var errs_4 = errors; if (typeof data2.label !== "string") { var err = { keyword: 'type', dataPath: (dataPath || '') + '.options[' + i1 + '].label', schemaPath: '#/properties/options/items/anyOf/4/properties/label/type', params: { type: 'string' }, message: 'should be string' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid4 = errors === errs_4; } if (valid4) { var data3 = data2.value; if (data3 === undefined) { valid4 = false; var err = { keyword: 'required', dataPath: (dataPath || '') + '.options[' + i1 + ']', schemaPath: '#/properties/options/items/anyOf/4/required', params: { missingProperty: 'value' }, message: 'should have required property \'value\'' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else { var errs_4 = errors; var errs__4 = errors; var valid4 = false; var errs_5 = errors; if (typeof data3 !== "string") { var err = { keyword: 'type', dataPath: (dataPath || '') + '.options[' + i1 + '].value', schemaPath: '#/properties/options/items/anyOf/4/properties/value/anyOf/0/type', params: { type: 'string' }, message: 'should be string' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid5 = errors === errs_5; valid4 = valid4 || valid5; if (!valid4) { var errs_5 = errors; if (typeof data3 !== "boolean") { var err = { keyword: 'type', dataPath: (dataPath || '') + '.options[' + i1 + '].value', schemaPath: '#/properties/options/items/anyOf/4/properties/value/anyOf/1/type', params: { type: 'boolean' }, message: 'should be boolean' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid5 = errors === errs_5; valid4 = valid4 || valid5; if (!valid4) { var errs_5 = errors; if (typeof data3 !== "number") { var err = { keyword: 'type', dataPath: (dataPath || '') + '.options[' + i1 + '].value', schemaPath: '#/properties/options/items/anyOf/4/properties/value/anyOf/2/type', params: { type: 'number' }, message: 'should be number' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid5 = errors === errs_5; valid4 = valid4 || valid5; if (!valid4) { var errs_5 = errors; if (data3 !== null) { var err = { keyword: 'type', dataPath: (dataPath || '') + '.options[' + i1 + '].value', schemaPath: '#/properties/options/items/anyOf/4/properties/value/anyOf/3/type', params: { type: 'null' }, message: 'should be null' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid5 = errors === errs_5; valid4 = valid4 || valid5; } } } if (!valid4) { var err = { keyword: 'anyOf', dataPath: (dataPath || '') + '.options[' + i1 + '].value', schemaPath: '#/properties/options/items/anyOf/4/properties/value/anyOf', params: {}, message: 'should match some schema in anyOf' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else { errors = errs__4; if (vErrors !== null) { if (errs__4) vErrors.length = errs__4; else vErrors = null; } } var valid4 = errors === errs_4; } } } } else { var err = { keyword: 'type', dataPath: (dataPath || '') + '.options[' + i1 + ']', schemaPath: '#/properties/options/items/anyOf/4/type', params: { type: 'object' }, message: 'should be object' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } var valid3 = errors === errs_3; valid2 = valid2 || valid3; } } } } if (!valid2) { var err = { keyword: 'anyOf', dataPath: (dataPath || '') + '.options[' + i1 + ']', schemaPath: '#/properties/options/items/anyOf', params: {}, message: 'should match some schema in anyOf' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; validate.errors = vErrors; return false; } else { errors = errs__2; if (vErrors !== null) { if (errs__2) vErrors.length = errs__2; else vErrors = null; } } var valid2 = errors === errs_2; if (!valid2) break; } } else { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.options', schemaPath: '#/properties/options/type', params: { type: 'array' }, message: 'should be array' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { if (data.universal === undefined) { valid1 = true; } else { var errs_1 = errors; if (typeof data.universal !== "boolean") { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.universal', schemaPath: '#/properties/universal/type', params: { type: 'boolean' }, message: 'should be boolean' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { if (data.placeholder === undefined) { valid1 = true; } else { var errs_1 = errors; if (typeof data.placeholder !== "string") { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.placeholder', schemaPath: '#/properties/placeholder/type', params: { type: 'string' }, message: 'should be string' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { if (data.helperText === undefined) { valid1 = true; } else { var errs_1 = errors; if (typeof data.helperText !== "string") { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.helperText', schemaPath: '#/properties/helperText/type', params: { type: 'string' }, message: 'should be string' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { var data1 = data.min_value; if (data1 === undefined) { valid1 = true; } else { var errs_1 = errors; if ((typeof data1 !== "number" || (data1 % 1) || data1 !== data1)) { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.min_value', schemaPath: '#/properties/min_value/type', params: { type: 'integer' }, message: 'should be integer' }]; return false; } var valid1 = errors === errs_1; } if (valid1) { var data1 = data.max_value; if (data1 === undefined) { valid1 = true; } else { var errs_1 = errors; if ((typeof data1 !== "number" || (data1 % 1) || data1 !== data1)) { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + '.max_value', schemaPath: '#/properties/max_value/type', params: { type: 'integer' }, message: 'should be integer' }]; return false; } var valid1 = errors === errs_1; } } } } } } } } } } } } } } } } else { validate.errors = [{ keyword: 'type', dataPath: (dataPath || '') + "", schemaPath: '#/type', params: { type: 'object' }, message: 'should be object' }]; return false; } validate.errors = vErrors; return errors === 0; }; })(); validate.schema = { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "http://mapeo.world/schemas/field.json", "title": "Field", "description": "A field defines a form field that will be shown to the user when creating or editing a map entity. Presets define which fields are shown to the user for a particular map entity. The field definition defines whether the field should show as a text box, multiple choice, single-select, etc. It defines what tag-value is set when the field is entered.", "type": "object", "properties": { "id": { "description": "Unique value that identifies this element", "type": "string" }, "key": { "description": "They key in a tags object that this field applies to. For nested properties, key can be an array e.g. for tags = `{ foo: { bar: 1 } }` the key is `['foo', 'bar']`", "oneOf": [{ "type": "string" }, { "type": "array", "items": { "type": "string" } }] }, "type": { "description": "Type of field - defines how the field is displayed to the user.", "type": "string", "meta:enum": { "text": "Freeform text field", "localized": "Text field with localization abilities (e.g. name=*, name:es=*, etc.). Currently only supported in Mapeo Desktop territory view.", "number": "Allows only numbers", "select_one": "Select one item from a list of pre-defined options", "select_multiple": "Select any number of items from a list of pre-defined options", "date": "Select a date", "datetime": "Select a date and time" }, "enum": ["text", "localized", "number", "select_one", "select_multiple", "date", "datetime"] }, "label": { "description": "Default language label for the form field label", "type": "string" }, "readonly": { "description": "Field is displayed, but it can't be edited", "type": "boolean", "default": false }, "appearance": { "description": "For text fields, display as a single-line or multi-line field", "type": "string", "meta:enum": { "singleline": "Text will be cut-off if more than one line", "multiline": "Text will wrap to multiple lines within text field" }, "enum": ["singleline", "multiline"], "default": "multiline" }, "snake_case": { "description": "Convert field value into snake_case (replace spaces with underscores and convert to lowercase)", "type": "boolean", "default": false }, "options": { "description": "List of options the user can select for single- or multi-select fields", "type": "array", "items": { "anyOf": [{ "type": "string" }, { "type": "boolean" }, { "type": "number" }, { "type": "null" }, { "type": "object", "properties": { "label": { "description": "Label in default language to display to the user for this option", "type": "string" }, "value": { "description": "Value for tag when this option is selected", "anyOf": [{ "type": "string" }, { "type": "boolean" }, { "type": "number" }, { "type": "null" }] } }, "required": ["value"] }] } }, "universal": { "description": "If true, this field will appear in the Add Field list for all presets", "type": "boolean", "default": false }, "placeholder": { "description": "Displayed as a placeholder in an empty text or number field before the user begins typing. Use 'helperText' for important information, because the placeholder is not visible after the user has entered data.", "type": "string" }, "helperText": { "description": "Additional context about the field, e.g. hints about how to answer the question.", "type": "string" }, "min_value": { "description": "Minimum field value (number, date or datetime fields only). For date or datetime fields, is seconds since unix epoch", "type": "integer" }, "max_value": { "description": "Maximum field value (number, date or datetime fields only). For date or datetime fields, is seconds since unix epoch", "type": "integer" } }, "required": ["id", "key", "type"], "additionalProperties": false }; validate.errors = null; module.exports = validate;