-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
336 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export function stringHints( | ||
schema: | ||
| (import('json-schema').JSONSchema4 & import('../validate').Extend) | ||
| (import('json-schema').JSONSchema6 & import('../validate').Extend) | ||
| (import('json-schema').JSONSchema7 & import('../validate').Extend), | ||
logic: boolean | ||
): string[]; | ||
export function numberHints( | ||
schema: | ||
| (import('json-schema').JSONSchema4 & import('../validate').Extend) | ||
| (import('json-schema').JSONSchema6 & import('../validate').Extend) | ||
| (import('json-schema').JSONSchema7 & import('../validate').Extend), | ||
logic: boolean | ||
): string[]; | ||
export type Schema = | ||
| (import('json-schema').JSONSchema4 & import('../validate').Extend) | ||
| (import('json-schema').JSONSchema6 & import('../validate').Extend) | ||
| (import('json-schema').JSONSchema7 & import('../validate').Extend); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
const Range = require('./Range'); | ||
|
||
/** @typedef {import("../validate").Schema} Schema */ | ||
|
||
/** | ||
* @param {Schema} schema | ||
* @param {boolean} logic | ||
* @return {string[]} | ||
*/ | ||
module.exports.stringHints = function stringHints(schema, logic) { | ||
const hints = []; | ||
let type = 'string'; | ||
const currentSchema = { ...schema }; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
alexander-akait
Member
|
||
|
||
if (!logic) { | ||
const tmpLength = currentSchema.minLength; | ||
const tmpFormat = currentSchema.formatMinimum; | ||
const tmpExclusive = currentSchema.formatExclusiveMaximum; | ||
|
||
currentSchema.minLength = currentSchema.maxLength; | ||
currentSchema.maxLength = tmpLength; | ||
currentSchema.formatMinimum = currentSchema.formatMaximum; | ||
currentSchema.formatMaximum = tmpFormat; | ||
currentSchema.formatExclusiveMaximum = !currentSchema.formatExclusiveMinimum; | ||
currentSchema.formatExclusiveMinimum = !tmpExclusive; | ||
} | ||
|
||
if (typeof currentSchema.minLength === 'number') { | ||
if (currentSchema.minLength === 1) { | ||
type = 'non-empty string'; | ||
} else { | ||
const length = Math.max(currentSchema.minLength - 1, 0); | ||
hints.push( | ||
`should be longer than ${length} character${length > 1 ? 's' : ''}` | ||
); | ||
} | ||
} | ||
|
||
if (typeof currentSchema.maxLength === 'number') { | ||
if (currentSchema.maxLength === 0) { | ||
type = 'empty string'; | ||
} else { | ||
const length = currentSchema.maxLength + 1; | ||
hints.push( | ||
`should be shorter than ${length} character${length > 1 ? 's' : ''}` | ||
); | ||
} | ||
} | ||
|
||
if (currentSchema.pattern) { | ||
hints.push( | ||
`should${logic ? '' : ' not'} match pattern ${JSON.stringify( | ||
currentSchema.pattern | ||
)}` | ||
); | ||
} | ||
|
||
if (currentSchema.format) { | ||
hints.push( | ||
`should${logic ? '' : ' not'} match format ${JSON.stringify( | ||
currentSchema.format | ||
)}` | ||
); | ||
} | ||
|
||
if (currentSchema.formatMinimum) { | ||
hints.push( | ||
`should be ${ | ||
currentSchema.formatExclusiveMinimum ? '>' : '>=' | ||
} ${JSON.stringify(currentSchema.formatMinimum)}` | ||
); | ||
} | ||
|
||
if (currentSchema.formatMaximum) { | ||
hints.push( | ||
`should be ${ | ||
currentSchema.formatExclusiveMaximum ? '<' : '<=' | ||
} ${JSON.stringify(currentSchema.formatMaximum)}` | ||
); | ||
} | ||
|
||
return [type].concat(hints); | ||
}; | ||
|
||
/** | ||
* @param {Schema} schema | ||
* @param {boolean} logic | ||
* @return {string[]} | ||
*/ | ||
module.exports.numberHints = function numberHints(schema, logic) { | ||
const hints = [schema.type === 'integer' ? 'integer' : 'number']; | ||
const range = new Range(); | ||
|
||
if (typeof schema.minimum === 'number') { | ||
range.left(schema.minimum); | ||
} | ||
|
||
if (typeof schema.exclusiveMinimum === 'number') { | ||
range.left(schema.exclusiveMinimum, true); | ||
} | ||
|
||
if (typeof schema.maximum === 'number') { | ||
range.right(schema.maximum); | ||
} | ||
|
||
if (typeof schema.exclusiveMaximum === 'number') { | ||
range.right(schema.exclusiveMaximum, true); | ||
} | ||
|
||
const rangeFormat = range.format(logic); | ||
|
||
if (rangeFormat) { | ||
hints.push(rangeFormat); | ||
} | ||
|
||
if (typeof schema.multipleOf === 'number') { | ||
hints.push( | ||
`should${logic ? '' : ' not'} be multiple of ${schema.multipleOf}` | ||
); | ||
} | ||
|
||
return hints; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
has error on webpack build:
Module build failed (from ./node_modules/babel-loader/lib/index.js):
/Users/andrew/Projects/.....com/app/Resources/frontend/webpack/node_modules/schema-utils/dist/util/hints.js:16
const currentSchema = {...schema};
^^^
SyntaxError: Unexpected token ...