refactor(json-schema-2020-12-samples): design formatAPI consistent with mediatypeAPI and encoderAPI (#9799)
Refs #9739
This commit is contained in:
@@ -2,9 +2,9 @@
|
|||||||
* @prettier
|
* @prettier
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Registry from "../class/Registry"
|
import FormatRegistry from "../class/FormatRegistry"
|
||||||
|
|
||||||
const registry = new Registry()
|
const registry = new FormatRegistry()
|
||||||
|
|
||||||
const formatAPI = (format, generator) => {
|
const formatAPI = (format, generator) => {
|
||||||
if (typeof generator === "function") {
|
if (typeof generator === "function") {
|
||||||
@@ -15,5 +15,6 @@ const formatAPI = (format, generator) => {
|
|||||||
|
|
||||||
return registry.get(format)
|
return registry.get(format)
|
||||||
}
|
}
|
||||||
|
formatAPI.getDefaults = () => registry.defaults
|
||||||
|
|
||||||
export default formatAPI
|
export default formatAPI
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
/**
|
||||||
|
* @prettier
|
||||||
|
*/
|
||||||
|
import Registry from "./Registry"
|
||||||
|
import int32Generator from "../generators/int32"
|
||||||
|
import int64Generator from "../generators/int64"
|
||||||
|
import floatGenerator from "../generators/float"
|
||||||
|
import doubleGenerator from "../generators/double"
|
||||||
|
import emailGenerator from "../generators/email"
|
||||||
|
import idnEmailGenerator from "../generators/idn-email"
|
||||||
|
import hostnameGenerator from "../generators/hostname"
|
||||||
|
import idnHostnameGenerator from "../generators/idn-hostname"
|
||||||
|
import ipv4Generator from "../generators/ipv4"
|
||||||
|
import ipv6Generator from "../generators/ipv6"
|
||||||
|
import uriGenerator from "../generators/uri"
|
||||||
|
import uriReferenceGenerator from "../generators/uri-reference"
|
||||||
|
import iriGenerator from "../generators/iri"
|
||||||
|
import iriReferenceGenerator from "../generators/iri-reference"
|
||||||
|
import uuidGenerator from "../generators/uuid"
|
||||||
|
import uriTemplateGenerator from "../generators/uri-template"
|
||||||
|
import jsonPointerGenerator from "../generators/json-pointer"
|
||||||
|
import relativeJsonPointerGenerator from "../generators/relative-json-pointer"
|
||||||
|
import dateTimeGenerator from "../generators/date-time"
|
||||||
|
import dateGenerator from "../generators/date"
|
||||||
|
import timeGenerator from "../generators/time"
|
||||||
|
import durationGenerator from "../generators/duration"
|
||||||
|
import passwordGenerator from "../generators/password"
|
||||||
|
import regexGenerator from "../generators/regex"
|
||||||
|
|
||||||
|
class FormatRegistry extends Registry {
|
||||||
|
#defaults = {
|
||||||
|
int32: int32Generator,
|
||||||
|
int64: int64Generator,
|
||||||
|
float: floatGenerator,
|
||||||
|
double: doubleGenerator,
|
||||||
|
email: emailGenerator,
|
||||||
|
"idn-email": idnEmailGenerator,
|
||||||
|
hostname: hostnameGenerator,
|
||||||
|
"idn-hostname": idnHostnameGenerator,
|
||||||
|
ipv4: ipv4Generator,
|
||||||
|
ipv6: ipv6Generator,
|
||||||
|
uri: uriGenerator,
|
||||||
|
"uri-reference": uriReferenceGenerator,
|
||||||
|
iri: iriGenerator,
|
||||||
|
"iri-reference": iriReferenceGenerator,
|
||||||
|
uuid: uuidGenerator,
|
||||||
|
"uri-template": uriTemplateGenerator,
|
||||||
|
"json-pointer": jsonPointerGenerator,
|
||||||
|
"relative-json-pointer": relativeJsonPointerGenerator,
|
||||||
|
"date-time": dateTimeGenerator,
|
||||||
|
date: dateGenerator,
|
||||||
|
time: timeGenerator,
|
||||||
|
duration: durationGenerator,
|
||||||
|
password: passwordGenerator,
|
||||||
|
regex: regexGenerator,
|
||||||
|
}
|
||||||
|
|
||||||
|
data = { ...this.#defaults }
|
||||||
|
|
||||||
|
get defaults() {
|
||||||
|
return { ...this.#defaults }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default FormatRegistry
|
||||||
@@ -3,8 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
import { number as randomNumber } from "../core/random"
|
import { number as randomNumber } from "../core/random"
|
||||||
import formatAPI from "../api/formatAPI"
|
import formatAPI from "../api/formatAPI"
|
||||||
import floatGenerator from "../generators/float"
|
|
||||||
import doubleGenerator from "../generators/double"
|
|
||||||
|
|
||||||
const generateFormat = (schema) => {
|
const generateFormat = (schema) => {
|
||||||
const { format } = schema
|
const { format } = schema
|
||||||
@@ -14,15 +12,6 @@ const generateFormat = (schema) => {
|
|||||||
return formatGenerator(schema)
|
return formatGenerator(schema)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (format) {
|
|
||||||
case "float": {
|
|
||||||
return floatGenerator()
|
|
||||||
}
|
|
||||||
case "double": {
|
|
||||||
return doubleGenerator()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return randomNumber()
|
return randomNumber()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,26 +5,6 @@ import identity from "lodash/identity"
|
|||||||
|
|
||||||
import { string as randomString, randexp } from "../core/random"
|
import { string as randomString, randexp } from "../core/random"
|
||||||
import { isJSONSchema } from "../core/predicates"
|
import { isJSONSchema } from "../core/predicates"
|
||||||
import emailGenerator from "../generators/email"
|
|
||||||
import idnEmailGenerator from "../generators/idn-email"
|
|
||||||
import hostnameGenerator from "../generators/hostname"
|
|
||||||
import idnHostnameGenerator from "../generators/idn-hostname"
|
|
||||||
import ipv4Generator from "../generators/ipv4"
|
|
||||||
import ipv6Generator from "../generators/ipv6"
|
|
||||||
import uriGenerator from "../generators/uri"
|
|
||||||
import uriReferenceGenerator from "../generators/uri-reference"
|
|
||||||
import iriGenerator from "../generators/iri"
|
|
||||||
import iriReferenceGenerator from "../generators/iri-reference"
|
|
||||||
import uuidGenerator from "../generators/uuid"
|
|
||||||
import uriTemplateGenerator from "../generators/uri-template"
|
|
||||||
import jsonPointerGenerator from "../generators/json-pointer"
|
|
||||||
import relativeJsonPointerGenerator from "../generators/relative-json-pointer"
|
|
||||||
import dateTimeGenerator from "../generators/date-time"
|
|
||||||
import dateGenerator from "../generators/date"
|
|
||||||
import timeGenerator from "../generators/time"
|
|
||||||
import durationGenerator from "../generators/duration"
|
|
||||||
import passwordGenerator from "../generators/password"
|
|
||||||
import regexGenerator from "../generators/regex"
|
|
||||||
import formatAPI from "../api/formatAPI"
|
import formatAPI from "../api/formatAPI"
|
||||||
import encoderAPI from "../api/encoderAPI"
|
import encoderAPI from "../api/encoderAPI"
|
||||||
import mediaTypeAPI from "../api/mediaTypeAPI"
|
import mediaTypeAPI from "../api/mediaTypeAPI"
|
||||||
@@ -37,67 +17,15 @@ const generateFormat = (schema) => {
|
|||||||
return formatGenerator(schema)
|
return formatGenerator(schema)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (format) {
|
return randomString()
|
||||||
case "email": {
|
}
|
||||||
return emailGenerator()
|
|
||||||
}
|
const generateMediaType = (schema) => {
|
||||||
case "idn-email": {
|
const { contentMediaType } = schema
|
||||||
return idnEmailGenerator()
|
|
||||||
}
|
const mediaTypeGenerator = mediaTypeAPI(contentMediaType)
|
||||||
case "hostname": {
|
if (typeof mediaTypeGenerator === "function") {
|
||||||
return hostnameGenerator()
|
return mediaTypeGenerator(schema)
|
||||||
}
|
|
||||||
case "idn-hostname": {
|
|
||||||
return idnHostnameGenerator()
|
|
||||||
}
|
|
||||||
case "ipv4": {
|
|
||||||
return ipv4Generator()
|
|
||||||
}
|
|
||||||
case "ipv6": {
|
|
||||||
return ipv6Generator()
|
|
||||||
}
|
|
||||||
case "uri": {
|
|
||||||
return uriGenerator()
|
|
||||||
}
|
|
||||||
case "uri-reference": {
|
|
||||||
return uriReferenceGenerator()
|
|
||||||
}
|
|
||||||
case "iri": {
|
|
||||||
return iriGenerator()
|
|
||||||
}
|
|
||||||
case "iri-reference": {
|
|
||||||
return iriReferenceGenerator()
|
|
||||||
}
|
|
||||||
case "uuid": {
|
|
||||||
return uuidGenerator()
|
|
||||||
}
|
|
||||||
case "uri-template": {
|
|
||||||
return uriTemplateGenerator()
|
|
||||||
}
|
|
||||||
case "json-pointer": {
|
|
||||||
return jsonPointerGenerator()
|
|
||||||
}
|
|
||||||
case "relative-json-pointer": {
|
|
||||||
return relativeJsonPointerGenerator()
|
|
||||||
}
|
|
||||||
case "date-time": {
|
|
||||||
return dateTimeGenerator()
|
|
||||||
}
|
|
||||||
case "date": {
|
|
||||||
return dateGenerator()
|
|
||||||
}
|
|
||||||
case "time": {
|
|
||||||
return timeGenerator()
|
|
||||||
}
|
|
||||||
case "duration": {
|
|
||||||
return durationGenerator()
|
|
||||||
}
|
|
||||||
case "password": {
|
|
||||||
return passwordGenerator()
|
|
||||||
}
|
|
||||||
case "regex": {
|
|
||||||
return regexGenerator()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return randomString()
|
return randomString()
|
||||||
@@ -119,6 +47,7 @@ const applyStringConstraints = (string, constraints = {}) => {
|
|||||||
|
|
||||||
return constrainedString
|
return constrainedString
|
||||||
}
|
}
|
||||||
|
|
||||||
const stringType = (schema, { sample } = {}) => {
|
const stringType = (schema, { sample } = {}) => {
|
||||||
const { contentEncoding, contentMediaType, contentSchema } = schema
|
const { contentEncoding, contentMediaType, contentSchema } = schema
|
||||||
const { pattern, format } = schema
|
const { pattern, format } = schema
|
||||||
@@ -140,10 +69,7 @@ const stringType = (schema, { sample } = {}) => {
|
|||||||
generatedString = applyStringConstraints(String(sample), schema)
|
generatedString = applyStringConstraints(String(sample), schema)
|
||||||
}
|
}
|
||||||
} else if (typeof contentMediaType === "string") {
|
} else if (typeof contentMediaType === "string") {
|
||||||
const mediaTypeGenerator = mediaTypeAPI(contentMediaType)
|
generatedString = generateMediaType(schema)
|
||||||
if (typeof mediaTypeGenerator === "function") {
|
|
||||||
generatedString = mediaTypeGenerator(schema)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
generatedString = applyStringConstraints(randomString(), schema)
|
generatedString = applyStringConstraints(randomString(), schema)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user