-
-
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.
* Style Spec for sky and fog (#298) * Copy style-spec pieces from maplibre/maplibre-gl-js#1713 * add missing SkySpecification * fix lint error * Add tests, improve documentation, add to the style-spec docs * Fix lint * Update changelog --------- Co-authored-by: Andrew Calcutt <acalcutt@techidiots.net>
- Loading branch information
Showing
13 changed files
with
198 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
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,26 @@ | ||
import {Markdown} from '~/components/markdown/markdown'; | ||
import ref from '../../../../src/reference/latest'; | ||
import {Items} from '../../components/items/items'; | ||
function Root() { | ||
|
||
const md = `# Sky | ||
Add sky and fog to the map. | ||
This feautre is still under development and is not yet available in the latest release. | ||
\`\`\`json | ||
"sky": ${JSON.stringify(ref.$root.sky.example, null, 2)} | ||
\`\`\` | ||
`; | ||
|
||
return ( | ||
<div> | ||
<Markdown content={md} /> | ||
<Items headingLevel='3' entry={ref.sky} /> | ||
</div> | ||
); | ||
} | ||
|
||
export default Root; |
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,40 @@ | ||
import validateSky from './validate_sky'; | ||
import validateSpec from './validate'; | ||
import v8 from '../reference/v8.json' assert {type: 'json'}; | ||
import {SkySpecification} from '../types.g'; | ||
|
||
describe('Validate sky', () => { | ||
it('Should pass when value is undefined', () => { | ||
const errors = validateSky({validateSpec, value: undefined, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(0); | ||
}); | ||
|
||
test('Should return error when value is not an object', () => { | ||
const errors = validateSky({validateSpec, value: '' as unknown as SkySpecification, 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 = validateSky({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 = validateSky({validateSpec, value: {'sky-color': 1 as any, 'fog-color': 2 as any, 'horizon-blend': {} as any, 'fog-blend': 'foo' as any}, styleSpec: v8, style: {} as any}); | ||
expect(errors).toHaveLength(4); | ||
expect(errors[0].message).toBe('sky-color: color expected, number found'); | ||
expect(errors[1].message).toBe('fog-color: color expected, number found'); | ||
expect(errors[2].message).toBe('horizon-blend: missing required property "stops"'); | ||
expect(errors[3].message).toBe('fog-blend: number expected, string found'); | ||
}); | ||
|
||
test('Should pass if everything is according to spec', () => { | ||
const errors = validateSky({validateSpec, value: {'sky-color': 'red', 'fog-color': '#123456', 'horizon-blend': 1, 'fog-blend': 0}, 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,44 @@ | ||
import ValidationError from '../error/validation_error'; | ||
import getType from '../util/get_type'; | ||
import validate from './validate'; | ||
import v8 from '../reference/v8.json' assert {type: 'json'}; | ||
import {SkySpecification, StyleSpecification} from '../types.g'; | ||
|
||
interface ValidateSkyOptions { | ||
sourceName?: string; | ||
value: SkySpecification; | ||
styleSpec: typeof v8; | ||
style: StyleSpecification; | ||
validateSpec: Function; | ||
} | ||
|
||
export default function validateSky(options: ValidateSkyOptions) { | ||
const sky = options.value; | ||
const styleSpec = options.styleSpec; | ||
const skySpec = styleSpec.sky; | ||
const style = options.style; | ||
|
||
const rootType = getType(sky); | ||
if (sky === undefined) { | ||
return []; | ||
} else if (rootType !== 'object') { | ||
return [new ValidationError('sky', sky, `object expected, ${rootType} found`)]; | ||
} | ||
|
||
let errors = []; | ||
for (const key in sky) { | ||
if (skySpec[key]) { | ||
errors = errors.concat(validate({ | ||
key, | ||
value: sky[key], | ||
valueSpec: skySpec[key], | ||
style, | ||
styleSpec | ||
})); | ||
} else { | ||
errors = errors.concat([new ValidationError(key, sky[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
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