| 25 | |
| 26 | class SchemaDefaultsCache { |
| 27 | constructor(schema) { |
| 28 | this.paths = []; |
| 29 | this.schemaTypes = []; |
| 30 | this.defaultsByNestedPath = new Map(); |
| 31 | |
| 32 | for (const path of Object.keys(schema.paths)) { |
| 33 | const schemaType = schema.paths[path]; |
| 34 | if (schemaType.defaultValue === undefined) { |
| 35 | continue; |
| 36 | } |
| 37 | const pieces = schemaType.splitPath(); |
| 38 | if (pieces[pieces.length - 1] === '$*') { |
| 39 | continue; |
| 40 | } |
| 41 | this.paths.push(path); |
| 42 | this.schemaTypes.push(schemaType); |
| 43 | |
| 44 | let nestedPath = ''; |
| 45 | for (let i = 0; i < pieces.length - 1; ++i) { |
| 46 | nestedPath = i === 0 ? pieces[0] : nestedPath + '.' + pieces[i]; |
| 47 | if (schema.nested[nestedPath] !== true) { |
| 48 | continue; |
| 49 | } |
| 50 | let entry = this.defaultsByNestedPath.get(nestedPath); |
| 51 | if (entry == null) { |
| 52 | entry = { paths: [], schemaTypes: [], depth: i + 1 }; |
| 53 | this.defaultsByNestedPath.set(nestedPath, entry); |
| 54 | } |
| 55 | entry.paths.push(path); |
| 56 | entry.schemaTypes.push(schemaType); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | module.exports = SchemaDefaultsCache; |