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

Add resolveRefs option or method #454

Closed
onury opened this issue Apr 4, 2017 · 6 comments
Closed

Add resolveRefs option or method #454

onury opened this issue Apr 4, 2017 · 6 comments

Comments

@onury
Copy link

onury commented Apr 4, 2017

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

v4.11.5

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

const options = {
    v5: true,
    allErrors: true,
    verbose: false,
    jsonPointers: false,
    uniqueItems: true,
    unicode: true,
    format: 'full',
    formats: {},
    unknownFormats: 'ignore',
    schemas: {},
    missingRefs: true,
    extendRefs: true,
    loadSchema: undefined,
    removeAdditional: false,
    useDefaults: true, 
    coerceTypes: false,
    async: undefined,
    transpile: undefined,
    meta: true,
    validateSchema: true,
    addUsedSchema: true,
    inlineRefs: true,
    passContext: false,
    loopRequired: Infinity,
    ownProperties: false,
    multipleOfPrecision: 0.1,
    errorDataPath: 'object',
    sourceCode: true,
    messages: true,
    beautify: false
}

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

const schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "date": { "$ref": "#/definitions/DateTime" },
        "notes": { "$ref": "#/definitions/String500" }
    },
    "additionalProperties": false,
    "definitions": {
        "DateTime": {
            "type": "string",
            "format": "date-time"
        },
        "String500": {
            "type": "string",
            "maxLength": 500
        }
    }
}

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

See RunKit example.

var ajv = Ajv(options);
console.log(ajv.compile(schema).schema.properties); 

Outputs:

{
    "date": { "$ref": "#/definitions/DateTime" }, // still defined as $ref
    "notes": { "$ref": "#/definitions/String500" } // ..
}

What results did you expect?
I need $ref values to be resolved within the compiled schema. But (as noted in docs) .compile() method stores the schema as a reference without resolving references.

inlineRefs option seems to be used for inlining referenced schemas but it doesn't. extendRefs is something else either.

@epoberezkin
Copy link
Member

duplicate of #22

@epoberezkin
Copy link
Member

$ref is not equivalent to schema inclusion - the expectation you have is wrong.

You can read more on the issue in various discussions in JSON-schema-org, this is the last json-schema-org/json-schema-spec#279 (comment)

@epoberezkin
Copy link
Member

epoberezkin commented Apr 4, 2017

inlineRefs option seems to be used for inlining referenced schemas but it doesn't.

It affects code generation - with this option false $ref is ALWAYS compiled to a function call, by default the code is inlined when possible. The schema itself NEVER changes.

@epoberezkin
Copy link
Member

epoberezkin commented Apr 4, 2017

For a subset of possible JSON-schemas $ref can be seen as pre-processing instruction to include the referenced schema in place. In general case such pre-processing is NOT possible.

Performing any schema manipulation, including inlining $refs in place, is out of scope of this package. The scope of this package is validating data against given schema (specifically, generating code to validate data).

@onury
Copy link
Author

onury commented Apr 5, 2017

Fair enough. Thanks.

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