(props: {
metadata: MetadataSchema;
explore: MetadataFactory.IExplore;
allowOptional?: boolean | undefined;
})
| 115 | }; |
| 116 | |
| 117 | export const validate = (props: { |
| 118 | metadata: MetadataSchema; |
| 119 | explore: MetadataFactory.IExplore; |
| 120 | allowOptional?: boolean | undefined; |
| 121 | }): string[] => { |
| 122 | const errors: string[] = []; |
| 123 | const insert = (msg: string) => errors.push(msg); |
| 124 | |
| 125 | if (props.explore.top === true) { |
| 126 | // TOP MUST BE ONLY OBJECT |
| 127 | if (props.metadata.objects.length !== 1 || props.metadata.bucket() !== 1) |
| 128 | insert("only one object type is allowed."); |
| 129 | if (props.metadata.nullable === true) |
| 130 | insert("query parameters cannot be null."); |
| 131 | if (props.metadata.isRequired() === false) { |
| 132 | if (props.allowOptional === true) { |
| 133 | const everyPropertiesAreOptional: boolean = |
| 134 | props.metadata.size() === 1 && |
| 135 | props.metadata.objects.length === 1 && |
| 136 | props.metadata.objects[0]!.type.properties.every( |
| 137 | (p) => p.value.isRequired() === false, |
| 138 | ); |
| 139 | if (everyPropertiesAreOptional === false) |
| 140 | insert( |
| 141 | "query parameters can be optional only when every properties are optional.", |
| 142 | ); |
| 143 | } else insert("query parameters cannot be undefined."); |
| 144 | } |
| 145 | } else if ( |
| 146 | props.explore.nested !== null && |
| 147 | props.explore.nested instanceof MetadataArrayType |
| 148 | ) { |
| 149 | //---- |
| 150 | // ARRAY |
| 151 | //---- |
| 152 | const atomics = HttpMetadataUtil.atomics(props.metadata); |
| 153 | const expected: number = |
| 154 | props.metadata.atomics.length + |
| 155 | props.metadata.templates.length + |
| 156 | props.metadata.constants |
| 157 | .map((c) => c.values.length) |
| 158 | .reduce((a, b) => a + b, 0); |
| 159 | if (atomics.size > 1) insert("union type is not allowed in array."); |
| 160 | if (props.metadata.size() !== expected) |
| 161 | insert("only atomic or constant types are allowed in array."); |
| 162 | } else if (props.explore.object && props.explore.property !== null) { |
| 163 | //---- |
| 164 | // COMMON |
| 165 | //---- |
| 166 | // PROPERTY MUST BE SOLE |
| 167 | if (typeof props.explore.property === "object") |
| 168 | insert("dynamic property is not allowed."); |
| 169 | // DO NOT ALLOW TUPLE TYPE |
| 170 | if (props.metadata.tuples.length) insert("tuple type is not allowed."); |
| 171 | // DO NOT ALLOW UNION TYPE |
| 172 | if (HttpMetadataUtil.isUnion(props.metadata)) |
| 173 | insert("union type is not allowed."); |
| 174 | // DO NOT ALLOW NESTED OBJECT |
no test coverage detected
searching dependent graphs…