@effect/schema
Advanced tools
Comparing version
@@ -122,2 +122,8 @@ "use strict"; | ||
const isParseJsonTransformation = ast => ast.annotations[AST.TypeAnnotationId] === filters_.ParseJsonTypeId; | ||
function merge(a, b) { | ||
return { | ||
...a, | ||
...b | ||
}; | ||
} | ||
const go = (ast, $defs, handleIdentifier, path) => { | ||
@@ -129,12 +135,5 @@ const hook = AST.getJSONSchemaAnnotation(ast); | ||
try { | ||
return { | ||
...go(ast.from, $defs, true, path), | ||
...getJsonSchemaAnnotations(ast), | ||
...handler | ||
}; | ||
return merge(merge(go(ast.from, $defs, true, path), getJsonSchemaAnnotations(ast)), handler); | ||
} catch (e) { | ||
return { | ||
...getJsonSchemaAnnotations(ast), | ||
...handler | ||
}; | ||
return merge(getJsonSchemaAnnotations(ast), handler); | ||
} | ||
@@ -169,21 +168,9 @@ } | ||
if (literal === null) { | ||
return { | ||
const: null, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
} else if (Predicate.isString(literal)) { | ||
return { | ||
const: literal, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
} else if (Predicate.isNumber(literal)) { | ||
return { | ||
const: literal, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
} else if (Predicate.isBoolean(literal)) { | ||
return { | ||
const: literal, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge({ | ||
const: null | ||
}, getJsonSchemaAnnotations(ast)); | ||
} else if (Predicate.isString(literal) || Predicate.isNumber(literal) || Predicate.isBoolean(literal)) { | ||
return merge({ | ||
const: literal | ||
}, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -201,31 +188,28 @@ throw new Error(errors_.getJSONSchemaMissingAnnotationErrorMessage(path, ast)); | ||
case "UnknownKeyword": | ||
return { | ||
...unknownJsonSchema, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(unknownJsonSchema, getJsonSchemaAnnotations(ast)); | ||
case "AnyKeyword": | ||
return { | ||
...anyJsonSchema, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(anyJsonSchema, getJsonSchemaAnnotations(ast)); | ||
case "ObjectKeyword": | ||
return { | ||
...objectJsonSchema, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(objectJsonSchema, getJsonSchemaAnnotations(ast)); | ||
case "StringKeyword": | ||
return { | ||
type: "string", | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
{ | ||
const out = { | ||
type: "string" | ||
}; | ||
return ast === AST.stringKeyword ? out : merge(out, getJsonSchemaAnnotations(ast)); | ||
} | ||
case "NumberKeyword": | ||
return { | ||
type: "number", | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
{ | ||
const out = { | ||
type: "number" | ||
}; | ||
return ast === AST.numberKeyword ? out : merge(out, getJsonSchemaAnnotations(ast)); | ||
} | ||
case "BooleanKeyword": | ||
return { | ||
type: "boolean", | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
{ | ||
const out = { | ||
type: "boolean" | ||
}; | ||
return ast === AST.booleanKeyword ? out : merge(out, getJsonSchemaAnnotations(ast)); | ||
} | ||
case "BigIntKeyword": | ||
@@ -238,10 +222,4 @@ throw new Error(errors_.getJSONSchemaMissingAnnotationErrorMessage(path, ast)); | ||
const len = ast.elements.length; | ||
const elements = ast.elements.map((e, i) => ({ | ||
...go(e.type, $defs, true, path.concat(i)), | ||
...getJsonSchemaAnnotations(e) | ||
})); | ||
const rest = ast.rest.map(annotatedAST => ({ | ||
...go(annotatedAST.type, $defs, true, path), | ||
...getJsonSchemaAnnotations(annotatedAST) | ||
})); | ||
const elements = ast.elements.map((e, i) => merge(go(e.type, $defs, true, path.concat(i)), getJsonSchemaAnnotations(e))); | ||
const rest = ast.rest.map(annotatedAST => merge(go(annotatedAST.type, $defs, true, path), getJsonSchemaAnnotations(annotatedAST))); | ||
const output = { | ||
@@ -280,6 +258,3 @@ type: "array" | ||
} | ||
return { | ||
...output, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(output, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -289,6 +264,3 @@ case "TypeLiteral": | ||
if (ast.propertySignatures.length === 0 && ast.indexSignatures.length === 0) { | ||
return { | ||
...empty(), | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(empty(), getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -328,6 +300,3 @@ let additionalProperties = undefined; | ||
const propertySignatures = ast.propertySignatures.map(ps => { | ||
return { | ||
...go(pruneUndefinedKeyword(ps), $defs, true, path.concat(ps.name)), | ||
...getJsonSchemaAnnotations(ps) | ||
}; | ||
return merge(go(pruneUndefinedKeyword(ps), $defs, true, path.concat(ps.name)), getJsonSchemaAnnotations(ps)); | ||
}); | ||
@@ -366,6 +335,3 @@ const output = { | ||
} | ||
return { | ||
...output, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(output, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -390,11 +356,9 @@ case "Union": | ||
if (enums.length === 1) { | ||
return { | ||
const: enums[0], | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge({ | ||
const: enums[0] | ||
}, getJsonSchemaAnnotations(ast)); | ||
} else { | ||
return { | ||
enum: enums, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge({ | ||
enum: enums | ||
}, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -411,6 +375,5 @@ } else { | ||
} | ||
return { | ||
anyOf, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge({ | ||
anyOf | ||
}, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -420,3 +383,3 @@ } | ||
{ | ||
return { | ||
return merge({ | ||
$comment: "/schemas/enums", | ||
@@ -426,5 +389,4 @@ oneOf: ast.enums.map(e => ({ | ||
const: e[1] | ||
})), | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
})) | ||
}, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -438,8 +400,7 @@ case "Refinement": | ||
const regex = AST.getTemplateLiteralRegExp(ast); | ||
return { | ||
return merge({ | ||
type: "string", | ||
description: "a template literal", | ||
pattern: regex.source, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
pattern: regex.source | ||
}, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -446,0 +407,0 @@ case "Suspend": |
@@ -89,2 +89,8 @@ /** | ||
const isParseJsonTransformation = ast => ast.annotations[AST.TypeAnnotationId] === filters_.ParseJsonTypeId; | ||
function merge(a, b) { | ||
return { | ||
...a, | ||
...b | ||
}; | ||
} | ||
const go = (ast, $defs, handleIdentifier, path) => { | ||
@@ -96,12 +102,5 @@ const hook = AST.getJSONSchemaAnnotation(ast); | ||
try { | ||
return { | ||
...go(ast.from, $defs, true, path), | ||
...getJsonSchemaAnnotations(ast), | ||
...handler | ||
}; | ||
return merge(merge(go(ast.from, $defs, true, path), getJsonSchemaAnnotations(ast)), handler); | ||
} catch (e) { | ||
return { | ||
...getJsonSchemaAnnotations(ast), | ||
...handler | ||
}; | ||
return merge(getJsonSchemaAnnotations(ast), handler); | ||
} | ||
@@ -136,21 +135,9 @@ } | ||
if (literal === null) { | ||
return { | ||
const: null, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
} else if (Predicate.isString(literal)) { | ||
return { | ||
const: literal, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
} else if (Predicate.isNumber(literal)) { | ||
return { | ||
const: literal, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
} else if (Predicate.isBoolean(literal)) { | ||
return { | ||
const: literal, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge({ | ||
const: null | ||
}, getJsonSchemaAnnotations(ast)); | ||
} else if (Predicate.isString(literal) || Predicate.isNumber(literal) || Predicate.isBoolean(literal)) { | ||
return merge({ | ||
const: literal | ||
}, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -168,31 +155,28 @@ throw new Error(errors_.getJSONSchemaMissingAnnotationErrorMessage(path, ast)); | ||
case "UnknownKeyword": | ||
return { | ||
...unknownJsonSchema, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(unknownJsonSchema, getJsonSchemaAnnotations(ast)); | ||
case "AnyKeyword": | ||
return { | ||
...anyJsonSchema, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(anyJsonSchema, getJsonSchemaAnnotations(ast)); | ||
case "ObjectKeyword": | ||
return { | ||
...objectJsonSchema, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(objectJsonSchema, getJsonSchemaAnnotations(ast)); | ||
case "StringKeyword": | ||
return { | ||
type: "string", | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
{ | ||
const out = { | ||
type: "string" | ||
}; | ||
return ast === AST.stringKeyword ? out : merge(out, getJsonSchemaAnnotations(ast)); | ||
} | ||
case "NumberKeyword": | ||
return { | ||
type: "number", | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
{ | ||
const out = { | ||
type: "number" | ||
}; | ||
return ast === AST.numberKeyword ? out : merge(out, getJsonSchemaAnnotations(ast)); | ||
} | ||
case "BooleanKeyword": | ||
return { | ||
type: "boolean", | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
{ | ||
const out = { | ||
type: "boolean" | ||
}; | ||
return ast === AST.booleanKeyword ? out : merge(out, getJsonSchemaAnnotations(ast)); | ||
} | ||
case "BigIntKeyword": | ||
@@ -205,10 +189,4 @@ throw new Error(errors_.getJSONSchemaMissingAnnotationErrorMessage(path, ast)); | ||
const len = ast.elements.length; | ||
const elements = ast.elements.map((e, i) => ({ | ||
...go(e.type, $defs, true, path.concat(i)), | ||
...getJsonSchemaAnnotations(e) | ||
})); | ||
const rest = ast.rest.map(annotatedAST => ({ | ||
...go(annotatedAST.type, $defs, true, path), | ||
...getJsonSchemaAnnotations(annotatedAST) | ||
})); | ||
const elements = ast.elements.map((e, i) => merge(go(e.type, $defs, true, path.concat(i)), getJsonSchemaAnnotations(e))); | ||
const rest = ast.rest.map(annotatedAST => merge(go(annotatedAST.type, $defs, true, path), getJsonSchemaAnnotations(annotatedAST))); | ||
const output = { | ||
@@ -247,6 +225,3 @@ type: "array" | ||
} | ||
return { | ||
...output, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(output, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -256,6 +231,3 @@ case "TypeLiteral": | ||
if (ast.propertySignatures.length === 0 && ast.indexSignatures.length === 0) { | ||
return { | ||
...empty(), | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(empty(), getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -295,6 +267,3 @@ let additionalProperties = undefined; | ||
const propertySignatures = ast.propertySignatures.map(ps => { | ||
return { | ||
...go(pruneUndefinedKeyword(ps), $defs, true, path.concat(ps.name)), | ||
...getJsonSchemaAnnotations(ps) | ||
}; | ||
return merge(go(pruneUndefinedKeyword(ps), $defs, true, path.concat(ps.name)), getJsonSchemaAnnotations(ps)); | ||
}); | ||
@@ -333,6 +302,3 @@ const output = { | ||
} | ||
return { | ||
...output, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge(output, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -357,11 +323,9 @@ case "Union": | ||
if (enums.length === 1) { | ||
return { | ||
const: enums[0], | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge({ | ||
const: enums[0] | ||
}, getJsonSchemaAnnotations(ast)); | ||
} else { | ||
return { | ||
enum: enums, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge({ | ||
enum: enums | ||
}, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -378,6 +342,5 @@ } else { | ||
} | ||
return { | ||
anyOf, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
return merge({ | ||
anyOf | ||
}, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -387,3 +350,3 @@ } | ||
{ | ||
return { | ||
return merge({ | ||
$comment: "/schemas/enums", | ||
@@ -393,5 +356,4 @@ oneOf: ast.enums.map(e => ({ | ||
const: e[1] | ||
})), | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
})) | ||
}, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -405,8 +367,7 @@ case "Refinement": | ||
const regex = AST.getTemplateLiteralRegExp(ast); | ||
return { | ||
return merge({ | ||
type: "string", | ||
description: "a template literal", | ||
pattern: regex.source, | ||
...getJsonSchemaAnnotations(ast) | ||
}; | ||
pattern: regex.source | ||
}, getJsonSchemaAnnotations(ast)); | ||
} | ||
@@ -413,0 +374,0 @@ case "Suspend": |
{ | ||
"name": "@effect/schema", | ||
"version": "0.68.12", | ||
"version": "0.68.13", | ||
"description": "Modeling the schema of data structures as first-class values", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -304,2 +304,9 @@ /** | ||
function merge(a: JsonSchemaAnnotations, b: JsonSchema7): JsonSchema7 | ||
function merge(a: JsonSchema7, b: JsonSchemaAnnotations): JsonSchema7 | ||
function merge(a: JsonSchema7, b: JsonSchema7): JsonSchema7 | ||
function merge(a: object, b: object): object { | ||
return { ...a, ...b } | ||
} | ||
const go = ( | ||
@@ -316,5 +323,5 @@ ast: AST.AST, | ||
try { | ||
return { ...go(ast.from, $defs, true, path), ...getJsonSchemaAnnotations(ast), ...handler } | ||
return merge(merge(go(ast.from, $defs, true, path), getJsonSchemaAnnotations(ast)), handler) | ||
} catch (e) { | ||
return { ...getJsonSchemaAnnotations(ast), ...handler } | ||
return merge(getJsonSchemaAnnotations(ast), handler) | ||
} | ||
@@ -346,9 +353,5 @@ } | ||
if (literal === null) { | ||
return { const: null, ...getJsonSchemaAnnotations(ast) } | ||
} else if (Predicate.isString(literal)) { | ||
return { const: literal, ...getJsonSchemaAnnotations(ast) } | ||
} else if (Predicate.isNumber(literal)) { | ||
return { const: literal, ...getJsonSchemaAnnotations(ast) } | ||
} else if (Predicate.isBoolean(literal)) { | ||
return { const: literal, ...getJsonSchemaAnnotations(ast) } | ||
return merge({ const: null }, getJsonSchemaAnnotations(ast)) | ||
} else if (Predicate.isString(literal) || Predicate.isNumber(literal) || Predicate.isBoolean(literal)) { | ||
return merge({ const: literal }, getJsonSchemaAnnotations(ast)) | ||
} | ||
@@ -366,13 +369,19 @@ throw new Error(errors_.getJSONSchemaMissingAnnotationErrorMessage(path, ast)) | ||
case "UnknownKeyword": | ||
return { ...unknownJsonSchema, ...getJsonSchemaAnnotations(ast) } | ||
return merge(unknownJsonSchema, getJsonSchemaAnnotations(ast)) | ||
case "AnyKeyword": | ||
return { ...anyJsonSchema, ...getJsonSchemaAnnotations(ast) } | ||
return merge(anyJsonSchema, getJsonSchemaAnnotations(ast)) | ||
case "ObjectKeyword": | ||
return { ...objectJsonSchema, ...getJsonSchemaAnnotations(ast) } | ||
case "StringKeyword": | ||
return { type: "string", ...getJsonSchemaAnnotations(ast) } | ||
case "NumberKeyword": | ||
return { type: "number", ...getJsonSchemaAnnotations(ast) } | ||
case "BooleanKeyword": | ||
return { type: "boolean", ...getJsonSchemaAnnotations(ast) } | ||
return merge(objectJsonSchema, getJsonSchemaAnnotations(ast)) | ||
case "StringKeyword": { | ||
const out: JsonSchema7 = { type: "string" } | ||
return ast === AST.stringKeyword ? out : merge(out, getJsonSchemaAnnotations(ast)) | ||
} | ||
case "NumberKeyword": { | ||
const out: JsonSchema7 = { type: "number" } | ||
return ast === AST.numberKeyword ? out : merge(out, getJsonSchemaAnnotations(ast)) | ||
} | ||
case "BooleanKeyword": { | ||
const out: JsonSchema7 = { type: "boolean" } | ||
return ast === AST.booleanKeyword ? out : merge(out, getJsonSchemaAnnotations(ast)) | ||
} | ||
case "BigIntKeyword": | ||
@@ -384,10 +393,14 @@ throw new Error(errors_.getJSONSchemaMissingAnnotationErrorMessage(path, ast)) | ||
const len = ast.elements.length | ||
const elements = ast.elements.map((e, i) => ({ | ||
...go(e.type, $defs, true, path.concat(i)), | ||
...getJsonSchemaAnnotations(e) | ||
})) | ||
const rest = ast.rest.map((annotatedAST) => ({ | ||
...go(annotatedAST.type, $defs, true, path), | ||
...getJsonSchemaAnnotations(annotatedAST) | ||
})) | ||
const elements = ast.elements.map((e, i) => | ||
merge( | ||
go(e.type, $defs, true, path.concat(i)), | ||
getJsonSchemaAnnotations(e) | ||
) | ||
) | ||
const rest = ast.rest.map((annotatedAST) => | ||
merge( | ||
go(annotatedAST.type, $defs, true, path), | ||
getJsonSchemaAnnotations(annotatedAST) | ||
) | ||
) | ||
const output: JsonSchema7Array = { type: "array" } | ||
@@ -426,7 +439,7 @@ // --------------------------------------------- | ||
return { ...output, ...getJsonSchemaAnnotations(ast) } | ||
return merge(output, getJsonSchemaAnnotations(ast)) | ||
} | ||
case "TypeLiteral": { | ||
if (ast.propertySignatures.length === 0 && ast.indexSignatures.length === 0) { | ||
return { ...empty(), ...getJsonSchemaAnnotations(ast) } | ||
return merge(empty(), getJsonSchemaAnnotations(ast)) | ||
} | ||
@@ -466,6 +479,6 @@ let additionalProperties: JsonSchema7 | undefined = undefined | ||
const propertySignatures = ast.propertySignatures.map((ps) => { | ||
return { | ||
...go(pruneUndefinedKeyword(ps), $defs, true, path.concat(ps.name)), | ||
...getJsonSchemaAnnotations(ps) | ||
} | ||
return merge( | ||
go(pruneUndefinedKeyword(ps), $defs, true, path.concat(ps.name)), | ||
getJsonSchemaAnnotations(ps) | ||
) | ||
}) | ||
@@ -505,3 +518,3 @@ const output: JsonSchema7Object = { | ||
return { ...output, ...getJsonSchemaAnnotations(ast) } | ||
return merge(output, getJsonSchemaAnnotations(ast)) | ||
} | ||
@@ -525,5 +538,5 @@ case "Union": { | ||
if (enums.length === 1) { | ||
return { const: enums[0], ...getJsonSchemaAnnotations(ast) } | ||
return merge({ const: enums[0] }, getJsonSchemaAnnotations(ast)) | ||
} else { | ||
return { enum: enums, ...getJsonSchemaAnnotations(ast) } | ||
return merge({ enum: enums }, getJsonSchemaAnnotations(ast)) | ||
} | ||
@@ -536,11 +549,10 @@ } else { | ||
} | ||
return { anyOf, ...getJsonSchemaAnnotations(ast) } | ||
return merge({ anyOf }, getJsonSchemaAnnotations(ast)) | ||
} | ||
} | ||
case "Enums": { | ||
return { | ||
return merge({ | ||
$comment: "/schemas/enums", | ||
oneOf: ast.enums.map((e) => ({ title: e[0], const: e[1] })), | ||
...getJsonSchemaAnnotations(ast) | ||
} | ||
oneOf: ast.enums.map((e) => ({ title: e[0], const: e[1] })) | ||
}, getJsonSchemaAnnotations(ast)) | ||
} | ||
@@ -552,8 +564,7 @@ case "Refinement": { | ||
const regex = AST.getTemplateLiteralRegExp(ast) | ||
return { | ||
return merge({ | ||
type: "string", | ||
description: "a template literal", | ||
pattern: regex.source, | ||
...getJsonSchemaAnnotations(ast) | ||
} | ||
pattern: regex.source | ||
}, getJsonSchemaAnnotations(ast)) | ||
} | ||
@@ -560,0 +571,0 @@ case "Suspend": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2440947
0.01%40577
-0.17%