Giter Club home page Giter Club logo

ajv-moment's People

Contributors

bfeister avatar cludden avatar kristianmandrup avatar lamuertepeluda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ajv-moment's Issues

Validate returns invalid `dataPath`

Given the following code:

const VALID_SHIFT_ITEM = {
  required: ['totalCapacity', 'start', 'end'],
  properties: {
    totalCapacity: {
      type: 'integer',
      minimum: 1,
      maximum: OPPORTUNITY_SHIFT_MAX_CAPACITY,
    },
    remainingCapacity: {
      type: 'integer',
      minimum: 0,
      maximum: { $data: '1/totalCapacity' },
    },
    start: {
      required: ['localDateTime', 'timeZone'],
      properties: {
        localDateTime: {
          localDateTime: true,
          moment: {
            "validate": [
              {
                "test": "isAfter",
                "value": {
                  "$data": "2/end/localDateTime"
                }
              }
            ]
          }
        },
        timeZone: { type: 'string', minLength: 1 },
      },
    },
    end: {
      required: ['localDateTime', 'timeZone'],
      properties: {
        localDateTime: {
          localDateTime: true,
        },
        timeZone: { type: 'string', minLength: 1 },
      },
    },
  }
}

I get the following error returned from validate()

image

Note that the value is located at the schema root and says localDateTime' when it should actually be localDateTime (sans apostrophe) nested like this: shifts/items/start/localDateTime.

A deeper look reveals that the error messages yielded by your plugin is as follows:

image

Note the double-encoded value wrapped in apostrophes.

I believe the main AJV library is now storing this at it.errorPath

When defining my own inline validation, I find it.errorPath returns the value that I believe is needed here

image

however the code is so difficult to follow, I can't find where this is being declared in the pre-compilation code in order to produce the compiled result found in the ajv-keywords library:

https://github.com/epoberezkin/ajv-keywords/blob/master/keywords/dot/_formatLimit.jst

Unable to set moment date format when using validate

Unable to set custom date format on moment.
Line 139 uses either the 'format' field (if present) or moment.ISO_8601 as the date format.

It is currently not possible to set the format field.
Current Line 82: const _val = { now: val.now, $data: val.$data, manipulate: val.manipulate };
The format field is not set/ populated.

I believe this is a defect.

There are several ways to correct this. I have included 2 below.

Proposed change 1
Schema
{
moment: {
"validate": [{
"test": "isBefore",
"value": {
"data": "1/due",
"format": [
"HH:mm:ss"
]
}
}]
}
}
ajv-moment
const _val = { now: val.now, $data: val.$data, manipulate: val.manipulate, format: val.format };

Proposed change 2
Schema:
{
moment: {
"format": [
"HH:mm:ss"
],
"validate": [{
"test": "isBefore",
"value": {
"data": "1/due"
}
}]
}
}

ajv-moment
const _val = { now: val.now, $data: val.$data, manipulate: val.manipulate, format: formats };

Is it possible to have this investigated?

Rob

Attach moment to ajv instance?

It may be a nicer solution than passing via context - from version 4.6.0 all custom keywords (but "validate") receive the third parameter - schema compilation context that has reference to ajv instance it.self. In this way if the moment is attached to the instance, you can access it from custom keywords.

Or you could make moment a dependency of this package (there are pros and cons of course...)

{ajv,moment} isn't parseable by eslint

Hi,

I love ajv-moment. However, we're using eslint now and the way its setup to parse the options isn't parseable as valid js. Objects have fields: values, while you're asking for an object that looks like an array.

Could you change it so that it's called like plugin({ ajv: ajv, moment: moment)? or just plugin(ajv, moment)?

Thanks. I'd prefer to use a blessed version rather than forking.

David

Broken in NPM

I think when you pushed the release tag after #8 you forgot to run postrelease or something like that? yarn add gives me an ajv-moment folder that lacks an dist/index.js

Constant value never working

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

moment date value is not validating

Hi,
here is my code :

 `moment: {
            "format": ["YYYY-MM-DD HH:mm"],
            "validate": [
                {
                    "test": "isSameOrAfter",
                    "value": moment().format("YYYY-MM-DD HH:mm")
                }
            ]
    }`

But the date is not validating by the plugin and always getting same error :
{ "reason": "\"isSameOrAfter\" validation failed for value(s): null" }

any suggestions.
thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.