-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add raster-dem encoding parameters to RasterDEMSourceSpecification (#337
) * Add parameters * Update hillshade docs * Add changelog * Changes names to *Factor and baseShift * Add custom encoding validation and tests * Properly validate raster-dem sources * Remove unneeded validation definition * Add additional tests
- Loading branch information
Showing
5 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import validateSpec from './validate'; | ||
import v8 from '../reference/v8.json' assert {type: 'json'}; | ||
import validateRasterDEMSource from './validate_raster_dem_source'; | ||
import {RasterDEMSourceSpecification} from '../types.g'; | ||
|
||
function checkErrorMessage(message: string, key: string, expectedType: string, foundType: string) { | ||
expect(message).toContain(key); | ||
expect(message).toContain(expectedType); | ||
expect(message).toContain(foundType); | ||
} | ||
|
||
describe('Validate source_raster_dem', () => { | ||
test('Should pass when value is undefined', () => { | ||
const errors = validateRasterDEMSource({validateSpec, value: undefined, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(0); | ||
}); | ||
|
||
test('Should return error when value is not an object', () => { | ||
const errors = validateRasterDEMSource({validateSpec, value: '' as unknown as RasterDEMSourceSpecification, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(1); | ||
expect(errors[0].message).toContain('object'); | ||
expect(errors[0].message).toContain('expected'); | ||
}); | ||
|
||
test('Should return error in case of unknown property', () => { | ||
const errors = validateRasterDEMSource({validateSpec, value: {a: 1} as any, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(1); | ||
expect(errors[0].message).toContain('a'); | ||
expect(errors[0].message).toContain('unknown'); | ||
}); | ||
|
||
test('Should return errors according to spec violations', () => { | ||
const errors = validateRasterDEMSource({validateSpec, value: {type: 'raster-dem', url: {} as any, tiles: {} as any, encoding: 'foo' as any}, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(3); | ||
checkErrorMessage(errors[0].message, 'url', 'string', 'object'); | ||
checkErrorMessage(errors[1].message, 'tiles', 'array', 'object'); | ||
checkErrorMessage(errors[2].message, 'encoding', '[terrarium, mapbox, custom]', 'foo'); | ||
}); | ||
|
||
test('Should return errors when custom encoding values are set but encoding is "mapbox"', () => { | ||
const errors = validateRasterDEMSource({validateSpec, value: {type: 'raster-dem', encoding: 'mapbox', 'redFactor': 1.0, 'greenFactor': 1.0, 'blueFactor': 1.0, 'baseShift': 1.0}, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(4); | ||
checkErrorMessage(errors[0].message, 'redFactor', 'custom', 'mapbox'); | ||
checkErrorMessage(errors[1].message, 'greenFactor', 'custom', 'mapbox'); | ||
checkErrorMessage(errors[2].message, 'blueFactor', 'custom', 'mapbox'); | ||
checkErrorMessage(errors[3].message, 'baseShift', 'custom', 'mapbox'); | ||
}); | ||
|
||
test('Should return errors when custom encoding values are set but encoding is "terrarium"', () => { | ||
const errors = validateRasterDEMSource({validateSpec, value: {type: 'raster-dem', encoding: 'terrarium', 'redFactor': 1.0, 'greenFactor': 1.0, 'blueFactor': 1.0, 'baseShift': 1.0}, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(4); | ||
checkErrorMessage(errors[0].message, 'redFactor', 'custom', 'terrarium'); | ||
checkErrorMessage(errors[1].message, 'greenFactor', 'custom', 'terrarium'); | ||
checkErrorMessage(errors[2].message, 'blueFactor', 'custom', 'terrarium'); | ||
checkErrorMessage(errors[3].message, 'baseShift', 'custom', 'terrarium'); | ||
}); | ||
|
||
test('Should pass when custom encoding values are set and encoding is "custom"', () => { | ||
const errors = validateRasterDEMSource({validateSpec, value: {type: 'raster-dem', encoding: 'custom', 'redFactor': 1.0, 'greenFactor': 1.0, 'blueFactor': 1.0, 'baseShift': 1.0}, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(0); | ||
}); | ||
|
||
test('Should pass if everything is according to spec', () => { | ||
const errors = validateRasterDEMSource({validateSpec, value: {type: 'raster-dem'}, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import ValidationError from '../error/validation_error'; | ||
import getType from '../util/get_type'; | ||
import type {RasterDEMSourceSpecification, StyleSpecification} from '../types.g'; | ||
import v8 from '../reference/v8.json' assert {type: 'json'}; | ||
import {unbundle} from '../util/unbundle_jsonlint'; | ||
|
||
interface ValidateRasterDENSourceOptions { | ||
sourceName?: string; | ||
value: RasterDEMSourceSpecification; | ||
styleSpec: typeof v8; | ||
style: StyleSpecification; | ||
validateSpec: Function; | ||
} | ||
|
||
export default function validateRasterDEMSource( | ||
options: ValidateRasterDENSourceOptions | ||
): ValidationError[] { | ||
|
||
const sourceName = options.sourceName ?? ''; | ||
const rasterDEM = options.value; | ||
const styleSpec = options.styleSpec; | ||
const rasterDEMSpec = styleSpec.source_raster_dem; | ||
const style = options.style; | ||
|
||
let errors = []; | ||
|
||
const rootType = getType(rasterDEM); | ||
if (rasterDEM === undefined) { | ||
return errors; | ||
} else if (rootType !== 'object') { | ||
errors.push(new ValidationError('source_raster_dem', rasterDEM, `object expected, ${rootType} found`)); | ||
return errors; | ||
} | ||
|
||
const encoding = unbundle(rasterDEM.encoding); | ||
const isCustomEncoding = encoding === 'custom'; | ||
const customEncodingKeys = ['redFactor', 'greenFactor', 'blueFactor', 'baseShift']; | ||
const encodingName = options.value.encoding ? `"${options.value.encoding}"` : 'Default'; | ||
|
||
for (const key in rasterDEM) { | ||
if (!isCustomEncoding && customEncodingKeys.includes(key)) { | ||
errors.push(new ValidationError(key, rasterDEM[key], `In "${sourceName}": "${key}" is only valid when "encoding" is set to "custom". ${encodingName} encoding found`)); | ||
} else if (rasterDEMSpec[key]) { | ||
errors = errors.concat(options.validateSpec({ | ||
key, | ||
value: rasterDEM[key], | ||
valueSpec: rasterDEMSpec[key], | ||
validateSpec: options.validateSpec, | ||
style, | ||
styleSpec | ||
})); | ||
} else { | ||
errors.push(new ValidationError(key, rasterDEM[key], `unknown property "${key}"`)); | ||
} | ||
} | ||
|
||
return errors; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters