Skip to content

Commit

Permalink
dist: update
Browse files Browse the repository at this point in the history
  • Loading branch information
sagold committed Jun 6, 2024
1 parent 1096736 commit 6a46972
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 40 deletions.
2 changes: 1 addition & 1 deletion dist/jsonSchemaLibrary.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/draft/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export declare class Draft {
*/
each(data: any, callback: EachCallback, schema?: JsonSchema, pointer?: JsonPointer): void;
eachSchema(callback: EachSchemaCallback, schema?: JsonSchema): void;
getChildSchemaSelection(property: string | number, schema?: JsonSchema): JsonError | JsonSchema[];
getChildSchemaSelection(property: string | number, schema?: JsonSchema): JsonSchema[] | JsonError;
/**
* Returns the json-schema of a data-json-pointer.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/module/lib/draft2019/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const draft2019Config = {
"additionalProperties",
"allOf",
"anyOf",
"dependencies",
// "dependencies",
"dependentSchemas",
"dependentRequired",
"enum",
Expand Down
30 changes: 18 additions & 12 deletions dist/module/lib/draft2019/validation/keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { validateDependentSchemas, validateDependentRequired } from "../../featu
/**
* Get a list of tests to search for a matching pattern to a property
*/
const getPatternTests = (patternProperties) => isObject(patternProperties) ?
Object.keys(patternProperties).map((pattern) => new RegExp(pattern))
const getPatternTests = (patternProperties) => isObject(patternProperties)
? Object.keys(patternProperties).map((pattern) => new RegExp(pattern))
: [];
/** tests if a property is evaluated by the given schema */
function isPropertyEvaluated(schemaNode, propertyName, value) {
Expand All @@ -25,7 +25,7 @@ function isPropertyEvaluated(schemaNode, propertyName, value) {
}
// PATTERN-PROPERTIES
const patterns = getPatternTests(schema.patternProperties);
if (patterns.find(pattern => pattern.test(propertyName))) {
if (patterns.find((pattern) => pattern.test(propertyName))) {
return true;
}
// ADDITIONAL-PROPERTIES
Expand All @@ -36,6 +36,7 @@ function isPropertyEvaluated(schemaNode, propertyName, value) {
return false;
}
const KeywordValidation = {
// ...omit(Keywords, "dependencies"),
...Keywords,
dependencies: undefined,
dependentSchemas: validateDependentSchemas,
Expand Down Expand Up @@ -65,17 +66,18 @@ const KeywordValidation = {
return undefined;
}
const testPatterns = getPatternTests(resolvedSchema.patternProperties);
unevaluated = unevaluated.filter(key => {
unevaluated = unevaluated.filter((key) => {
var _a;
if ((_a = resolvedSchema.properties) === null || _a === void 0 ? void 0 : _a[key]) {
return false;
}
// special case: an evaluation in if statement counts too
// we have an unevaluated prop only if the if-schema does not match
if (isObject(schema.if) && isPropertyEvaluated(node.next({ type: "object", ...schema.if }), key, value[key])) {
if (isObject(schema.if) &&
isPropertyEvaluated(node.next({ type: "object", ...schema.if }), key, value[key])) {
return false;
}
if (testPatterns.find(pattern => pattern.test(key))) {
if (testPatterns.find((pattern) => pattern.test(key))) {
return false;
}
// @todo is this evaluated by additionaProperties per property
Expand All @@ -89,7 +91,7 @@ const KeywordValidation = {
}
const errors = [];
if (resolvedSchema.unevaluatedProperties === false) {
unevaluated.forEach(key => {
unevaluated.forEach((key) => {
errors.push(draft.errors.unevaluatedPropertyError({
pointer: `${pointer}/${key}`,
value: JSON.stringify(value[key]),
Expand All @@ -98,7 +100,7 @@ const KeywordValidation = {
});
return errors;
}
unevaluated.forEach(key => {
unevaluated.forEach((key) => {
if (isObject(resolvedSchema.unevaluatedProperties)) {
// note: only key changes
const keyErrors = draft.validate(node.next(resolvedSchema.unevaluatedProperties, key), value[key]);
Expand All @@ -116,7 +118,10 @@ const KeywordValidation = {
var _a;
const { draft, schema, pointer } = node;
// if not in items, and not matches additionalItems
if (!Array.isArray(value) || value.length === 0 || schema.unevaluatedItems == null || schema.unevaluatedItems === true) {
if (!Array.isArray(value) ||
value.length === 0 ||
schema.unevaluatedItems == null ||
schema.unevaluatedItems === true) {
return undefined;
}
// resolve all dynamic schemas
Expand All @@ -129,7 +134,8 @@ const KeywordValidation = {
if (isObject(schema.if)) {
const nextSchemaNode = { type: "array", ...schema.if };
if (draft.isValid(value, nextSchemaNode)) {
if (Array.isArray(nextSchemaNode.items) && nextSchemaNode.items.length === value.length) {
if (Array.isArray(nextSchemaNode.items) &&
nextSchemaNode.items.length === value.length) {
return undefined;
}
}
Expand All @@ -138,7 +144,7 @@ const KeywordValidation = {
if (isObject(resolvedSchema.items)) {
const nextSchemaNode = { ...resolvedSchema, unevaluatedItems: undefined };
const errors = draft.validate(node.next(nextSchemaNode), value);
return errors.map(e => draft.errors.unevaluatedItemsError({ ...e.data }));
return errors.map((e) => draft.errors.unevaluatedItemsError({ ...e.data }));
}
if (Array.isArray(resolvedSchema.items)) {
const items = [];
Expand All @@ -152,7 +158,7 @@ const KeywordValidation = {
items.push({ index: i, value: value[i] });
}
}
return items.map(item => draft.errors.unevaluatedItemsError({
return items.map((item) => draft.errors.unevaluatedItemsError({
pointer: `${pointer}/${item.index}`,
value: JSON.stringify(item.value),
schema: resolvedSchema.unevaluatedItems
Expand Down
74 changes: 49 additions & 25 deletions dist/module/lib/validation/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const isValidIPV6 = /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){
const isValidHostname = /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/;
const matchDate = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
// const matchTime = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d(?::?\d\d)?)?$/i;
const matchTime = /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i;
const matchTime = /^(?<time>(?:([0-1]\d|2[0-3]):[0-5]\d:(?<second>[0-5]\d|60)))(?:\.\d+)?(?<offset>(?:z|[+-]([0-1]\d|2[0-3])(?::?[0-5]\d)?))$/i;
const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const isValidJsonPointer = /^(?:\/(?:[^~/]|~0|~1)*)*$/;
const isValidRelativeJsonPointer = /^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)$/;
Expand Down Expand Up @@ -49,11 +49,13 @@ const formatValidators = {
if (typeof value !== "string" || value === "") {
return undefined;
}
if (value === "" || isValidDateTime.test(value)) {
if (new Date(value).toString() === "Invalid Date") {
return draft.errors.formatDateTimeError({ value, pointer, schema });
const dateAndTime = value.split(/t/i);
if (dateAndTime.length === 2) {
const dateIsValid = formatValidators.date(node, dateAndTime[0]) === undefined;
const timeIsValid = formatValidators.time(node, dateAndTime[1]) === undefined;
if (dateIsValid && timeIsValid) {
return undefined;
}
return undefined;
}
return draft.errors.formatDateTimeError({ value, pointer, schema });
},
Expand All @@ -64,8 +66,13 @@ const formatValidators = {
}
// weeks cannot be combined with other units
const isInvalidDurationString = /(\d+M)(\d+W)|(\d+Y)(\d+W)/;
if (!isValidDurationString.test(value) || isInvalidDurationString.test(value)) {
return node.draft.errors.formatDurationError({ value, pointer: node.pointer, schema: node.schema });
if (!isValidDurationString.test(value) ||
isInvalidDurationString.test(value)) {
return node.draft.errors.formatDurationError({
value,
pointer: node.pointer,
schema: node.schema
});
}
},
email: (node, value) => {
Expand Down Expand Up @@ -184,31 +191,48 @@ const formatValidators = {
return draft.errors.formatRegExError({ value, pointer, schema });
},
// hh:mm:ss.sTZD
// https://opis.io/json-schema/2.x/formats.html
// regex https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s07.html
// RFC 3339 https://datatracker.ietf.org/doc/html/rfc3339#section-4
time: (node, value) => {
const { draft, schema, pointer } = node;
if (typeof value !== "string" || value === "") {
return undefined;
}
// https://github.com/cfworker/cfworker/blob/main/packages/json-schema/src/format.ts
const matches = value.match(matchTime);
return matches ? undefined : draft.errors.formatDateTimeError({ value, pointer, schema });
// if (!matches) {
// return errors.formatDateTimeError({ value, pointer, schema });
// }
// const hour = +matches[1];
// const minute = +matches[2];
// const second = +matches[3];
// const timeZone = !!matches[5];
// if (
// ((hour <= 23 && minute <= 59 && second <= 59) ||
// (hour == 23 && minute == 59 && second == 60)) &&
// timeZone
// ) {
// return undefined;
// }
// return errors.formatTimeError({ value, pointer, schema });
if (!matches) {
return draft.errors.formatDateTimeError({ value, pointer, schema });
}
// leap second
if (matches.groups.second === "60") {
// bail early
if (/23:59:60(z|\+00:00)/i.test(value)) {
return undefined;
}
// check if sum matches 23:59
const minutes = matches.groups.time.match(/(\d+):(\d+):/);
const offsetMinutes = matches.groups.offset.match(/(\d+):(\d+)/);
if (offsetMinutes) {
const hour = parseInt(minutes[1]);
const offsetHour = parseInt(offsetMinutes[1]);
const min = parseInt(minutes[2]);
const offsetMin = parseInt(offsetMinutes[2]);
let deltaTime;
if (/^-/.test(matches.groups.offset)) {
deltaTime = (hour + offsetHour) * 60 + (min + offsetMin);
}
else {
deltaTime = (24 + hour - offsetHour) * 60 + (min - offsetMin);
}
const hours = Math.floor(deltaTime / 60);
const actualHour = hours % 24;
const actualMinutes = deltaTime - hours * 60;
if (actualHour === 23 && actualMinutes === 59) {
return undefined;
}
}
return draft.errors.formatDateTimeError({ value, pointer, schema });
}
return undefined;
},
uri: (node, value) => {
const { draft, schema, pointer } = node;
Expand Down

0 comments on commit 6a46972

Please sign in to comment.