feat(json-schema-2020-12): add support for string validation keywords (#8625)
Includes following keywords: - minLength - maxLength - pattern Refs #8513
This commit is contained in:
@@ -3,14 +3,27 @@
|
|||||||
*/
|
*/
|
||||||
import React from "react"
|
import React from "react"
|
||||||
import PropTypes from "prop-types"
|
import PropTypes from "prop-types"
|
||||||
|
import classNames from "classnames"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This component represents various constraint keywords
|
* This component represents various constraint keywords
|
||||||
* from JSON Schema 2020-12 validation vocabulary.
|
* from JSON Schema 2020-12 validation vocabulary.
|
||||||
*/
|
*/
|
||||||
const Constraint = ({ constraint }) => (
|
const Constraint = ({ constraint }) => {
|
||||||
<span className="json-schema-2020-12__constraint">{constraint}</span>
|
const isPattern = /^matches /.test(constraint)
|
||||||
)
|
const isStringRange = /characters/.test(constraint)
|
||||||
|
const isStringRelated = isPattern || isStringRange
|
||||||
|
|
||||||
|
return (
|
||||||
|
<span
|
||||||
|
className={classNames("json-schema-2020-12__constraint", {
|
||||||
|
"json-schema-2020-12__constraint--string-related": isStringRelated,
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
{constraint}
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
Constraint.propTypes = {
|
Constraint.propTypes = {
|
||||||
constraint: PropTypes.string.isRequired,
|
constraint: PropTypes.string.isRequired,
|
||||||
|
|||||||
@@ -6,4 +6,9 @@
|
|||||||
color: white;
|
color: white;
|
||||||
background-color: #805AD5;
|
background-color: #805AD5;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
|
|
||||||
|
&--string-related {
|
||||||
|
color: white;
|
||||||
|
background-color: #D69E2E;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -231,15 +231,44 @@ const stringifyConstraintNumberRange = (schema) => {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const stringifyConstraintRange = (label, min, max) => {
|
||||||
|
const hasMin = typeof min === "number"
|
||||||
|
const hasMax = typeof max === "number"
|
||||||
|
|
||||||
|
if (hasMin && hasMax) {
|
||||||
|
if (min === max) {
|
||||||
|
return `${min} ${label}`
|
||||||
|
} else {
|
||||||
|
return `[${min}, ${max}] ${label}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasMin) {
|
||||||
|
return `>= ${min} ${label}`
|
||||||
|
}
|
||||||
|
if (hasMax) {
|
||||||
|
return `<= ${max} ${label}`
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
export const stringifyConstraints = (schema) => {
|
export const stringifyConstraints = (schema) => {
|
||||||
const constraints = []
|
const constraints = []
|
||||||
|
|
||||||
// Validation Keywords for Numeric Instances (number and integer)
|
// validation Keywords for Numeric Instances (number and integer)
|
||||||
const constraintMultipleOf = stringifyConstraintMultipleOf(schema)
|
const constraintMultipleOf = stringifyConstraintMultipleOf(schema)
|
||||||
if (constraintMultipleOf !== null) constraints.push(constraintMultipleOf)
|
if (constraintMultipleOf !== null) constraints.push(constraintMultipleOf)
|
||||||
|
|
||||||
const constraintNumberRange = stringifyConstraintNumberRange(schema)
|
const constraintNumberRange = stringifyConstraintNumberRange(schema)
|
||||||
if (constraintNumberRange !== null) constraints.push(constraintNumberRange)
|
if (constraintNumberRange !== null) constraints.push(constraintNumberRange)
|
||||||
|
|
||||||
|
// validation Keywords for Strings
|
||||||
|
const constraintStringRange = stringifyConstraintRange(
|
||||||
|
"characters",
|
||||||
|
schema?.minLength,
|
||||||
|
schema?.maxLength
|
||||||
|
)
|
||||||
|
if (constraintStringRange !== null) constraints.push(constraintStringRange)
|
||||||
|
if (schema?.pattern) constraints.push(`matches ${schema?.pattern}`)
|
||||||
|
|
||||||
return constraints
|
return constraints
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user