json-schema#JSONSchema7Definition TypeScript Examples
The following examples show how to use
json-schema#JSONSchema7Definition.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: model.ts From prisma-json-schema-generator with MIT License | 6 votes |
export function getJSONSchemaModel(
modelMetaData: ModelMetaData,
transformOptions: TransformOptions,
) {
return (model: DMMF.Model): DefinitionMap => {
const definitionPropsMap = model.fields.map(
getJSONSchemaProperty(modelMetaData, transformOptions),
)
const propertiesMap = definitionPropsMap.map(
([name, definition]) => [name, definition] as DefinitionMap,
)
const relationScalarFields = getRelationScalarFields(model)
const propertiesWithoutRelationScalars = propertiesMap.filter(
(prop) =>
relationScalarFields.findIndex((field) => field === prop[0]) ===
-1,
)
const properties = Object.fromEntries(
transformOptions?.keepRelationScalarFields === 'true'
? propertiesMap
: propertiesWithoutRelationScalars,
)
const definition: JSONSchema7Definition = {
type: 'object',
properties,
}
return [model.name, definition]
}
}
Example #2
Source File: properties.ts From prisma-json-schema-generator with MIT License | 6 votes |
function getPropertyDefinition(
modelMetaData: ModelMetaData,
transformOptions: TransformOptions,
field: DMMF.Field,
) {
const type = getJSONSchemaType(field)
const format = getFormatByDMMFType(field.type)
const items = getItemsByDMMFType(field, transformOptions)
const enumList = getEnumListByDMMFType(modelMetaData)(field)
const defaultValue = getDefaultValue(field)
const description = getDescription(field)
const definition: JSONSchema7Definition = {
type,
...(isDefined(defaultValue) && { default: defaultValue }),
...(isDefined(format) && { format }),
...(isDefined(items) && { items }),
...(isDefined(enumList) && { enum: enumList }),
...(isDefined(description) && { description }),
}
return definition
}
Example #3
Source File: transformDMMF.ts From prisma-json-schema-generator with MIT License | 6 votes |
function getPropertyDefinition({ schemaId }: TransformOptions) {
return (
model: DMMF.Model,
): [name: string, reference: JSONSchema7Definition] => {
const ref = `${DEFINITIONS_ROOT}${model.name}`
return [
toCamelCase(model.name),
{
$ref: schemaId ? `${schemaId}${ref}` : ref,
},
]
}
}
Example #4
Source File: ActionsPage.tsx From backstage with Apache License 2.0 | 4 votes |
ActionsPage = () => {
const api = useApi(scaffolderApiRef);
const classes = useStyles();
const { loading, value, error } = useAsync(async () => {
return api.listActions();
});
if (loading) {
return <Progress />;
}
if (error) {
return (
<ErrorPage
statusMessage="Failed to load installed actions"
status="500"
/>
);
}
const formatRows = (input: JSONSchema7) => {
const properties = input.properties;
if (!properties) {
return undefined;
}
return Object.entries(properties).map(entry => {
const [key] = entry;
const props = entry[1] as unknown as JSONSchema7;
const codeClassname = classNames(classes.code, {
[classes.codeRequired]: input.required?.includes(key),
});
return (
<TableRow key={key}>
<TableCell>
<div className={codeClassname}>{key}</div>
</TableCell>
<TableCell>{props.title}</TableCell>
<TableCell>{props.description}</TableCell>
<TableCell>
<span className={classes.code}>{props.type}</span>
</TableCell>
</TableRow>
);
});
};
const renderTable = (input: JSONSchema7) => {
if (!input.properties) {
return undefined;
}
return (
<TableContainer component={Paper}>
<Table size="small">
<TableHead>
<TableRow>
<TableCell>Name</TableCell>
<TableCell>Title</TableCell>
<TableCell>Description</TableCell>
<TableCell>Type</TableCell>
</TableRow>
</TableHead>
<TableBody>{formatRows(input)}</TableBody>
</Table>
</TableContainer>
);
};
const renderTables = (name: string, input?: JSONSchema7Definition[]) => {
if (!input) {
return undefined;
}
return (
<>
<Typography variant="h6">{name}</Typography>
{input.map((i, index) => (
<div key={index}>{renderTable(i as unknown as JSONSchema7)}</div>
))}
</>
);
};
const items = value?.map(action => {
if (action.id.startsWith('legacy:')) {
return undefined;
}
const oneOf = renderTables('oneOf', action.schema?.input?.oneOf);
return (
<Box pb={4} key={action.id}>
<Typography variant="h4" className={classes.code}>
{action.id}
</Typography>
<Typography>{action.description}</Typography>
{action.schema?.input && (
<Box pb={2}>
<Typography variant="h5">Input</Typography>
{renderTable(action.schema.input)}
{oneOf}
</Box>
)}
{action.schema?.output && (
<Box pb={2}>
<Typography variant="h5">Output</Typography>
{renderTable(action.schema.output)}
</Box>
)}
</Box>
);
});
return (
<Page themeId="home">
<Header
pageTitleOverride="Create a New Component"
title="Installed actions"
subtitle="This is the collection of all installed actions"
/>
<Content>{items}</Content>
</Page>
);
}
Example #5
Source File: transform.ts From farrow with MIT License | 4 votes |
transformResult = (formatResult: FormatResult): JSONSchema7 => {
const transformType = (input: FormatType): JSONSchema7 => {
switch (input.type) {
case 'Scalar': {
return transformScalarType(input)
}
case 'Object': {
return transformObjectType(input)
}
case 'Union': {
return transformUnionType(input)
}
case 'Intersect': {
return transformIntersectType(input)
}
case 'Struct': {
return transformStructType(input)
}
case 'Record': {
return transformRecordType(input)
}
case 'List': {
return transformListType(input)
}
case 'Tuple': {
return transformTupleType(input)
}
case 'Literal': {
return transformLiteralType(input)
}
case 'Nullable': {
return transformNullableType(input)
}
case 'Strict': {
return transformStrictType(input)
}
case 'NonStrict': {
return transformNonStrictType(input)
}
case 'ReadOnly': {
return transformReadOnlyType(input)
}
case 'ReadOnlyDeep': {
return transformReadOnlyDeepType(input)
}
// for eslint
default: {
throw new Error(`Unknown format type: ${input}`)
}
}
}
const transformScalarType = (input: FormatScalarType): JSONSchema7 => {
switch (input.valueName) {
case 'String':
return {
type: 'string',
}
case 'ID':
return {
type: 'integer',
}
case 'Number':
return {
type: 'number',
}
case 'Int':
return {
type: 'integer',
}
case 'Float':
return {
type: 'number',
}
case 'Boolean':
return {
type: 'boolean',
}
case 'Date':
return {
type: 'string',
}
case 'Unknown':
return {}
case 'Any':
return {}
case 'Json':
return {}
default:
throw new Error(`Unknown Scalar Type name: ${input.valueName}`)
}
}
const transformObjectType = (input: FormatObjectType): JSONSchema7 => {
const fields = transformFieldsType(input.fields)
return {
...fields,
type: 'object',
}
}
type Properties = {
[key: string]: JSONSchema7Definition
}
const transformFieldsType = (input: FormatFields): JSONSchema7 => {
const properties: Properties = {}
for (const name in input) {
properties[name] = transformFieldType(input[name])
}
return {
properties,
}
}
const transformFieldType = (input: FormatField): JSONSchema7 => {
return {
...findSchema(input.typeId),
description: input.description,
}
}
const transformUnionType = (input: FormatUnionType): JSONSchema7 => {
const items: JSONSchema7[] = input.itemTypes.map(({ typeId }) => findSchema(typeId))
return {
oneOf: items,
}
}
const transformIntersectType = (input: FormatIntersectType): JSONSchema7 => {
const items: JSONSchema7[] = input.itemTypes.map(({ typeId }) => findSchema(typeId))
const properties: Properties = {}
for (const item of items) {
if (item.properties) {
for (const key in item.properties) {
properties[key] = item.properties[key]
}
}
}
return {
type: 'object',
properties,
}
}
const transformStructType = (input: FormatStructType): JSONSchema7 => {
const fields = transformFieldsType(input.fields)
return {
type: 'object',
...fields,
}
}
const transformRecordType = (input: FormatRecordType): JSONSchema7 => {
const item = findSchema(input.valueTypeId)
return {
type: 'object',
additionalProperties: item,
}
}
const transformListType = (input: FormatListType): JSONSchema7 => {
const item = findSchema(input.itemTypeId)
return {
type: 'array',
additionalItems: item,
}
}
const transformTupleType = (input: FormatTupleType): JSONSchema7 => {
const items = input.itemTypes.map(({ typeId }) => findSchema(typeId))
return {
type: 'array',
items,
}
}
const transformLiteralType = (input: FormatLiteralType): JSONSchema7 => {
return {
const: [input.value!],
}
}
const transformNullableType = (input: FormatNullableType): JSONSchema7 => {
const item = findSchema(input.itemTypeId)
return {
anyOf: [
item,
{
const: [null],
},
],
}
}
const transformStrictType = (input: FormatStrictType): JSONSchema7 => {
const item = findSchema(input.itemTypeId)
return item
}
const transformNonStrictType = (input: FormatNonStrictType): JSONSchema7 => {
const item = findSchema(input.itemTypeId)
return item
}
const transformReadOnlyType = (input: FormatReadOnlyType): JSONSchema7 => {
const item = findSchema(input.itemTypeId)
return item
}
const transformReadOnlyDeepType = (input: FormatReadonlyDeepType): JSONSchema7 => {
const item = findSchema(input.itemTypeId)
return item
}
const schemas = new Map<string, JSONSchema7>()
const findSchema = (typeId: number): JSONSchema7 => {
const schema = schemas.get(typeId.toString())
if (!schema) {
const item = findType(typeId)
const schema = transformType(item)
schemas.set(`${typeId}`, schema)
}
return {
$ref: `#/definitions/${typeId}`,
}
}
const findType = (typeId: number): FormatType => {
for (const key in formatResult.types) {
if (key === `${typeId}`) {
return formatResult.types[key]
}
}
throw new Error(`Unknown typeId: ${typeId}`)
}
for (const id in formatResult.types) {
if (!schemas.has(id)) {
schemas.set(id, {
$id: id,
...transformType(formatResult.types[id]),
})
}
}
const definitions = Object.fromEntries(schemas)
return {
$id: '/farrow/schema',
definitions,
...findSchema(formatResult.typeId),
}
}