-
-
Notifications
You must be signed in to change notification settings - Fork 0
Support map properties #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,8 @@ import { | |||||||||||||||||||||||
| Enum, | ||||||||||||||||||||||||
| EnumMember, | ||||||||||||||||||||||||
| IntegerLiteral, | ||||||||||||||||||||||||
| MapProperties, | ||||||||||||||||||||||||
| MapValue, | ||||||||||||||||||||||||
| MemberValue, | ||||||||||||||||||||||||
| Parser, | ||||||||||||||||||||||||
| Primitive, | ||||||||||||||||||||||||
|
|
@@ -20,7 +22,7 @@ import { | |||||||||||||||||||||||
| Violation, | ||||||||||||||||||||||||
| } from 'basketry'; | ||||||||||||||||||||||||
| import parse = require('json-to-ast'); | ||||||||||||||||||||||||
| import { getName, resolve, LiteralNode, Literal } from './json'; | ||||||||||||||||||||||||
| import { getName, isLiteralNode, resolve, LiteralNode, Literal } from './json'; | ||||||||||||||||||||||||
| import * as AST from './json-schema'; | ||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||
| parseObjectValidationRules, | ||||||||||||||||||||||||
|
|
@@ -324,6 +326,7 @@ class JsonSchemaParser { | |||||||||||||||||||||||
| name, | ||||||||||||||||||||||||
| description: toDescription(schema.description), | ||||||||||||||||||||||||
| members, | ||||||||||||||||||||||||
| disjunction: { kind: 'DisjunctionKindLiteral', value: 'exclusive' }, | ||||||||||||||||||||||||
| loc, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -354,7 +357,28 @@ class JsonSchemaParser { | |||||||||||||||||||||||
| loc: string | undefined, | ||||||||||||||||||||||||
| ): MemberValue { | ||||||||||||||||||||||||
| if (schema.anyOf) { | ||||||||||||||||||||||||
| // TODO | ||||||||||||||||||||||||
| const members: MemberValue[] = schema.anyOf.map( | ||||||||||||||||||||||||
| (member) => | ||||||||||||||||||||||||
| this.parseType(member, encodeRange(0, member.loc)).memberValue, | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| const name = this.parseTypeName(schema); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!name) return untyped(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| this.unions.set(name.value, { | ||||||||||||||||||||||||
| kind: 'SimpleUnion', | ||||||||||||||||||||||||
| name, | ||||||||||||||||||||||||
| description: toDescription(schema.description), | ||||||||||||||||||||||||
| members, | ||||||||||||||||||||||||
| disjunction: { kind: 'DisjunctionKindLiteral', value: 'inclusive' }, | ||||||||||||||||||||||||
| loc, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||
| kind: 'ComplexValue', | ||||||||||||||||||||||||
| typeName: name, | ||||||||||||||||||||||||
| rules: [], | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return untyped(); | ||||||||||||||||||||||||
|
|
@@ -469,11 +493,14 @@ class JsonSchemaParser { | |||||||||||||||||||||||
| .map((child) => this.parseProperty(child)) | ||||||||||||||||||||||||
| .filter((prop): prop is Property => !!prop); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const mapProperties = this.parseMapProperties(schema, typeName); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| this.types.set(typeName.value, { | ||||||||||||||||||||||||
| kind: 'Type', | ||||||||||||||||||||||||
| name: typeName, | ||||||||||||||||||||||||
| description: toDescription(schema.description), | ||||||||||||||||||||||||
| properties: properties || [], | ||||||||||||||||||||||||
| mapProperties, | ||||||||||||||||||||||||
| rules: Array.from(parseObjectValidationRules(schema)), | ||||||||||||||||||||||||
| loc, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
@@ -632,6 +659,104 @@ class JsonSchemaParser { | |||||||||||||||||||||||
| return untyped(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private parseMapProperties( | ||||||||||||||||||||||||
| schema: AST.AbstractSchemaNode, | ||||||||||||||||||||||||
| typeName: StringLiteral, | ||||||||||||||||||||||||
| ): MapProperties | undefined { | ||||||||||||||||||||||||
| const ap = schema.additionalProperties; | ||||||||||||||||||||||||
| if (!ap) return undefined; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Handle boolean literal additionalProperties (true/false) | ||||||||||||||||||||||||
| if (isLiteralNode(ap.node)) { | ||||||||||||||||||||||||
| // false is handled by objectAdditionalPropertiesRule; true means untyped map | ||||||||||||||||||||||||
| if (ap.node.value !== true) return undefined; | ||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||
| kind: 'MapProperties', | ||||||||||||||||||||||||
| key: { | ||||||||||||||||||||||||
| kind: 'MapKey', | ||||||||||||||||||||||||
| value: { | ||||||||||||||||||||||||
| kind: 'PrimitiveValue', | ||||||||||||||||||||||||
| typeName: { kind: 'PrimitiveLiteral', value: 'string' }, | ||||||||||||||||||||||||
| rules: [], | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| requiredKeys: [], | ||||||||||||||||||||||||
| value: { | ||||||||||||||||||||||||
| kind: 'MapValue', | ||||||||||||||||||||||||
| value: untyped(), | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||
| kind: 'MapProperties', | ||||||||||||||||||||||||
| key: { | ||||||||||||||||||||||||
| kind: 'MapKey', | ||||||||||||||||||||||||
| value: { | ||||||||||||||||||||||||
| kind: 'PrimitiveValue', | ||||||||||||||||||||||||
| typeName: { kind: 'PrimitiveLiteral', value: 'string' }, | ||||||||||||||||||||||||
| rules: [], | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| requiredKeys: [], | ||||||||||||||||||||||||
| value: this.parseMapValue(ap, typeName.value), | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
Comment on lines
+662
to
+703
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private parseMapValue( | ||||||||||||||||||||||||
| schema: AST.AbstractSchemaNode, | ||||||||||||||||||||||||
| parentName: string, | ||||||||||||||||||||||||
| ): MapValue { | ||||||||||||||||||||||||
| const isInline = !schema.ref; | ||||||||||||||||||||||||
| const { memberValue } = this.parseType(schema, encodeRange(0, schema.loc)); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (isInline && memberValue.kind === 'ComplexValue') { | ||||||||||||||||||||||||
| const autoName = memberValue.typeName.value; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const desiredName = | ||||||||||||||||||||||||
| schema.oneOf || schema.anyOf | ||||||||||||||||||||||||
| ? `${parentName}MapValues` | ||||||||||||||||||||||||
| : schema.enum | ||||||||||||||||||||||||
| ? `${parentName}MapValue` | ||||||||||||||||||||||||
| : `${parentName}MapValues`; | ||||||||||||||||||||||||
|
Comment on lines
+716
to
+721
|
||||||||||||||||||||||||
| const desiredName = | |
| schema.oneOf || schema.anyOf | |
| ? `${parentName}MapValues` | |
| : schema.enum | |
| ? `${parentName}MapValue` | |
| : `${parentName}MapValues`; | |
| const hasUnion = !!(schema.oneOf || schema.anyOf); | |
| const desiredName = | |
| schema.enum && !hasUnion | |
| ? `${parentName}MapValue` | |
| : `${parentName}MapValues`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says
additionalProperties: falseis handled byobjectAdditionalPropertiesRule, butobjectAdditionalPropertiesFactorycurrently returnsundefined(TODO), so the parser won’t emit any rule/violation foradditionalProperties: false. Either update this comment to reflect current behavior, or implement the missing additionalProperties rule sofalseis enforced.