cludden / ajv-moment Goto Github PK
View Code? Open in Web Editor NEWrobust date validation for json-schema
License: MIT License
robust date validation for json-schema
License: MIT License
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()
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:
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
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 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
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...)
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
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
Hello I was following the examples to validate against a date range
{
"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"
]
}
}
}
{
year: '2010'
}
data/year "isAfter" validation failed for value(s): null
"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
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.