-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Projection specifications (#687)
* Add all that is requires for projection * Fix lint and v8 doc comment. * Update changelog * Fix changelog * Add space in changelog
- Loading branch information
Showing
8 changed files
with
128 additions
and
1 deletion.
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
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
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,38 @@ | ||
import validateProjection from './validate_projection'; | ||
import validateSpec from './validate'; | ||
import v8 from '../reference/v8.json' with {type: 'json'}; | ||
import {ProjectionSpecification} from '../types.g'; | ||
|
||
describe('Validate projection', () => { | ||
it('Should pass when value is undefined', () => { | ||
const errors = validateProjection({validateSpec, value: undefined, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(0); | ||
}); | ||
|
||
test('Should return error when value is not an object', () => { | ||
const errors = validateProjection({validateSpec, value: '' as unknown as ProjectionSpecification, 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 = validateProjection({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 = validateProjection({validateSpec, value: {type: 1 as any}, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(1); | ||
expect(errors[0].message).toBe('type: expected one of [mercator, globe], 1 found'); | ||
}); | ||
|
||
test('Should pass if everything is according to spec', () => { | ||
let errors = validateProjection({validateSpec, value: {type: 'globe'}, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(0); | ||
errors = validateProjection({validateSpec, value: {type: 'mercator'}, 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,43 @@ | ||
import ValidationError from '../error/validation_error'; | ||
import getType from '../util/get_type'; | ||
import v8 from '../reference/v8.json' with {type: 'json'}; | ||
import {ProjectionSpecification, StyleSpecification} from '../types.g'; | ||
|
||
interface ValidateProjectionOptions { | ||
sourceName?: string; | ||
value: ProjectionSpecification; | ||
styleSpec: typeof v8; | ||
style: StyleSpecification; | ||
validateSpec: Function; | ||
} | ||
|
||
export default function validateProjection(options: ValidateProjectionOptions) { | ||
const projection = options.value; | ||
const styleSpec = options.styleSpec; | ||
const projectionSpec = styleSpec.projection; | ||
const style = options.style; | ||
|
||
const rootType = getType(projection); | ||
if (projection === undefined) { | ||
return []; | ||
} else if (rootType !== 'object') { | ||
return [new ValidationError('projection', projection, `object expected, ${rootType} found`)]; | ||
} | ||
|
||
let errors = []; | ||
for (const key in projection) { | ||
if (projectionSpec[key]) { | ||
errors = errors.concat(options.validateSpec({ | ||
key, | ||
value: projection[key], | ||
valueSpec: projectionSpec[key], | ||
style, | ||
styleSpec | ||
})); | ||
} else { | ||
errors = errors.concat([new ValidationError(key, projection[key], `unknown property "${key}"`)]); | ||
} | ||
} | ||
|
||
return errors; | ||
} |