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

Constant value never working #13

Closed
lamuertepeluda opened this issue Feb 5, 2020 · 0 comments
Closed

Constant value never working #13

lamuertepeluda opened this issue Feb 5, 2020 · 0 comments

Comments

@lamuertepeluda
Copy link
Contributor

lamuertepeluda commented Feb 5, 2020

Hello I was following the examples to validate against a date range

Schema

{
    "definitions": {},
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "https://some-url/mini-schema.json",
    "type": "object",
    "title": "Mini Schema",
    "required": [
        "year"
    ],
    "additionalProperties": false,
    "properties": {
        "year": {
            "$id": "#/properties/year",
            "type": "string",
            "title": "Year (4 digits)",
            "description": "a valid year string in YYYY format",
            "default": "",
            "moment": {
                "format": [
                    "YYYY"
                ],
                "validate": [
                    {
                        "test": "isSameOrAfter",
                        "value": "1900-01-01T00:00:00.000Z"
                    },
                    {
                        "test": "isSameOrBefore",
                        "value": "2999-01-01T00:00:00.000Z"
                    }
                ]
            },
            "examples": [
                "2020"
            ]
        }
    }
}

Data

{
    year: '2010'
}

Error text

data/year "isAfter" validation failed for value(s): null

Library Versions

    "ajv": "^6.10.2",
    "ajv-keywords": "^3.4.1",
    "ajv-moment": "^2.0.3",
    "moment": "^2.24.0",
    "moment-timezone": "^0.5.27"

I think the problem is at this line of code https://github.com/cludden/ajv-moment/blob/master/lib/index.js#L82

const _val = { now: val.now, $data: val.$data, manipulate: val.manipulate };
            if (_val.now !== true && typeof _val.$data === 'string') {
                _val.value = it.util.getData(_val.$data, it.dataLevel, it.dataPathArr);
            }

_val.value is always undefined when "value" is a constant like in my example

Should be

const _val = { now: val.now, $data: val.$data, manipulate: val.manipulate };
 if (typeof val === 'string'){
    _val.value = JSON.stringify(val);
 }
else  if (_val.now !== true && typeof _val.$data === 'string') {
   _val.value = it.util.getData(_val.$data, it.dataLevel, it.dataPathArr);
}

I will try to submit a PR

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

No branches or pull requests

1 participant