Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected identifier with export union type #311

Closed
cristianmtr opened this issue Sep 28, 2016 · 5 comments
Closed

Unexpected identifier with export union type #311

cristianmtr opened this issue Sep 28, 2016 · 5 comments
Labels

Comments

@cristianmtr
Copy link

What version of Ajv are you using? Does the issue happen if you use the latest version?

4.7.4

Ajv options object (see https://github.com/epoberezkin/ajv#options):

None, default.

JSON Schema (please make it as small as possible to reproduce the issue):

{
    "type": "object",
    "properties": {
        "field": {
            "$ref": "#/definitions/\"main\".IPaneType"
        }
    },
    "definitions": {
        "\"main\".IPaneType": {
            "type": "string",
            "enum": [
                "filter",
                "card",
                "table",
                "filter-as-table",
                "search-filter"
            ]
        }
    },
    "$schema": "http://json-schema.org/draft-04/schema#"
}

Data (please make it as small as posssible to reproduce the issue):

{
    "field": "filter"
}

Your code (please use options, schema and data as variables):

var fs = require("fs");
var schema = JSON.parse(fs.readFileSync("schema1.json",'utf-8'));
var data = JSON.parse(fs.readFileSync("sample1.json",'utf-8'));

// AJV

var ajvM = require("ajv");
var options = {}
var ajv = new ajvM(options);
var vl = ajv.compile(schema); // will fail
console.log(vl(data));
console.log(vl.errors);

Validation result, data AFTER validation, error messages:

Error compiling schema, function code: var refVal1 = refVal[1]; var validate =  (function  (data, dataPath, parentData, parentDataProperty, rootData) { 'use strict'; var vErrors = null;  var errors = 0;      if (rootData === undefined) rootData = data; if ((data && typeof data === "object" && !Array.isArray(data))) {   var errs__0 = errors;var valid1 = true; var data1 = data.field;  if (data1 === undefined) { valid1 = true; } else {   var errs_1 = errors;    var errs_2 = errors;   var schema2 = refVal1.enum;var valid2;valid2 = false;for (var i2=0; i2<schema2.length; i2++) if (equal(data1, schema2[i2])) { valid2 = true; break; } if (!valid2) {    validate.errors = [ { keyword: 'enum' , dataPath: (dataPath || '') + '.field' , schemaPath: "#/definitions/"main".IPaneType/enum" , params: { allowedValues: schema2 }  , message: 'should be equal to one of the allowed values'  } ]; return false;  }
 if (errors === errs_2) {  if (typeof data1 !== "string") {    validate.errors = [ { keyword: 'type' , dataPath: (dataPath || '') + '.field' , schemaPath: "#/definitions/"main".IPaneType/type" , params: { type: 'string' }  , message: 'should be string'  } ]; return false;  } }  var valid2 = errors === errs_2;     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;        }); return validate;return validate;
C:\TMP\typescript-json-schema\node_modules\ajv\lib\compile\index.js:162
      throw e;
      ^

SyntaxError: Unexpected identifier
    at localCompile (C:\TMP\typescript-json-schema\node_modules\ajv\lib\compile\index.js:130:26)
    at Ajv.compile (C:\TMP\typescript-json-schema\node_modules\ajv\lib\compile\index.js:59:13)
    at _compile (C:\TMP\typescript-json-schema\node_modules\ajv\lib\ajv.js:325:29)
    at Ajv.compile (C:\TMP\typescript-json-schema\node_modules\ajv\lib\ajv.js:111:34)
    at Object.<anonymous> (C:\TMP\typescript-json-schema\export-type-test\validate.js:10:14)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)

What results did you expect?

To compile the schema and validate.

Are you going to resolve the issue?

Idk, might use another validation module. Depends on my time.

@cristianmtr
Copy link
Author

It seems to stem from the fact that our schema contains a double quote in a definition. This is due to the tool we use to generate schema (https://github.com/YousefED/typescript-json-schema. But ajv should handle this, since other tools seem to work. But we don't want to switch to another tool for the validation, since we've spent a lot of time making sure ajv works with our pretty complex schema (not included here).

@epoberezkin
Copy link
Member

https://runkit.com/esp/57ebc100d3420b13000ea8a8

Yes, good catch.

@cristianmtr
Copy link
Author

cristianmtr commented Sep 28, 2016

Error: Cannot find module '../dotjs/validate'
    at Function.Module._resolveFilename (module.js:440:15)
    at Function.Module._load (module.js:388:25)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (CLIPPED\src\node_modules\ajv\lib\compile\index.js:10:25)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)

Anything I'm missing?

@epoberezkin
Copy link
Member

are you trying to use from github? just do npm install. it's in 4.7.5

@epoberezkin
Copy link
Member

or npm update (just published)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants